Module serde_with::rust::sets_last_value_wins

source ·
Expand description

Ensure that the last value is taken, if duplicate values exist

By default serde has a first-value-wins implementation, if duplicate keys for a set exist. Sometimes the opposite strategy is desired. This helper implements a first-value-wins strategy.

The implementation supports both the HashSet and the BTreeSet from the standard library.

§Converting to serde_as

The same functionality can be more clearly expressed using the serde_as macro and SetLastValueWins. The _ is a placeholder which works for any type which implements Serialize/Deserialize.

#[serde_as]
#[derive(Deserialize, Serialize)]
struct A {
    #[serde_as(as = "SetLastValueWins<_, _>")]
    s: HashSet<usize>,
}

Functions§

  • Deserialize a set and keep the last of equal values
  • Serialize the set with the default serializer