pub enum Hex {
Vector(Vec<u8>),
Bytes([u8; 24], usize),
}
Expand description
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(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(65534);
assert_eq!(65534, d.to_i64().unwrap());
Variants§
Implementations§
source§impl Hex
impl Hex
sourcepub fn empty() -> Self
pub fn empty() -> Self
Make an empty Hex
.
For example:
use sodg::Hex;
let d = Hex::empty();
assert!(d.is_empty());
assert_eq!("--", d.print());
sourcepub fn bytes(&self) -> &[u8] ⓘ
pub fn bytes(&self) -> &[u8] ⓘ
Take the bytes contained.
For example:
use sodg::Hex;
let d = Hex::from(2);
assert_eq!(8, d.len())
sourcepub fn from_slice(slice: &[u8]) -> Self
pub fn from_slice(slice: &[u8]) -> Self
Create a new Hex
from slice, in appropriate mode.
For example:
use sodg::Hex;
let d = Hex::from_slice(&[0xDE, 0xAD]);
assert_eq!("DE-AD", d.print());
let v = Hex::from_slice(&vec![0xBE, 0xEF]);
assert_eq!("BE-EF", v.print());
sourcepub fn from_vec(bytes: Vec<u8>) -> Self
pub fn from_vec(bytes: Vec<u8>) -> Self
Create a new Hex
from Vec<u8>
.
For example:
use sodg::Hex;
let d = Hex::from_vec(vec![0xCA, 0xFE]);
assert_eq!("CA-FE", d.print());
sourcepub fn from_string_bytes(d: String) -> Self
pub fn from_string_bytes(d: String) -> Self
Create a new Hex
from String
.
For example:
use sodg::Hex;
let d = Hex::from_string_bytes("Ура!".to_string());
assert_eq!("D0-A3-D1-80-D0-B0-21", d.print());
sourcepub fn from_str_bytes(d: &str) -> Self
pub fn from_str_bytes(d: &str) -> Self
Create a new Hex
from the bytes composing &str
.
For example:
use sodg::Hex;
let d = Hex::from_str_bytes("Ура!");
assert_eq!("D0-A3-D1-80-D0-B0-21", d.print());
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Is it empty and has no data (not a single byte)?
For example:
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
.
For example:
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
.
For example:
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
.
For example:
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
in UTF-8 encoding.
For example:
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.
For example:
use sodg::Hex;
let d = Hex::from_vec([0xCA, 0xFE].to_vec());
assert_eq!("CA-FE", d.print());
A string of one letter will be printed as xx
, without the trailing dash:
use sodg::Hex;
let d = Hex::from_vec([0xCA].to_vec());
assert_eq!("CA", d.print());
An empty string will be printed as --
:
use sodg::Hex;
let d = Hex::empty();
assert_eq!("--", d.print());
sourcepub fn byte_at(&self, pos: usize) -> u8
pub fn byte_at(&self, pos: usize) -> u8
Take one byte.
For example:
use sodg::Hex;
let d = Hex::from_str_bytes("你好");
assert_eq!("E4-BD-A0-E5-A5-BD", d.print());
assert_eq!(0xA0, d.byte_at(2));
Trait Implementations§
source§impl<'de> Deserialize<'de> for Hex
impl<'de> Deserialize<'de> for Hex
source§fn 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>,
source§impl FromStr for Hex
impl FromStr for Hex
source§fn from_str(hex: &str) -> Result<Self, Self::Err>
fn from_str(hex: &str) -> Result<Self, Self::Err>
Create a Hex
from a &str
containing a hexadecimal representation of data.
For example, this is how you make a new Hex
from "DE-AD-BE-EF-20-22"
string:
use sodg::Hex;
use std::str::FromStr;
let hex = "DE-AD-BE-EF-20-22";
let d: Hex = hex.parse().unwrap();
let d2 = Hex::from_str(hex).unwrap();
assert_eq!("DE-AD-BE-EF-20-22", d.print());
assert_eq!("DE-AD-BE-EF-20-22", d2.print());
An empty Hex
may be created either from an empty string
or "--"
:
use sodg::Hex;
use std::str::FromStr;
let d1: Hex = Hex::from_str("--").unwrap();
let d2: Hex = Hex::from_str("").unwrap();
assert_eq!(Hex::empty(), d1);
assert_eq!(Hex::empty(), d2);