pub struct Hex { /* private fields */ }
Expand description
It is an object-oriented representation of binary data in hexadecimal format, which can be put into vertices of the graph. You can create it from Rust primitives:
use sodg::Hex;
let d = Hex::from_i64(65534);
assert_eq!("00-00-00-00-00-00-FF-FE", d.print());
Then, you can turn it back to Rust primitives:
use sodg::Hex;
let d = Hex::from_i64(65534);
assert_eq!(65534, d.to_i64().unwrap());
Implementations
sourceimpl Hex
impl Hex
sourcepub fn empty() -> Self
pub fn empty() -> Self
Make an empty Hex
.
use sodg::Hex;
let d = Hex::empty();
assert!(d.is_empty());
assert_eq!("--", d.print());
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
How many bytes in there.
use sodg::Hex;
let d = Hex::empty();
assert_eq!(0, d.len());
sourcepub fn from_vec(bytes: Vec<u8>) -> Self
pub fn from_vec(bytes: Vec<u8>) -> Self
From Vec<u8>
.
use sodg::Hex;
let d = Hex::from_vec(vec![0xCA, 0xFE]);
assert_eq!("CA-FE", d.print());
sourcepub fn parse(hex: String) -> Self
pub fn parse(hex: String) -> Self
From String
as HEX, for example DE-AD-BE-EF-20-22
.
use sodg::Hex;
let hex = "DE-AD-BE-EF-20-22";
let d = Hex::parse(hex.to_string());
assert_eq!("DE-AD-BE-EF-20-22", d.print());
sourcepub fn from_i64(d: i64) -> Self
pub fn from_i64(d: i64) -> Self
Make Hex
from i64
.
use sodg::Hex;
let d = Hex::from_i64(65536);
assert_eq!("00-00-00-00-00-01-00-00", d.print());
sourcepub fn from_bool(d: bool) -> Self
pub fn from_bool(d: bool) -> Self
From bool
.
use sodg::Hex;
let d = Hex::from_bool(true);
assert_eq!("01", d.print());
sourcepub fn from_f64(d: f64) -> Self
pub fn from_f64(d: f64) -> Self
Make Hex
from f64
.
use std::f64::consts::PI;
use sodg::Hex;
let d = Hex::from_f64(PI);
assert_eq!("40-09-21-FB-54-44-2D-18", d.print());
sourcepub fn from_string(d: String) -> Self
pub fn from_string(d: String) -> Self
Make Hex
from String
.
use sodg::Hex;
let d = Hex::from_string("Ура!".to_string());
assert_eq!("D0-A3-D1-80-D0-B0-21", d.print());
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
It’s empty and no data?
use sodg::Hex;
let d = Hex::from_vec(vec![]);
assert_eq!(true, d.is_empty());
sourcepub fn to_bool(&self) -> Result<bool>
pub fn to_bool(&self) -> Result<bool>
Turn it into bool
.
use sodg::Hex;
let d = Hex::from_vec([0x01].to_vec());
assert_eq!(true, d.to_bool().unwrap());
sourcepub fn to_i64(&self) -> Result<i64>
pub fn to_i64(&self) -> Result<i64>
Turn it into i64
.
use sodg::Hex;
let d = Hex::from_vec([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2A].to_vec());
assert_eq!(42, d.to_i64().unwrap());
sourcepub fn to_f64(&self) -> Result<f64>
pub fn to_f64(&self) -> Result<f64>
Turn it into f64
.
use sodg::Hex;
let d = Hex::from_vec([0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18].to_vec());
assert_eq!(std::f64::consts::PI, d.to_f64().unwrap());
sourcepub fn to_utf8(&self) -> Result<String>
pub fn to_utf8(&self) -> Result<String>
Turn it into string
.
use sodg::Hex;
let d = Hex::from_vec([0x41, 0x42].to_vec());
assert_eq!("AB", d.to_utf8().unwrap());
sourcepub fn print(&self) -> String
pub fn print(&self) -> String
Turn it into a hexadecimal string.
use sodg::Hex;
let d = Hex::from_vec([0xCA, 0xFE].to_vec());
assert_eq!("CA-FE", d.print());
sourcepub fn to_vec(&self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn to_vec(&self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Turn it into a vector of bytes (making a clone).
sourcepub fn as_vec(&self) -> &Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn as_vec(&self) -> &Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Return a reference to the encapsulated immutable vec.
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Hex
impl<'de> Deserialize<'de> for Hex
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations
impl RefUnwindSafe for Hex
impl Send for Hex
impl Sync for Hex
impl Unpin for Hex
impl UnwindSafe for Hex
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more