Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit 5c03714

Browse files
committed
Add support for no_std
1 parent 9935079 commit 5c03714

File tree

9 files changed

+43
-11
lines changed

9 files changed

+43
-11
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ jobs:
2020
fail-fast: false
2121
matrix:
2222
rust: [stable, beta, nightly, 1.56.0]
23+
features: [--no-default-features, ""]
2324
timeout-minutes: 45
2425
steps:
2526
- uses: actions/checkout@v3
2627
- uses: dtolnay/rust-toolchain@master
2728
with:
2829
toolchain: ${{matrix.rust}}
29-
- run: cargo build
30-
- run: cargo test --features serde/derive,serde/rc
30+
- run: cargo build ${{matrix.features}}
31+
- run: cargo test ${{matrix.features}} --features serde/derive,serde/rc
3132
- uses: actions/upload-artifact@v4
3233
if: matrix.rust == 'nightly' && always()
3334
with:
@@ -38,12 +39,16 @@ jobs:
3839
minimal:
3940
name: Minimal versions
4041
runs-on: ubuntu-latest
42+
strategy:
43+
fail-fast: false
44+
matrix:
45+
features: [--no-default-features, ""]
4146
timeout-minutes: 45
4247
steps:
4348
- uses: actions/checkout@v3
4449
- uses: dtolnay/rust-toolchain@nightly
4550
- run: cargo generate-lockfile -Z minimal-versions
46-
- run: cargo check --locked
51+
- run: cargo check --locked ${{matrix.features}}
4752

4853
doc:
4954
name: Documentation
@@ -61,11 +66,15 @@ jobs:
6166
name: Clippy
6267
runs-on: ubuntu-latest
6368
if: github.event_name != 'pull_request'
69+
strategy:
70+
fail-fast: false
71+
matrix:
72+
features: [--no-default-features, ""]
6473
timeout-minutes: 45
6574
steps:
6675
- uses: actions/checkout@v3
6776
- uses: dtolnay/rust-toolchain@clippy
68-
- run: cargo clippy -- -Dclippy::all -Dclippy::pedantic
77+
- run: cargo clippy ${{matrix.features}} -- -Dclippy::all -Dclippy::pedantic
6978

7079
outdated:
7180
name: Outdated

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ license = "MIT OR Apache-2.0"
1111
repository = "https://github.com/serde-rs/test"
1212
rust-version = "1.56"
1313

14+
[features]
15+
default = ["std"]
16+
std = ["serde/std"]
17+
1418
[dependencies]
15-
serde = "1.0.69"
19+
serde = { version = "1.0.69", default-features = false, features = ["alloc"] }
1620

1721
[dev-dependencies]
18-
serde = { version = "1", features = ["rc"] }
22+
serde = { version = "1", default-features = false, features = ["rc"] }
1923
serde_derive = "1"
2024

2125
[lib]

src/assert.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::de::Deserializer;
22
use crate::ser::Serializer;
33
use crate::token::Token;
4+
use core::fmt::Debug;
45
use serde::{Deserialize, Serialize};
5-
use std::fmt::Debug;
66

77
/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
88
///
@@ -83,6 +83,10 @@ where
8383
/// `error`.
8484
///
8585
/// ```
86+
/// # #[cfg(not(feature = "std"))]
87+
/// # fn main() {}
88+
/// # #[cfg(feature = "std")]
89+
/// # fn main() {
8690
/// use serde_derive::Serialize;
8791
/// use serde_test::{assert_ser_tokens_error, Token};
8892
/// use std::sync::{Arc, Mutex};
@@ -120,6 +124,8 @@ where
120124
/// let error = "lock poison error while serializing";
121125
/// assert_ser_tokens_error(&example, expected, error);
122126
/// }
127+
/// # main();
128+
/// # }
123129
/// ```
124130
#[track_caller]
125131
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: &str)

src/configure.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use alloc::string::String;
2+
use alloc::vec::Vec;
3+
use core::fmt::{self, Display};
14
use serde::de::{
25
Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, MapAccess, SeqAccess,
36
VariantAccess, Visitor,
@@ -6,7 +9,6 @@ use serde::ser::{
69
Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
710
SerializeTupleStruct, SerializeTupleVariant, Serializer,
811
};
9-
use std::fmt::{self, Display};
1012

1113
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
1214
pub struct Readable<T: ?Sized>(T);

src/de.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::error::Error;
22
use crate::token::Token;
3+
use alloc::borrow::ToOwned;
4+
use alloc::format;
5+
use alloc::string::ToString;
36
use serde::de::value::{MapAccessDeserializer, SeqAccessDeserializer};
47
use serde::de::{
58
self, Deserialize, DeserializeSeed, EnumAccess, IntoDeserializer, MapAccess, SeqAccess,

src/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use alloc::string::{String, ToString};
2+
use core::fmt::{self, Display};
13
use serde::{de, ser};
4+
#[cfg(feature = "std")]
25
use std::error;
3-
use std::fmt::{self, Display};
46

57
#[derive(Clone, Debug)]
68
pub struct Error {
@@ -29,6 +31,7 @@ impl fmt::Display for Error {
2931
}
3032
}
3133

34+
#[cfg(feature = "std")]
3235
impl error::Error for Error {
3336
fn description(&self) -> &str {
3437
&self.msg

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
clippy::module_name_repetitions,
159159
clippy::too_many_lines
160160
)]
161+
#![cfg_attr(not(feature = "std"), no_std)]
162+
163+
extern crate alloc;
161164

162165
mod assert;
163166
mod configure;

src/ser.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::error::Error;
22
use crate::token::Token;
3+
use alloc::format;
4+
use alloc::string::String;
35
use serde::ser::{self, Serialize};
46

57
/// A `Serializer` that ensures that a value serializes to a given list of
@@ -45,7 +47,7 @@ macro_rules! assert_next_token {
4547
($ser:expr, $actual:ident { $($k:ident),* }) => {{
4648
let compare = ($($k,)*);
4749
let field_format = || {
48-
use std::fmt::Write;
50+
use core::fmt::Write;
4951
let mut buffer = String::new();
5052
$(
5153
write!(&mut buffer, concat!(stringify!($k), ": {:?}, "), $k).unwrap();

src/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::{self, Debug, Display};
1+
use core::fmt::{self, Debug, Display};
22

33
#[derive(Copy, Clone, PartialEq, Debug)]
44
pub enum Token {

0 commit comments

Comments
 (0)