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

bounded-collections: fix build for no_std + serde #789

Merged
merged 5 commits into from
Sep 17, 2023
Merged
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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
uses: Swatinem/rust-cache@e207df5d269b42b69c8bc5101da26f7d31feddb4 # v2.6.2

- run: rustup target add wasm32-unknown-unknown
- run: rustup target add mips64-unknown-linux-muslabi64

- name: Test no-default-features
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -122,7 +123,7 @@ jobs:
with:
use-cross: true
command: test
args: -p uint --target=mips64-unknown-linux-gnuabi64
args: -p uint --target=mips64-unknown-linux-muslabi64

test_windows:
name: Test Windows
Expand Down
86 changes: 47 additions & 39 deletions bounded-collections/src/bounded_btree_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
D: Deserializer<'de>,
{
// Create a visitor to visit each element in the sequence
struct BTreeSetVisitor<T, S>(std::marker::PhantomData<(T, S)>);
struct BTreeSetVisitor<T, S>(PhantomData<(T, S)>);

impl<'de, T, S> Visitor<'de> for BTreeSetVisitor<T, S>
where
Expand All @@ -59,7 +59,7 @@ where
{
type Value = BTreeSet<T>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("a sequence")
}

Expand Down Expand Up @@ -396,7 +396,7 @@ where
}
}

#[cfg(all(test, feature = "std"))]
#[cfg(test)]
mod test {
use super::*;
use crate::ConstU32;
Expand Down Expand Up @@ -586,48 +586,56 @@ mod test {
}
}

#[test]
fn test_serializer() {
let mut c = BoundedBTreeSet::<u32, ConstU32<6>>::new();
c.try_insert(0).unwrap();
c.try_insert(1).unwrap();
c.try_insert(2).unwrap();

assert_eq!(serde_json::json!(&c).to_string(), r#"[0,1,2]"#);
}
#[cfg(feature = "serde")]
mod serde {
use super::*;
use crate::alloc::string::ToString as _;

#[test]
fn test_deserializer() {
let c: Result<BoundedBTreeSet<u32, ConstU32<6>>, serde_json::error::Error> = serde_json::from_str(r#"[0,1,2]"#);
assert!(c.is_ok());
let c = c.unwrap();
#[test]
fn test_serializer() {
let mut c = BoundedBTreeSet::<u32, ConstU32<6>>::new();
c.try_insert(0).unwrap();
c.try_insert(1).unwrap();
c.try_insert(2).unwrap();

assert_eq!(c.len(), 3);
assert!(c.contains(&0));
assert!(c.contains(&1));
assert!(c.contains(&2));
}
assert_eq!(serde_json::json!(&c).to_string(), r#"[0,1,2]"#);
}

#[test]
fn test_deserializer_bound() {
let c: Result<BoundedBTreeSet<u32, ConstU32<3>>, serde_json::error::Error> = serde_json::from_str(r#"[0,1,2]"#);
assert!(c.is_ok());
let c = c.unwrap();
#[test]
fn test_deserializer() {
let c: Result<BoundedBTreeSet<u32, ConstU32<6>>, serde_json::error::Error> =
serde_json::from_str(r#"[0,1,2]"#);
assert!(c.is_ok());
let c = c.unwrap();

assert_eq!(c.len(), 3);
assert!(c.contains(&0));
assert!(c.contains(&1));
assert!(c.contains(&2));
}

assert_eq!(c.len(), 3);
assert!(c.contains(&0));
assert!(c.contains(&1));
assert!(c.contains(&2));
}
#[test]
fn test_deserializer_bound() {
let c: Result<BoundedBTreeSet<u32, ConstU32<3>>, serde_json::error::Error> =
serde_json::from_str(r#"[0,1,2]"#);
assert!(c.is_ok());
let c = c.unwrap();

assert_eq!(c.len(), 3);
assert!(c.contains(&0));
assert!(c.contains(&1));
assert!(c.contains(&2));
}

#[test]
fn test_deserializer_failed() {
let c: Result<BoundedBTreeSet<u32, ConstU32<4>>, serde_json::error::Error> =
serde_json::from_str(r#"[0,1,2,3,4]"#);
#[test]
fn test_deserializer_failed() {
let c: Result<BoundedBTreeSet<u32, ConstU32<4>>, serde_json::error::Error> =
serde_json::from_str(r#"[0,1,2,3,4]"#);

match c {
Err(msg) => assert_eq!(msg.to_string(), "out of bounds at line 1 column 11"),
_ => unreachable!("deserializer must raise error"),
match c {
Err(msg) => assert_eq!(msg.to_string(), "out of bounds at line 1 column 11"),
_ => unreachable!("deserializer must raise error"),
}
}
}
}
Loading