diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fe1da43..2c9c56b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -80,7 +80,7 @@ jobs: - uses: actions-rs/cargo@v1 with: command: test - args: --no-default-features --features ${{ matrix.feature }} + args: --no-default-features --features "std, ${{ matrix.feature }}" no_std_features: name: Test Individual Features NoStd @@ -106,7 +106,7 @@ jobs: - uses: actions-rs/cargo@v1 with: command: test - args: --no-default-features --features "no_std, ${{ matrix.feature }}" + args: --no-default-features --features "${{ matrix.feature }}" test: name: Test @@ -153,4 +153,4 @@ jobs: - uses: actions-rs/cargo@v1 with: command: test - args: --features "no_std" + args: --no-default-features --features "binary, json, ron, toml" diff --git a/Cargo.toml b/Cargo.toml index ff100ce..98e79c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,12 +22,12 @@ edition = "2018" repository = "https://github.com/not-fl3/nanoserde" [features] -default = ["json", "binary", "ron", "toml"] +default = ["json", "binary", "ron", "toml", "std"] json = ["dep:nanoserde-derive", "nanoserde-derive/json"] binary = ["dep:nanoserde-derive", "nanoserde-derive/binary"] ron = ["dep:nanoserde-derive", "nanoserde-derive/ron"] toml = [] -no_std = [] +std = [] [dependencies] nanoserde-derive = { path = "derive", version = "=0.2.0", optional = true } diff --git a/derive/Cargo.toml b/derive/Cargo.toml index ceff924..ea32cb6 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -14,4 +14,3 @@ default = [] json = [] binary = [] ron = [] -no_std = [] diff --git a/derive/src/lib.rs b/derive/src/lib.rs index c8a5e79..6863d71 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -1,4 +1,4 @@ -#![cfg_attr(feature = "no_std", no_std)] +#![cfg(no_std)] extern crate alloc; extern crate proc_macro; diff --git a/derive/src/parse.rs b/derive/src/parse.rs index 1122733..2c08164 100644 --- a/derive/src/parse.rs +++ b/derive/src/parse.rs @@ -4,10 +4,12 @@ //! https://docs.rs/syn/0.15.44/syn/enum.Type.html //! https://ziglang.org/documentation/0.5.0/#toc-typeInfo +use alloc::collections::BTreeSet; use core::iter::Peekable; -use std::collections::HashSet; -use std::num::IntErrorKind; +use core::num::IntErrorKind; +use alloc::borrow::ToOwned; +use alloc::boxed::Box; use alloc::string::{String, ToString}; use alloc::vec::Vec; use alloc::{format, vec}; @@ -16,6 +18,7 @@ use proc_macro::{Delimiter, Group, TokenStream, TokenTree}; #[derive(Debug, Clone)] pub struct Attribute { + #[allow(unused)] pub name: String, pub tokens: Vec, } @@ -37,6 +40,7 @@ pub struct Lifetime { #[derive(Debug, Clone)] pub struct Field { pub attributes: Vec, + #[allow(unused)] pub vis: Visibility, pub field_name: Option, pub ty: Type, @@ -1079,14 +1083,14 @@ fn next_attribute>( (true, _) => { attrs.push(Attribute { name: name.clone(), - tokens: std::mem::take(&mut attr_tokens), + tokens: core::mem::take(&mut attr_tokens), }); break; } (false, Some(",")) => { attrs.push(Attribute { name: name.clone(), - tokens: std::mem::take(&mut attr_tokens), + tokens: core::mem::take(&mut attr_tokens), }); continue; } @@ -1105,14 +1109,14 @@ fn next_attribute>( (true, _) => { attrs.push(Attribute { name: name.clone(), - tokens: std::mem::take(&mut attr_tokens), + tokens: core::mem::take(&mut attr_tokens), }); break; } (false, true) => { attrs.push(Attribute { name: name.clone(), - tokens: std::mem::take(&mut attr_tokens), + tokens: core::mem::take(&mut attr_tokens), }); } _ => {} @@ -1402,7 +1406,7 @@ fn next_generic + Clone>( fn get_all_bounds + Clone>(source: &mut Peekable) -> Vec { let mut ret = Vec::new(); - let mut already = HashSet::new(); + let mut already = BTreeSet::new(); if source.peek().map_or(false, |x| x.to_string() == "<") { source.next(); } else { diff --git a/derive/src/shared.rs b/derive/src/shared.rs index 110f814..735bf75 100644 --- a/derive/src/shared.rs +++ b/derive/src/shared.rs @@ -1,4 +1,8 @@ -use alloc::string::String; +use alloc::{ + format, + string::{String, ToString}, + vec::Vec, +}; #[cfg(any(feature = "binary", feature = "json"))] use crate::parse::{Enum, Struct}; diff --git a/src/lib.rs b/src/lib.rs index 8384509..98c2778 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,11 +15,7 @@ //! `nanoserde` supports some serialization customisation with `#[nserde()]` attributes. //! For `#[nserde(..)]` supported attributes for each format check [Features support matrix](https://github.com/not-fl3/nanoserde#features-support-matrix) -#![cfg_attr(feature = "no_std", no_std)] -// Possibly stable in 1.65. -// See: https://github.com/rust-lang/rust/pull/99917 -// and https://github.com/rust-lang/rust/issues/103765 -#![cfg_attr(feature = "no_std", feature(error_in_core))] +#![cfg_attr(not(feature = "std"), no_std)] extern crate alloc; diff --git a/src/serde_bin.rs b/src/serde_bin.rs index 66c7fc1..c99e858 100644 --- a/src/serde_bin.rs +++ b/src/serde_bin.rs @@ -77,12 +77,8 @@ impl core::fmt::Display for DeBinErr { } } -#[cfg(feature = "no_std")] impl core::error::Error for DeBinErr {} -#[cfg(not(feature = "no_std"))] -impl std::error::Error for DeBinErr {} - macro_rules! impl_ser_de_bin_for { ($ty:ident) => { impl SerBin for $ty { @@ -287,7 +283,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl SerBin for std::collections::HashSet where T: SerBin, @@ -301,7 +297,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl DeBin for std::collections::HashSet where T: DeBin + core::hash::Hash + Eq, @@ -529,7 +525,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl SerBin for std::collections::HashMap where K: SerBin, @@ -545,7 +541,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl DeBin for std::collections::HashMap where K: DeBin + core::cmp::Eq + core::hash::Hash, diff --git a/src/serde_json.rs b/src/serde_json.rs index 47ceef0..a10c63e 100644 --- a/src/serde_json.rs +++ b/src/serde_json.rs @@ -160,12 +160,8 @@ impl core::fmt::Display for DeJsonErr { } } -#[cfg(feature = "no_std")] impl core::error::Error for DeJsonErr {} -#[cfg(not(feature = "no_std"))] -impl std::error::Error for DeJsonErr {} - impl DeJsonState { pub fn next(&mut self, i: &mut Chars) { if let Some(c) = i.next() { @@ -878,7 +874,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl SerJson for std::collections::HashSet where T: SerJson, @@ -899,7 +895,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl DeJson for std::collections::HashSet where T: DeJson + core::hash::Hash + Eq, @@ -1172,7 +1168,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl SerJson for std::collections::HashMap where K: SerJson, @@ -1197,7 +1193,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl DeJson for std::collections::HashMap where K: DeJson + Eq + core::hash::Hash, diff --git a/src/serde_ron.rs b/src/serde_ron.rs index ce8b954..f9b8677 100644 --- a/src/serde_ron.rs +++ b/src/serde_ron.rs @@ -157,12 +157,8 @@ impl core::fmt::Display for DeRonErr { } } -#[cfg(feature = "no_std")] impl core::error::Error for DeRonErr {} -#[cfg(not(feature = "no_std"))] -impl std::error::Error for DeRonErr {} - impl DeRonState { pub fn next(&mut self, i: &mut Chars) { if let Some(c) = i.next() { @@ -876,7 +872,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl SerRon for std::collections::HashSet where T: SerRon, @@ -897,7 +893,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl DeRon for std::collections::HashSet where T: DeRon + core::hash::Hash + Eq, @@ -1184,7 +1180,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl SerRon for std::collections::HashMap where K: SerRon, @@ -1204,7 +1200,7 @@ where } } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] impl DeRon for std::collections::HashMap where K: DeRon + Eq + core::hash::Hash, diff --git a/src/toml.rs b/src/toml.rs index 3073e02..7d1fad6 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -249,12 +249,8 @@ impl Out { } } -#[cfg(feature = "no_std")] impl core::error::Error for TomlErr {} -#[cfg(not(feature = "no_std"))] -impl std::error::Error for TomlErr {} - impl TomlParser { /// Parse a TOML string. pub fn parse(data: &str) -> Result, TomlErr> { diff --git a/tests/json.rs b/tests/json.rs index 5aa14d1..33a3228 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -6,7 +6,7 @@ use std::{ sync::atomic::AtomicBool, }; -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] use std::collections::HashMap; #[test] @@ -69,6 +69,7 @@ fn de_multiline_comment() { fn de_illegal_inline_comment() { #[derive(DeJson)] pub struct Test { + #[allow(unused)] pub a: f32, } @@ -92,6 +93,7 @@ fn de_illegal_inline_comment() { fn de_illegal_multiline_comment() { #[derive(DeJson)] pub struct Test { + #[allow(unused)] pub a: f32, } @@ -467,7 +469,7 @@ fn path_type() { assert_eq!(bar.d, Some(vec![vec![1, 2], vec![3, 4]])); } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] #[test] fn hashmaps() { #[derive(DeJson)] diff --git a/tests/ron.rs b/tests/ron.rs index df8395e..5b7679e 100644 --- a/tests/ron.rs +++ b/tests/ron.rs @@ -6,7 +6,7 @@ use std::{ sync::atomic::AtomicBool, }; -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] use std::collections::HashMap; #[test] @@ -263,7 +263,7 @@ fn path_type() { assert_eq!(bar.d, Some(vec![vec![1, 2], vec![3, 4]])); } -#[cfg(not(feature = "no_std"))] +#[cfg(feature = "std")] #[test] fn hashmaps() { #[derive(DeRon)]