inspect-core 0.1.0

Core types and traits for the inspect-rs introspection system
Documentation
//! Inspect implementations for primitive types.

use crate::{Inspect, InspectCx, Kind, TypeInfo, ValueRef};

// Unit type
impl Inspect for () {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::Unit, TypeInfo::new("()"))
    }
}

// Boolean
impl Inspect for bool {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::Bool(*self), TypeInfo::new("bool"))
    }
}

// Character
impl Inspect for char {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::Char(*self), TypeInfo::new("char"))
    }
}

// Signed integers
impl Inspect for i8 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::I8(*self), TypeInfo::new("i8"))
    }
}

impl Inspect for i16 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::I16(*self), TypeInfo::new("i16"))
    }
}

impl Inspect for i32 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::I32(*self), TypeInfo::new("i32"))
    }
}

impl Inspect for i64 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::I64(*self), TypeInfo::new("i64"))
    }
}

impl Inspect for i128 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::I128(*self), TypeInfo::new("i128"))
    }
}

impl Inspect for isize {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::Isize(*self), TypeInfo::new("isize"))
    }
}

// Unsigned integers
impl Inspect for u8 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::U8(*self), TypeInfo::new("u8"))
    }
}

impl Inspect for u16 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::U16(*self), TypeInfo::new("u16"))
    }
}

impl Inspect for u32 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::U32(*self), TypeInfo::new("u32"))
    }
}

impl Inspect for u64 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::U64(*self), TypeInfo::new("u64"))
    }
}

impl Inspect for u128 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::U128(*self), TypeInfo::new("u128"))
    }
}

impl Inspect for usize {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::Usize(*self), TypeInfo::new("usize"))
    }
}

// Floating point
impl Inspect for f32 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::F32(*self), TypeInfo::new("f32"))
    }
}

impl Inspect for f64 {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::F64(*self), TypeInfo::new("f64"))
    }
}

// String types
impl Inspect for str {
    fn inspect(&self, cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        let truncated = if self.len() > cx.limits().max_string_length {
            &self[..cx.limits().max_string_length]
        } else {
            self
        };
        ValueRef::with_type(Kind::Str(truncated.into()), TypeInfo::new("str"))
    }
}

#[cfg(feature = "std")]
impl Inspect for String {
    fn inspect(&self, cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        self.as_str().inspect(cx)
    }
}

#[cfg(not(feature = "std"))]
impl Inspect for alloc::string::String {
    fn inspect(&self, cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        self.as_str().inspect(cx)
    }
}

// Byte slices - special case for u8 slices only
impl Inspect for &[u8] {
    fn inspect(&self, cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        let truncated =
            if self.len() > cx.limits().max_bytes { &self[..cx.limits().max_bytes] } else { self };
        ValueRef::with_type(Kind::Bytes(truncated), TypeInfo::new("[u8]"))
    }
}