Skip to content

Commit 229f799

Browse files
bors[bot]matklad
andauthored
40: fix no_std support r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 56eaef5 + 2cdb8d7 commit 229f799

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

.github/ci.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fn try_main() -> Result<()> {
3636
{
3737
let _s = Section::new("TEST");
3838
shell("cargo test --all-features --workspace")?;
39+
shell("cargo test --no-default-features --workspace")?;
3940
}
4041

4142
let current_branch = shell_output("git branch --show-current")?;

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "smol_str"
3-
version = "0.1.19"
3+
version = "0.1.20"
44
description = "small-string optimized string type with O(1) clone"
55
license = "MIT OR Apache-2.0"
66
repository = "https://github.com/matklad/smol_str"

src/lib.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
1-
#![cfg_attr(not(feature = "std"), no_std)]
2-
3-
#[cfg(not(feature = "std"))]
4-
extern crate core as std;
5-
6-
#[cfg(not(feature = "std"))]
1+
#![no_std]
72
extern crate alloc;
83

9-
use core::convert::Infallible;
10-
use std::{
4+
use alloc::{
5+
string::{String, ToString},
6+
sync::Arc,
7+
};
8+
use core::{
119
borrow::Borrow,
1210
cmp::{self, Ordering},
11+
convert::Infallible,
1312
fmt, hash, iter,
1413
ops::Deref,
1514
str::FromStr,
1615
};
1716

18-
#[cfg(not(feature = "std"))]
19-
use alloc::{
20-
string::{String, ToString},
21-
sync::Arc,
22-
};
23-
24-
#[cfg(feature = "std")]
25-
use std::sync::Arc;
26-
2717
/// A `SmolStr` is a string type that has the following properties:
2818
///
2919
/// * `size_of::<SmolStr>() == size_of::<String>()`
@@ -131,7 +121,7 @@ impl SmolStr {
131121
if size + len > INLINE_CAP {
132122
let (min_remaining, _) = iter.size_hint();
133123
let mut heap = String::with_capacity(size + len + min_remaining);
134-
heap.push_str(std::str::from_utf8(&buf[..len]).unwrap());
124+
heap.push_str(core::str::from_utf8(&buf[..len]).unwrap());
135125
heap.push(ch);
136126
heap.extend(iter);
137127
return SmolStr(Repr::Heap(heap.into_boxed_str().into()));
@@ -265,7 +255,7 @@ where
265255
let size = slice.len();
266256
if size + len > INLINE_CAP {
267257
let mut heap = String::with_capacity(size + len);
268-
heap.push_str(std::str::from_utf8(&buf[..len]).unwrap());
258+
heap.push_str(core::str::from_utf8(&buf[..len]).unwrap());
269259
heap.push_str(&slice);
270260
heap.extend(iter);
271261
return SmolStr(Repr::Heap(heap.into_boxed_str().into()));
@@ -411,7 +401,7 @@ impl Repr {
411401
Repr::Inline { len, buf } => {
412402
let len = *len as usize;
413403
let buf = &buf[..len];
414-
unsafe { ::std::str::from_utf8_unchecked(buf) }
404+
unsafe { ::core::str::from_utf8_unchecked(buf) }
415405
}
416406
Repr::Substring { newlines, spaces } => {
417407
let newlines = *newlines;
@@ -425,9 +415,12 @@ impl Repr {
425415

426416
#[cfg(feature = "serde")]
427417
mod serde {
428-
use super::SmolStr;
429-
use ::serde::de::{Deserializer, Error, Unexpected, Visitor};
430-
use std::fmt;
418+
use alloc::{string::String, vec::Vec};
419+
use core::fmt;
420+
421+
use serde::de::{Deserializer, Error, Unexpected, Visitor};
422+
423+
use crate::SmolStr;
431424

432425
// https://github.com/serde-rs/serde/blob/629802f2abfd1a54a6072992888fea7ca5bc209f/serde/src/private/de.rs#L56-L125
433426
fn smol_str<'de: 'a, 'a, D>(deserializer: D) -> Result<SmolStr, D::Error>
@@ -468,7 +461,7 @@ mod serde {
468461
where
469462
E: Error,
470463
{
471-
match std::str::from_utf8(v) {
464+
match core::str::from_utf8(v) {
472465
Ok(s) => Ok(SmolStr::from(s)),
473466
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
474467
}
@@ -478,7 +471,7 @@ mod serde {
478471
where
479472
E: Error,
480473
{
481-
match std::str::from_utf8(v) {
474+
match core::str::from_utf8(v) {
482475
Ok(s) => Ok(SmolStr::from(s)),
483476
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
484477
}

0 commit comments

Comments
 (0)