Move u64/u128 to IndirectAtom as well

This commit is contained in:
Sigilante 2023-11-15 15:41:22 -06:00 committed by GitHub
parent 7debba4ad7
commit 1ae6303cbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -464,6 +464,24 @@ impl IndirectAtom {
UBig::from_le_bytes_stack(stack, self.as_bytes())
}
pub unsafe fn as_u64(self) -> Result<u64> {
if self.size() == 1 {
Ok(*(self.data_pointer()))
} else {
Err(Error::NotRepresentable)
}
}
pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> {
if self.size() <= 2 {
let mut u128_array = &mut [0u64; 2];
u128_array.copy_from_slice(&(self.as_slice()[0..2]));
Ok(unsafe { *u128_array })
} else {
Err(Error::NotRepresentable)
}
}
/** Ensure that the size does not contain any trailing 0 words */
pub unsafe fn normalize(&mut self) -> &Self {
let mut index = self.size() - 1;
@ -826,24 +844,6 @@ impl Atom {
}
}
pub unsafe fn as_u64(self) -> Result<u64> {
if self.size() == 1 {
Ok(*(self.data_pointer()))
} else {
Err(Error::NotRepresentable)
}
}
pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> {
if self.size() <= 2 {
let mut u128_array = &mut [0u64; 2];
u128_array.copy_from_slice(&(self.as_slice()[0..2]));
Ok(unsafe { *u128_array })
} else {
Err(Error::NotRepresentable)
}
}
pub fn as_noun(self) -> Noun {
Noun { atom: self }
}