From a960dfde6c79bf504ff88ae041abaf2b6b4bd9ba Mon Sep 17 00:00:00 2001 From: Mark <> Date: Mon, 15 Apr 2024 16:43:45 +0200 Subject: [PATCH] add Byte type --- mers_lib/src/data/byte.rs | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 mers_lib/src/data/byte.rs diff --git a/mers_lib/src/data/byte.rs b/mers_lib/src/data/byte.rs new file mode 100644 index 0000000..5c8cc4b --- /dev/null +++ b/mers_lib/src/data/byte.rs @@ -0,0 +1,65 @@ +use std::{any::Any, fmt::Display, sync::Arc}; + +use super::{MersData, MersType, Type}; + +#[derive(Debug, Clone, Copy)] +pub struct Byte(pub u8); + +impl MersData for Byte { + fn is_eq(&self, other: &dyn MersData) -> bool { + if let Some(other) = other.as_any().downcast_ref::() { + other.0 == self.0 + } else { + false + } + } + fn clone(&self) -> Box { + Box::new(Clone::clone(self)) + } + fn as_type(&self) -> super::Type { + Type::new(ByteT) + } + fn as_any(&self) -> &dyn Any { + self + } + fn mut_any(&mut self) -> &mut dyn Any { + self + } + fn to_any(self) -> Box { + Box::new(self) + } +} + +#[derive(Debug, Clone)] +pub struct ByteT; +impl MersType for ByteT { + fn is_same_type_as(&self, other: &dyn MersType) -> bool { + other.as_any().downcast_ref::().is_some() + } + fn is_included_in_single(&self, target: &dyn MersType) -> bool { + self.is_same_type_as(target) + } + fn subtypes(&self, acc: &mut Type) { + acc.add(Arc::new(self.clone())); + } + fn as_any(&self) -> &dyn Any { + self + } + fn mut_any(&mut self) -> &mut dyn Any { + self + } + fn to_any(self) -> Box { + Box::new(self) + } +} + +impl Display for Byte { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} +impl Display for ByteT { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Byte") + } +}