Skip to main content

Hex

Struct Hex 

Source
pub struct Hex<'a> { /* private fields */ }
Expand description

A borrowed hexadecimal view of a byte slice, available without allocation.

Display and LowerHex use lowercase digits; UpperHex uses uppercase. Every byte produces two digits in source order, including leading zeroes. The slice is borrowed, not copied. This is a byte-sequence view: it does not interpret the input as an integer or reverse bytes according to the machine’s endianness.

§Formatting

All three formatting traits use the following integer-style flags:

  • # adds 0x, also for uppercase output and an empty slice.
  • + adds a leading plus sign. Width includes the sign and prefix.
  • Width, fill and alignment apply to the complete value; default alignment is right. Width counts Unicode characters, so a non-ASCII fill character counts as one.
  • 0 pads after the sign/prefix, overriding fill and alignment.
  • Precision and - are ignored. Precision never truncates a byte sequence.

§Allocation and writer errors

Available without alloc or std. Formatting uses bounded stack storage and allocates no intermediate string; the destination writer may still allocate. For example, format! allocates its resulting string, while write! can write into existing storage.

An error from the writer is returned immediately and no further writes are attempted. Text already accepted by the writer remains written, including a partial write from the failing call. The number and size of writes are unspecified. To make output atomic, format into a separate buffer first.

§Examples

use faster_hex::Hex;
let bytes = [0, 0xab, 0xcd];
let hex = Hex::new(&bytes);
assert_eq!(format!("{hex}"), "00abcd");
assert_eq!(format!("{hex:#010X}"), "0x0000ABCD");
assert_eq!(format!("{hex:.2}"), "00abcd");

Append to a text destination without creating an intermediate hex string:

use core::fmt::Write;
use faster_hex::Hex;

let mut output = String::with_capacity(64);
write!(output, "hash={:#X}", Hex::new(&[0, 0xab, 0xcd]))?;
assert_eq!(output, "hash=0x00ABCD");

Implementations§

Source§

impl<'a> Hex<'a>

Source

pub const fn new(bytes: &'a [u8]) -> Self

Borrows bytes for hexadecimal formatting without copying or allocating.

The view cannot outlive bytes. It accepts an empty slice and is usable in constant expressions. Creating a view does not perform any encoding.

§Examples
use faster_hex::Hex;

const ID: Hex<'static> = Hex::new(&[0, 0xab]);
assert_eq!(format!("{ID}"), "00ab");
assert_eq!(format!("{ID:#X}"), "0x00AB");

Trait Implementations§

Source§

impl<'a> Clone for Hex<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Hex<'a>

Source§

impl<'a> Debug for Hex<'a>

Source§

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

Formats the value using the given formatter. 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 LowerHex for Hex<'_>

Source§

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

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

impl UpperHex for Hex<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Hex<'a>

§

impl<'a> RefUnwindSafe for Hex<'a>

§

impl<'a> Send for Hex<'a>

§

impl<'a> Sync for Hex<'a>

§

impl<'a> Unpin for Hex<'a>

§

impl<'a> UnsafeUnpin for Hex<'a>

§

impl<'a> UnwindSafe for Hex<'a>

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.