gc_value 0.1.0

Generic value type, capturing the data model of common formats such as JSON, YAML and CBOR, based on Arc for cheap cloning.
Documentation
//! I provide [`Eq`], [`Ord`] and [`Hash`] impl for [`GcValue`]

use std::{cmp::Ordering, hash::Hash, sync::Arc};

use crate::Map;

use super::GcValue;

impl Eq for GcValue {}

impl Hash for GcValue {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        core::mem::discriminant(self).hash(state);
        match self {
            GcValue::Text(i) => i.hash(state),
            GcValue::Int(i) => i.hash(state),
            GcValue::Float(i) => i.to_bits().hash(state),
            GcValue::Boolean(i) => i.hash(state),
            GcValue::Array(i) => i.hash(state),
            GcValue::Map(i) => i.hash(state),
            GcValue::Tag(i) => i.hash(state),
            GcValue::Bytes(i) => i.hash(state),
            GcValue::Null => (),
        }
    }
}

impl Ord for GcValue {
    fn cmp(&self, other: &Self) -> Ordering {
        match (self, other) {
            // deal with identical variants
            (Self::Text(l0), Self::Text(r0)) => l0.cmp(r0),
            (Self::Int(l0), Self::Int(r0)) => l0.cmp(r0),
            (Self::Float(l0), Self::Float(r0)) => l0.to_bits().cmp(&r0.to_bits()),
            (Self::Boolean(l0), Self::Boolean(r0)) => l0.cmp(r0),
            (Self::Array(l0), Self::Array(r0)) => l0.cmp(r0),
            (Self::Map(l0), Self::Map(r0)) => l0.cmp(r0),
            (Self::Tag(l0), Self::Tag(r0)) => l0.cmp(r0),
            (Self::Bytes(l0), Self::Bytes(r0)) => l0.cmp(r0),
            (Self::Null, Self::Null) => Ordering::Equal,
            // deal with different variants
            _ => discriminant(self).cmp(&discriminant(other)),
        }
    }
}

impl PartialEq for GcValue {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Text(l0), Self::Text(r0)) => l0 == r0,
            (Self::Int(l0), Self::Int(r0)) => l0 == r0,
            (Self::Float(l0), Self::Float(r0)) => l0.to_bits() == r0.to_bits(),
            (Self::Boolean(l0), Self::Boolean(r0)) => l0 == r0,
            (Self::Array(l0), Self::Array(r0)) => l0 == r0,
            (Self::Map(l0), Self::Map(r0)) => l0 == r0,
            (Self::Tag(l0), Self::Tag(r0)) => l0 == r0,
            (Self::Bytes(l0), Self::Bytes(r0)) => l0 == r0,
            (Self::Null, Self::Null) => true,
            _ => false,
        }
    }
}

impl PartialOrd for GcValue {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

fn discriminant(v: &GcValue) -> u8 {
    match v {
        GcValue::Text(_) => 0,
        GcValue::Int(_) => 1,
        GcValue::Float(_) => 2,
        GcValue::Boolean(_) => 3,
        GcValue::Array(_) => 4,
        GcValue::Map(_) => 5,
        GcValue::Tag(_) => 6,
        GcValue::Bytes(_) => 7,
        GcValue::Null => 8,
    }
}

// Comparison to other types

impl PartialEq<str> for GcValue {
    fn eq(&self, other: &str) -> bool {
        if let GcValue::Text(txt) = self {
            txt.as_ref() == other
        } else {
            false
        }
    }
}

impl PartialEq<&str> for GcValue {
    fn eq(&self, other: &&str) -> bool {
        if let GcValue::Text(txt) = self {
            txt.as_ref() == *other
        } else {
            false
        }
    }
}

impl PartialEq<Arc<str>> for GcValue {
    fn eq(&self, other: &Arc<str>) -> bool {
        if let GcValue::Text(txt) = self {
            txt == other
        } else {
            false
        }
    }
}

impl PartialEq<i128> for GcValue {
    fn eq(&self, other: &i128) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other
        } else {
            false
        }
    }
}

impl PartialEq<i8> for GcValue {
    fn eq(&self, other: &i8) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<i16> for GcValue {
    fn eq(&self, other: &i16) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<i32> for GcValue {
    fn eq(&self, other: &i32) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<i64> for GcValue {
    fn eq(&self, other: &i64) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<isize> for GcValue {
    fn eq(&self, other: &isize) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<u8> for GcValue {
    fn eq(&self, other: &u8) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<u16> for GcValue {
    fn eq(&self, other: &u16) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<u32> for GcValue {
    fn eq(&self, other: &u32) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<u64> for GcValue {
    fn eq(&self, other: &u64) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<usize> for GcValue {
    fn eq(&self, other: &usize) -> bool {
        if let GcValue::Int(n) = self {
            *n == *other as i128
        } else {
            false
        }
    }
}

impl PartialEq<f64> for GcValue {
    fn eq(&self, other: &f64) -> bool {
        if let GcValue::Float(n) = self {
            n.to_bits() == other.to_bits()
        } else {
            false
        }
    }
}

impl PartialEq<f32> for GcValue {
    fn eq(&self, other: &f32) -> bool {
        if let GcValue::Float(n) = self {
            n.to_bits() == (*other as f64).to_bits()
        } else {
            false
        }
    }
}

impl PartialEq<bool> for GcValue {
    fn eq(&self, other: &bool) -> bool {
        if let GcValue::Boolean(b) = self {
            b == other
        } else {
            false
        }
    }
}

impl<T> PartialEq<[T]> for GcValue
where
    GcValue: PartialEq<T>,
{
    fn eq(&self, other: &[T]) -> bool {
        if let GcValue::Array(a) = self {
            a.iter().zip(other.iter()).all(|(i, j)| i == j)
        } else {
            false
        }
    }
}

impl PartialEq<Map> for GcValue {
    fn eq(&self, other: &Map) -> bool {
        if let GcValue::Map(m) = self {
            m.as_ref() == other
        } else {
            false
        }
    }
}

impl<T, U> PartialEq<(T, U)> for GcValue
where
    GcValue: PartialEq<T> + PartialEq<U>,
{
    fn eq(&self, other: &(T, U)) -> bool {
        if let GcValue::Tag(pair) = self {
            let (l0, l1) = pair.as_ref();
            let (r0, r1) = other;
            l0 == r0 && l1 == r1
        } else {
            false
        }
    }
}