Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for no_std #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ jobs:
fail-fast: false
matrix:
rust: [stable, beta, nightly, 1.56.0]
features: [--no-default-features, ""]
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{matrix.rust}}
- run: cargo build
- run: cargo test --features serde/derive,serde/rc
- run: cargo build ${{matrix.features}}
- run: cargo test ${{matrix.features}} --features serde/derive,serde/rc
- uses: actions/upload-artifact@v4
if: matrix.rust == 'nightly' && always()
with:
Expand All @@ -38,12 +39,16 @@ jobs:
minimal:
name: Minimal versions
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
features: [--no-default-features, ""]
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@nightly
- run: cargo generate-lockfile -Z minimal-versions
- run: cargo check --locked
- run: cargo check --locked ${{matrix.features}}

doc:
name: Documentation
Expand All @@ -61,11 +66,15 @@ jobs:
name: Clippy
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
strategy:
fail-fast: false
matrix:
features: [--no-default-features, ""]
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@clippy
- run: cargo clippy -- -Dclippy::all -Dclippy::pedantic
- run: cargo clippy ${{matrix.features}} -- -Dclippy::all -Dclippy::pedantic

outdated:
name: Outdated
Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/serde-rs/test"
rust-version = "1.56"

[features]
default = ["std"]
std = ["serde/std"]

[dependencies]
serde = "1.0.69"
serde = { version = "1.0.70", default-features = false, features = ["alloc"] }

[dev-dependencies]
serde = { version = "1", features = ["rc"] }
serde = { version = "1", default-features = false, features = ["rc"] }
serde_derive = "1"

[lib]
Expand Down
8 changes: 7 additions & 1 deletion src/assert.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::de::Deserializer;
use crate::ser::Serializer;
use crate::token::Token;
use core::fmt::Debug;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
///
Expand Down Expand Up @@ -83,6 +83,10 @@ where
/// `error`.
///
/// ```
/// # #[cfg(not(feature = "std"))]
/// # fn main() {}
/// # #[cfg(feature = "std")]
/// # fn main() {
/// use serde_derive::Serialize;
/// use serde_test::{assert_ser_tokens_error, Token};
/// use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -120,6 +124,8 @@ where
/// let error = "lock poison error while serializing";
/// assert_ser_tokens_error(&example, expected, error);
/// }
/// # main();
/// # }
/// ```
#[track_caller]
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: &str)
Expand Down
4 changes: 3 additions & 1 deletion src/configure.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt::{self, Display};
use serde::de::{
Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, MapAccess, SeqAccess,
VariantAccess, Visitor,
Expand All @@ -6,7 +9,6 @@ use serde::ser::{
Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
SerializeTupleStruct, SerializeTupleVariant, Serializer,
};
use std::fmt::{self, Display};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Readable<T: ?Sized>(T);
Expand Down
3 changes: 3 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::error::Error;
use crate::token::Token;
use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::ToString;
use serde::de::value::{MapAccessDeserializer, SeqAccessDeserializer};
use serde::de::{
self, Deserialize, DeserializeSeed, EnumAccess, IntoDeserializer, MapAccess, SeqAccess,
Expand Down
5 changes: 4 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use alloc::string::{String, ToString};
use core::fmt::{self, Display};
use serde::{de, ser};
#[cfg(feature = "std")]
use std::error;
use std::fmt::{self, Display};

#[derive(Clone, Debug)]
pub struct Error {
Expand Down Expand Up @@ -29,6 +31,7 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
impl error::Error for Error {
fn description(&self) -> &str {
&self.msg
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@
clippy::module_name_repetitions,
clippy::too_many_lines
)]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

mod assert;
mod configure;
Expand Down
4 changes: 3 additions & 1 deletion src/ser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::error::Error;
use crate::token::Token;
use alloc::format;
use alloc::string::String;
use serde::ser::{self, Serialize};

/// A `Serializer` that ensures that a value serializes to a given list of
Expand Down Expand Up @@ -45,7 +47,7 @@ macro_rules! assert_next_token {
($ser:expr, $actual:ident { $($k:ident),* }) => {{
let compare = ($($k,)*);
let field_format = || {
use std::fmt::Write;
use core::fmt::Write;
let mut buffer = String::new();
$(
write!(&mut buffer, concat!(stringify!($k), ": {:?}, "), $k).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{self, Debug, Display};
use core::fmt::{self, Debug, Display};

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Token {
Expand Down
Loading