Skip to content

Commit

Permalink
Convert seq to map using Map instead of the older BTreeMap/HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb committed Mar 7, 2023
1 parent d49ca22 commit fa2fab7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ enum Foo {
// We can treat a Vec like a map with duplicates.
// JSON only allows string keys, so convert i32 to strings
// The bytes will be hex encoded
#[serde_as(as = "BTreeMap<DisplayFromStr, Hex>")]
#[serde_as(as = "Map<DisplayFromStr, Hex>")]
bytes: Vec<(i32, Vec<u8>)>,
}
}
Expand Down
5 changes: 2 additions & 3 deletions serde_with/src/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ The `serde_as` scheme is based on two new traits: [`SerializeAs`] and [`Deserial

```rust
# use serde::{Deserialize, Serialize};
# use serde_with::{serde_as, DisplayFromStr};
# use std::collections::HashMap;
# use serde_with::{serde_as, DisplayFromStr, Map};
# use std::net::Ipv4Addr;
#
#[serde_as]
Expand All @@ -34,7 +33,7 @@ struct Data {
address: Ipv4Addr,
// Treat the Vec like a map with duplicates
// Convert u32 into a String and keep the String the same type
#[serde_as(as = "HashMap<DisplayFromStr, _>")]
#[serde_as(as = "Map<DisplayFromStr, _>")]
vec_as_map: Vec<(u32, String)>,
}

Expand Down
5 changes: 2 additions & 3 deletions serde_with/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@
//! # #[cfg(all(feature = "macros", feature = "hex"))]
//! # use {
//! # serde::{Deserialize, Serialize},
//! # serde_with::{serde_as, DisplayFromStr, DurationSeconds, hex::Hex},
//! # serde_with::{serde_as, DisplayFromStr, DurationSeconds, hex::Hex, Map},
//! # std::time::Duration,
//! # std::collections::BTreeMap,
//! # };
//! # #[cfg(all(feature = "macros", feature = "hex"))]
//! #[serde_as]
Expand All @@ -228,7 +227,7 @@
//! // We can treat a Vec like a map with duplicates.
//! // JSON only allows string keys, so convert i32 to strings
//! // The bytes will be hex encoded
//! #[serde_as(as = "BTreeMap<DisplayFromStr, Hex>")]
//! #[serde_as(as = "Map<DisplayFromStr, Hex>")]
//! bytes: Vec<(i32, Vec<u8>)>,
//! }
//! }
Expand Down
5 changes: 2 additions & 3 deletions serde_with_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ fn field_has_attribute(field: &Field, namespace: &str, name: &str) -> bool {
/// # Example
///
/// ```rust,ignore
/// use serde_with::{serde_as, DisplayFromStr};
/// use std::collections::HashMap;
/// use serde_with::{serde_as, DisplayFromStr, Map};
///
/// #[serde_as]
/// #[derive(Serialize, Deserialize)]
Expand All @@ -476,7 +475,7 @@ fn field_has_attribute(field: &Field, namespace: &str, name: &str) -> bool {
/// b: u32,
///
/// /// Serialize into a map from String to String
/// #[serde_as(as = "HashMap<DisplayFromStr, _>")]
/// #[serde_as(as = "Map<DisplayFromStr, _>")]
/// c: Vec<(u32, String)>,
/// }
/// ```
Expand Down

0 comments on commit fa2fab7

Please sign in to comment.