mirror of
https://github.com/Dummi26/mers.git
synced 2025-03-10 14:13:52 +01:00
add Byte type
This commit is contained in:
parent
2056a89446
commit
a960dfde6c
65
mers_lib/src/data/byte.rs
Normal file
65
mers_lib/src/data/byte.rs
Normal file
@ -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::<Self>() {
|
||||||
|
other.0 == self.0
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn clone(&self) -> Box<dyn MersData> {
|
||||||
|
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<dyn Any> {
|
||||||
|
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::<Self>().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<dyn Any> {
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user