feat(primitives): Preserve order of map structures (#192)

This commit is contained in:
Santiago Carmuega 2022-09-14 15:00:53 -03:00 committed by GitHub
parent 4ebeb60119
commit 4dc373335f
3 changed files with 53 additions and 28 deletions

View file

@ -37,19 +37,32 @@ impl<C, const N: usize> minicbor::Encode<C> for SkipCbor<N> {
/// canonicalization for isomorphic decoding / encoding operators, we use a Vec
/// as the underlaying struct for storage of the items (as opposed to a BTreeMap
/// or HashMap).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum KeyValuePairs<K, V> {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[serde(from = "Vec::<(K, V)>", into = "Vec::<(K, V)>")]
pub enum KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
Def(Vec<(K, V)>),
Indef(Vec<(K, V)>),
}
impl<K, V> KeyValuePairs<K, V> {
impl<K, V> KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
pub fn to_vec(self) -> Vec<(K, V)> {
self.into()
}
}
impl<K, V> From<KeyValuePairs<K, V>> for Vec<(K, V)> {
impl<K, V> From<KeyValuePairs<K, V>> for Vec<(K, V)>
where
K: Clone,
V: Clone,
{
fn from(other: KeyValuePairs<K, V>) -> Self {
match other {
KeyValuePairs::Def(x) => x,
@ -58,7 +71,21 @@ impl<K, V> From<KeyValuePairs<K, V>> for Vec<(K, V)> {
}
}
impl<K, V> Deref for KeyValuePairs<K, V> {
impl<K, V> From<Vec<(K, V)>> for KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
fn from(other: Vec<(K, V)>) -> Self {
KeyValuePairs::Def(other)
}
}
impl<K, V> Deref for KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
type Target = Vec<(K, V)>;
fn deref(&self) -> &Self::Target {
@ -71,8 +98,8 @@ impl<K, V> Deref for KeyValuePairs<K, V> {
impl<'b, C, K, V> minicbor::decode::Decode<'b, C> for KeyValuePairs<K, V>
where
K: Encode<C> + Decode<'b, C>,
V: Encode<C> + Decode<'b, C>,
K: Encode<C> + Decode<'b, C> + Clone,
V: Encode<C> + Decode<'b, C> + Clone,
{
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
let datatype = d.datatype()?;
@ -92,8 +119,8 @@ where
impl<C, K, V> minicbor::encode::Encode<C> for KeyValuePairs<K, V>
where
K: Encode<C>,
V: Encode<C>,
K: Encode<C> + Clone,
V: Encode<C> + Clone,
{
fn encode<W: minicbor::encode::Write>(
&self,