Hex

Enum Hex 

Source
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§

§

Vector(Vec<u8>)

§

Bytes([u8; 24], usize)

Implementations§

Source§

impl Hex

Source

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

pub fn bytes(&self) -> &[u8]

Take the bytes contained.

For example:

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

pub fn len(&self) -> usize

Count, how many bytes are in there.

For example, an empty Hex has zero bytes:

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

A non-empty Hex with an i64 inside has eight bytes:

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

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

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

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

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

pub fn to_bool(&self) -> bool

Turn it into bool.

For example:

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

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

If it’s impossible to convert to an integer, an error will be returned.

Source

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

If it’s impossible to convert to a float, an error will be returned.

Source

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

If it’s impossible to convert to a UTF-8 string, an error will be returned.

Source

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

pub fn to_vec(&self) -> Vec<u8>

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

Source

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

pub fn tail(&self, skip: usize) -> Self

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

For example:

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

pub fn concat(&self, h: &Self) -> Self

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

For example:

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§

Source§

impl Clone for Hex

Source§

fn clone(&self) -> Hex

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Hex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Hex

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Hex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<bool> for Hex

Source§

fn from(d: bool) -> Self

Create a new Hex from bool.

For example:

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

impl From<f64> for Hex

Source§

fn from(d: f64) -> Self

Make a new Hex from f64.

For example:

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

impl From<i64> for Hex

Source§

fn from(d: i64) -> Self

Make a new Hex from i64.

For example:

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

impl FromStr for Hex

Source§

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

If it’s impossible to convert from a String, an error will be returned.

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

impl PartialEq for Hex

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Hex

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Hex

Auto Trait Implementations§

§

impl Freeze for Hex

§

impl RefUnwindSafe for Hex

§

impl Send for Hex

§

impl Sync for Hex

§

impl Unpin for Hex

§

impl UnwindSafe for Hex

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,