Enum sodg::Hex

source ·
pub enum Hex {
    Vector(Vec<u8>),
    Bytes([u8; 24], usize),
}
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(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§

§

Vector(Vec<u8>)

§

Bytes([u8; 24], usize)

Implementations§

Make an empty Hex.

use sodg::Hex;
let d = Hex::empty();
assert!(d.is_empty());
assert_eq!("--", d.print());

Bytes contained.

use sodg::Hex;
let d = Hex::from(2);
assert_eq!(8, d.len())

How many bytes in there.

use sodg::Hex;
let d = Hex::empty();
assert_eq!(0, d.len());

Create from slice, in appropriate mode.

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());

From Vec<u8>.

use sodg::Hex;
let d = Hex::from_vec(vec![0xCA, 0xFE]);
assert_eq!("CA-FE", d.print());

Make Hex from String.

use sodg::Hex;
let d = Hex::from_string_bytes("Ура!".to_string());
assert_eq!("D0-A3-D1-80-D0-B0-21", d.print());

Make hex from the bytes composing &str.

use sodg::Hex;
let d = Hex::from_str_bytes("Ура!");
assert_eq!("D0-A3-D1-80-D0-B0-21", d.print());

It’s empty and no data?

use sodg::Hex;
let d = Hex::from_vec(vec![]);
assert_eq!(true, d.is_empty());

Turn it into bool.

use sodg::Hex;
let d = Hex::from_vec([0x01].to_vec());
assert_eq!(true, d.to_bool().unwrap());

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());

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());

Turn it into string.

use sodg::Hex;
let d = Hex::from_vec([0x41, 0x42].to_vec());
assert_eq!("AB", d.to_utf8().unwrap());

Turn it into a hexadecimal string.

use sodg::Hex;
let d = Hex::from_vec([0xCA, 0xFE].to_vec());
assert_eq!("CA-FE", d.print());

Turn it into a vector of bytes (making a clone).

Take one byte.

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));

Skip a few bytes at the beginning and return the rest as a new instance of Hex.

use sodg::Hex;
let d = Hex::from_str_bytes("Hello, world!");
assert_eq!("world!", d.tail(7).to_utf8().unwrap());

Create a new Hex, which is a concatenation of self and h.

use sodg::Hex;
let a = Hex::from_slice("dead".as_bytes());
let b = Hex::from_slice("beef".as_bytes());
let c = a.concat(b);
assert_eq!(c, Hex::from_slice("deadbeef".as_bytes()));

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more

From bool.

use sodg::Hex;
let d = Hex::from(true);
assert_eq!("01", d.print());

Make Hex from f64.

use std::f64::consts::PI;
use sodg::Hex;
let d = Hex::from(PI);
assert_eq!("40-09-21-FB-54-44-2D-18", d.print());

Make Hex from i64.

use sodg::Hex;
let d = Hex::from(65536);
assert_eq!("00-00-00-00-00-01-00-00", d.print());

Creeate a Hex from a &str containing a hexadecimal representation of some data, for example, DE-AD-BE-EF-20-22.

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());
The associated error which can be returned from parsing.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.