Add lit impls

Also fixed 32-bit word store
next
Sayan Nandan 2 years ago
parent 0410a019cf
commit 66dab00eb3
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -44,6 +44,15 @@ pub struct Lit<'a> {
_lt: PhantomData<&'a [u8]>,
}
impl<'a> Lit<'a> {
fn as_ir(&'a self) -> LitIR<'a> {
unsafe {
// UNSAFE(@ohsayan): 'tis the lifetime. 'tis the savior
mem::transmute_copy(self)
}
}
}
impl<'a> DataspecMeta1D for Lit<'a> {
type Target = NativeDword;
type StringItem = Box<str>;
@ -138,6 +147,25 @@ impl<'a> Clone for Lit<'a> {
}
}
impl<'a> ToString for Lit<'a> {
fn to_string(&self) -> String {
<Self as DataspecMethods1D>::to_string_debug(self)
}
}
enum_impls! {
Lit<'a> => {
bool as Bool,
u64 as UnsignedInt,
i64 as SignedInt,
f64 as Float,
&'a str as Str,
String as Str,
Box<str> as Str,
&'a [u8] as Bin,
}
}
/*
LitIR
*/
@ -204,6 +232,12 @@ unsafe impl<'a> Dataspec1D for LitIR<'a> {
}
}
impl<'a> ToString for LitIR<'a> {
fn to_string(&self) -> String {
<Self as DataspecMethods1D>::to_string_debug(self)
}
}
/*
UNSAFE(@ohsayan): Safety:
- No touches
@ -237,6 +271,17 @@ impl<'a> Clone for LitIR<'a> {
}
}
enum_impls! {
LitIR<'a> => {
bool as Bool,
u64 as UnsignedInt,
i64 as SignedInt,
f64 as Float,
&'a str as Str,
&'a [u8] as Bin,
}
}
#[test]
fn tlit() {
let str1 = Lit::Str("hello".into());

@ -25,7 +25,7 @@
*/
/*
So, I woke up and chose violence. God bless me and the stack memory. What I've done here is a sin. Do not follow my footsteps here if you want to write maintainable code.
So, I woke up and chose violence. God bless me and the stack memory. What I've done here is a sin. Do not follow my footsteps here if you want to write safe and maintainable code.
-- @ohsayan
*/
@ -48,7 +48,6 @@ pub enum Dataflag {
Float,
Bin,
Str,
List,
}
/// Information about the type that implements the dataspec traits
@ -296,12 +295,17 @@ pub unsafe trait DataspecMethods1D: Dataspec1D {
Dataflag::Float => self.read_float_uck(),
Dataflag::Bin => self.read_bin_uck(),
Dataflag::Str => self.read_str_uck(),
#[allow(unused_variables)]
#[allow(unreachable_code)]
Dataflag::List => {
panic!("found 2D in 1D data")
},
)
};
}
fn to_string_debug(&self) -> String {
match self.kind() {
Dataflag::Bool => self.bool().to_string(),
Dataflag::UnsignedInt => self.uint().to_string(),
Dataflag::SignedInt => self.sint().to_string(),
Dataflag::Float => self.float().to_string(),
Dataflag::Bin => format!("{:?}", self.bin()),
Dataflag::Str => format!("{:?}", self.str()),
}
}
}

@ -96,7 +96,7 @@ impl SystemDword for NativeTword {
let x;
#[cfg(target_pointer_width = "32")]
{
let [a, b] = unsafe { core::mem::transmute(u) };
let [a, b]: [usize; 2] = unsafe { core::mem::transmute(u) };
x = [a, b, 0];
}
#[cfg(target_pointer_width = "64")]

Loading…
Cancel
Save