implement add_bytes() on Atom

This commit is contained in:
Philip Quirk 2023-09-28 21:22:56 -05:00
parent 4dd009c04c
commit 73f1d3d0fb

View File

@ -186,6 +186,16 @@ impl DirectAtom {
pub fn as_bitslice_mut(&mut self) -> &mut BitSlice<u64, Lsb0> {
BitSlice::from_element_mut(&mut self.0)
}
pub fn as_bytes(&self) -> &[u8] {
let bytes: &[u8; 8] = unsafe { std::mem::transmute(self.0) };
&bytes[..]
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
let bytes: &mut [u8; 8] = unsafe { std::mem::transmute(self.0) };
&mut bytes[..]
}
}
impl fmt::Display for DirectAtom {
@ -392,6 +402,11 @@ impl IndirectAtom {
unsafe { from_raw_parts(self.data_pointer() as *const u8, self.size() << 3) }
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
unsafe { from_raw_parts_mut(self.data_pointer_mut() as *mut u8, self.size() << 3) }
}
/** BitSlice view on an indirect atom, with lifetime tied to reference to indirect atom. */
pub fn as_bitslice(&self) -> &BitSlice<u64, Lsb0> {
BitSlice::from_slice(self.as_slice())
@ -710,6 +725,22 @@ impl Atom {
pub fn as_noun(self) -> Noun {
Noun { atom: self }
}
pub fn as_bytes(&self) -> &[u8] {
if self.is_direct() {
unsafe { self.direct.as_bytes() }
} else {
unsafe { self.indirect.as_bytes() }
}
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
if self.is_direct() {
unsafe { self.direct.as_bytes_mut() }
} else {
unsafe { self.indirect.as_bytes_mut() }
}
}
}
impl fmt::Display for Atom {