Skip to content

Commit

Permalink
bounded-collections: fix build for no_std + serde (#789)
Browse files Browse the repository at this point in the history
* bounded-collections: fix build for no_std + serde

* put serde test under feature

* try something

* try something else

* try without
  • Loading branch information
ordian authored Sep 17, 2023
1 parent a8a85a4 commit 7b41a04
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
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"),
}
}
}
}

0 comments on commit 7b41a04

Please sign in to comment.