pub struct Atom { /* private fields */ }Expand description
An arbitrarily large unsigned integer.
An atom is an arbitrarily large unsigned integer represented as a little-endian contiguous sequence of bytes. An atom can be:
- created a single bit at a time or from other types that can be easily converted into atoms like primitive unsigned integers, strings, and string slices;
- iterated over a single bit at a time;
- compared to other atoms and other atom-like types;
- pretty-printed as a hexadecimal number;
- converted into a noun, a primitive unsigned integer type, or a string slice.
§Examples
To create a new atom, use one of the From<T> implementations:
let atom = Atom::from("hello");
assert_eq!(atom, "hello");let atom = Atom::from(0u8);
assert_eq!(atom, 0u8);Implementations§
Source§impl Atom
impl Atom
Sourcepub fn builder() -> Builder
pub fn builder() -> Builder
Creates an empty atom builder.
This method is equivalent to Builder::new().
Sourcepub fn as_str(&self) -> Result<&str, Utf8Error>
pub fn as_str(&self) -> Result<&str, Utf8Error>
Converts this atom into a string slice, returning an error if the atom is not composed of valid UTF-8 bytes.
Sourcepub fn as_u8(&self) -> Option<u8>
pub fn as_u8(&self) -> Option<u8>
Converts this atom into an 8-bit unsigned integer, returning None if the atom is greater
than u8::MAX.
§Examples
let uint = u8::MAX;
let atom = Atom::from(uint);
assert_eq!(atom.as_u8().unwrap(), uint);Sourcepub fn as_u16(&self) -> Option<u16>
pub fn as_u16(&self) -> Option<u16>
Converts this atom into an 16-bit unsigned integer, returning None if the atom is greater
than u16::MAX.
§Examples
let uint = u16::MAX;
let atom = Atom::from(uint);
assert_eq!(atom.as_u16().unwrap(), uint);Sourcepub fn as_u32(&self) -> Option<u32>
pub fn as_u32(&self) -> Option<u32>
Converts this atom into an 32-bit unsigned integer, returning None if the atom is greater
than u32::MAX.
§Examples
let uint = u32::MAX;
let atom = Atom::from(uint);
assert_eq!(atom.as_u32().unwrap(), uint);Sourcepub fn as_u64(&self) -> Option<u64>
pub fn as_u64(&self) -> Option<u64>
Converts this atom into an 64-bit unsigned integer, returning None if the atom is greater
than u64::MAX.
§Examples
let uint = u64::MAX;
let atom = Atom::from(uint);
assert_eq!(atom.as_u64().unwrap(), uint);Sourcepub fn as_u128(&self) -> Option<u128>
pub fn as_u128(&self) -> Option<u128>
Converts this atom into an 128-bit unsigned integer, returning None if the atom is greater
than u128::MAX.
§Examples
let uint = u128::MAX;
let atom = Atom::from(uint);
assert_eq!(atom.as_u128().unwrap(), uint);Sourcepub fn as_usize(&self) -> Option<usize>
pub fn as_usize(&self) -> Option<usize>
Converts this atom into a pointer-sized unsigned integer, returning None if the atom is
greater than usize::MAX.
§Examples
let uint = usize::MAX;
let atom = Atom::from(uint);
assert_eq!(atom.as_usize().unwrap(), uint);Sourcepub fn into_vec(self) -> Vec<u8> ⓘ
pub fn into_vec(self) -> Vec<u8> ⓘ
Converts this atom into a byte vector, consuming the atom.
This method does not allocate on the heap.
pub fn as_noun(&self) -> Noun
pub fn into_noun(self) -> Noun
Sourcepub fn format_aura(&self, aura: Aura) -> Result<String, FormatError>
pub fn format_aura(&self, aura: Aura) -> Result<String, FormatError>
Formats an atom into a string using the given aura
§Examples
let atom = Atom::from(123434910u64);
let s = atom.format_aura(Aura::U).unwrap();
assert_eq!(s, "123.434.910");Sourcepub fn parse_aura(aura: Aura, s: &str) -> Result<Self, ParseError>
pub fn parse_aura(aura: Aura, s: &str) -> Result<Self, ParseError>
Parses a string into an atom using the given aura
§Examples
let atom = Atom::parse_aura(Aura::U, "123.434.910").unwrap();
assert_eq!(atom, Atom::from(123434910u64));