pub struct HexArray<const N: usize>(pub [u8; N]);hex only.Expand description
A byte array that displays and parses as hex.
HexArray<N> wraps [u8; N], providing Display,
FromStr, LowerHex, and
UpperHex implementations that use hexadecimal
encoding.
With the serde feature enabled, it also implements
Serialize and Deserialize (hex strings in human-readable
formats, raw bytes in binary formats), and can be used with
#[serde(with = "HexArray::<N>")] on [u8; N] fields.
§Examples
use byte_wrapper::HexArray;
let h = HexArray::new([0x01, 0x02, 0xab, 0xff]);
assert_eq!(h.to_string(), "0102abff");
let parsed: HexArray<4> = "0102abff".parse().unwrap();
assert_eq!(parsed, h);Tuple Fields§
§0: [u8; N]Implementations§
Source§impl<const N: usize> HexArray<N>
impl<const N: usize> HexArray<N>
Sourcepub fn serialize<S>(bytes: &[u8; N], serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Available on crate feature serde only.
pub fn serialize<S>(bytes: &[u8; N], serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
serde only.Serializes a byte array as hex in human-readable formats, or as raw bytes otherwise.
Intended for use with
#[serde(with = "HexArray::<N>")].
§Examples
use byte_wrapper::HexArray;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Record {
#[serde(with = "HexArray::<4>")]
id: [u8; 4],
}
let r = Record { id: [0x01, 0x02, 0x03, 0x04] };
let json = serde_json::to_string(&r).unwrap();
assert_eq!(json, r#"{"id":"01020304"}"#);Sourcepub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; N], D::Error>where
D: Deserializer<'de>,
Available on crate feature serde only.
pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; N], D::Error>where
D: Deserializer<'de>,
serde only.Deserializes a byte array from hex if the format is human-readable, or as raw bytes otherwise.
Intended for use with
#[serde(with = "HexArray::<N>")].
§Examples
use byte_wrapper::HexArray;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Record {
#[serde(with = "HexArray::<4>")]
id: [u8; 4],
}
let r: Record = serde_json::from_str(r#"{"id":"01020304"}"#).unwrap();
assert_eq!(r.id, [0x01, 0x02, 0x03, 0x04]);Methods from Deref<Target = [u8; N]>§
Sourcepub fn as_ascii(&self) -> Option<&[AsciiChar; N]>
🔬This is a nightly-only experimental API. (ascii_char)
pub fn as_ascii(&self) -> Option<&[AsciiChar; N]>
ascii_char)Converts this array of bytes into an array of ASCII characters,
or returns None if any of the characters is non-ASCII.
§Examples
#![feature(ascii_char)]
const HEX_DIGITS: [std::ascii::Char; 16] =
*b"0123456789abcdef".as_ascii().unwrap();
assert_eq!(HEX_DIGITS[1].as_str(), "1");
assert_eq!(HEX_DIGITS[10].as_str(), "a");Sourcepub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]
🔬This is a nightly-only experimental API. (ascii_char)
pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]
ascii_char)Converts this array of bytes into an array of ASCII characters, without checking whether they’re valid.
§Safety
Every byte in the array must be in 0..=127, or else this is UB.
1.57.0 · Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice containing the entire array. Equivalent to &s[..].
1.57.0 · Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice containing the entire array. Equivalent to
&mut s[..].
1.77.0 · Sourcepub fn each_ref(&self) -> [&T; N]
pub fn each_ref(&self) -> [&T; N]
Borrows each element and returns an array of references with the same
size as self.
§Example
let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);This method is particularly useful if combined with other methods, like
map. This way, you can avoid moving the original
array if its elements are not Copy.
let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);
// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);1.77.0 · Sourcepub fn each_mut(&mut self) -> [&mut T; N]
pub fn each_mut(&mut self) -> [&mut T; N]
Borrows each element mutably and returns an array of mutable references
with the same size as self.
§Example
let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);Sourcepub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
🔬This is a nightly-only experimental API. (split_array)
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
split_array)Divides one array reference into two at an index.
The first will contain all indices from [0, M) (excluding
the index M itself) and the second will contain all
indices from [M, N) (excluding the index N itself).
§Panics
Panics if M > N.
§Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.split_array_ref::<0>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<2>();
assert_eq!(left, &[1, 2]);
assert_eq!(right, &[3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<6>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}Sourcepub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
🔬This is a nightly-only experimental API. (split_array)
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
split_array)Divides one mutable array reference into two at an index.
The first will contain all indices from [0, M) (excluding
the index M itself) and the second will contain all
indices from [M, N) (excluding the index N itself).
§Panics
Panics if M > N.
§Examples
#![feature(split_array)]
let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);Sourcepub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
🔬This is a nightly-only experimental API. (split_array)
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
split_array)Divides one array reference into two at an index from the end.
The first will contain all indices from [0, N - M) (excluding
the index N - M itself) and the second will contain all
indices from [N - M, N) (excluding the index N itself).
§Panics
Panics if M > N.
§Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.rsplit_array_ref::<0>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}
{
let (left, right) = v.rsplit_array_ref::<2>();
assert_eq!(left, &[1, 2, 3, 4]);
assert_eq!(right, &[5, 6]);
}
{
let (left, right) = v.rsplit_array_ref::<6>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}Sourcepub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
🔬This is a nightly-only experimental API. (split_array)
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
split_array)Divides one mutable array reference into two at an index from the end.
The first will contain all indices from [0, N - M) (excluding
the index N - M itself) and the second will contain all
indices from [N - M, N) (excluding the index N itself).
§Panics
Panics if M > N.
§Examples
#![feature(split_array)]
let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);Trait Implementations§
Source§impl<'de, const N: usize> Deserialize<'de> for HexArray<N>
Available on crate feature serde only.
impl<'de, const N: usize> Deserialize<'de> for HexArray<N>
serde only.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<const N: usize> JsonSchema for HexArray<N>
Available on crate feature schemars08 only.
impl<const N: usize> JsonSchema for HexArray<N>
schemars08 only.Source§fn schema_name() -> String
fn schema_name() -> String
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref keyword. Read moreSource§fn json_schema(_generator: &mut SchemaGenerator) -> Schema
fn json_schema(_generator: &mut SchemaGenerator) -> Schema
Source§impl<const N: usize> Ord for HexArray<N>
impl<const N: usize> Ord for HexArray<N>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const N: usize> PartialOrd for HexArray<N>
impl<const N: usize> PartialOrd for HexArray<N>
impl<const N: usize> Copy for HexArray<N>
impl<const N: usize> Eq for HexArray<N>
impl<const N: usize> StructuralPartialEq for HexArray<N>
Auto Trait Implementations§
impl<const N: usize> Freeze for HexArray<N>
impl<const N: usize> RefUnwindSafe for HexArray<N>
impl<const N: usize> Send for HexArray<N>
impl<const N: usize> Sync for HexArray<N>
impl<const N: usize> Unpin for HexArray<N>
impl<const N: usize> UnsafeUnpin for HexArray<N>
impl<const N: usize> UnwindSafe for HexArray<N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Upper case
letters are used (e.g. F9B4CA)