From 71ab3c8ef9a20a5a93c007002795b74526416e4f Mon Sep 17 00:00:00 2001 From: Edward Amsden Date: Sun, 24 Sep 2023 00:05:45 -0500 Subject: [PATCH] noun: add as_u64() method for Atom and IndirectAtom --- rust/ares/src/noun.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index 2b646ba..a18fb0c 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -431,6 +431,14 @@ impl IndirectAtom { } } + pub unsafe fn as_u64(self) -> Result { + if self.size() == 1 { + Ok(*(self.data_pointer())) + } else { + Err(Error::NotRepresentable) + } + } + pub fn as_atom(self) -> Atom { Atom { indirect: self } } @@ -630,6 +638,16 @@ impl Atom { unsafe { is_indirect_atom(self.raw) } } + pub fn as_u64(self) -> Result { + if self.is_direct() { + Ok(unsafe { self.direct.data() }) + } else { + unsafe { + self.indirect.as_u64() + } + } + } + pub fn as_direct(&self) -> Result { if self.is_direct() { unsafe { Ok(self.direct) }