Struct Hex
pub struct Hex<'a> { /* private fields */ }Expand description
A borrowed view that encodes bytes as hex digits with optional framing.
Construct a view with crate::fmt::hex. Creating, copying, or configuring
it does not allocate or encode the input. Each byte is always represented by
two digits, in input order, including leading zero bytes.
Display and the output methods use the configured case and
prefix and separator. LowerHex and UpperHex
override case and prefix: :x and :X omit the prefix, while :#x and :#X
include 0x. These traits retain the configured separator.
Other formatting flags are ignored.
use rama_utils::fmt::hex;
let bytes = [0x00, 0xab, 0xff];
let view = hex(&bytes);
assert_eq!(view.to_string(), "00abff");
assert_eq!(view.to_vec(), b"00abff");
assert_eq!(format!("{view:#X}"), "0x00ABFF");
assert_eq!(view.with_custom_prefix("bytes:").with_separator(":").to_string(), "bytes:00:ab:ff");
assert_eq!(view.with_upper_case().with_prefix(true).to_string(), "0x00ABFF");
let mut storage = [0; 8];
assert_eq!(view.encode_to_slice(&mut storage)?, b"00abff");Implementations§
§impl<'a> Hex<'a>
impl<'a> Hex<'a>
pub const fn with_lower_case(self) -> Hex<'a>
Available on crate features grpc and http and std only.
pub const fn with_lower_case(self) -> Hex<'a>
grpc and http and std only.Select lowercase hex digits.
Applies to Display and output methods.
pub fn set_lower_case(&mut self) -> &mut Hex<'a>
Available on crate features grpc and http and std only.
pub fn set_lower_case(&mut self) -> &mut Hex<'a>
grpc and http and std only.Select lowercase hex digits.
Applies to Display and output methods.
pub const fn with_upper_case(self) -> Hex<'a>
Available on crate features grpc and http and std only.
pub const fn with_upper_case(self) -> Hex<'a>
grpc and http and std only.Select uppercase hex digits.
Applies to Display and output methods.
pub fn set_upper_case(&mut self) -> &mut Hex<'a>
Available on crate features grpc and http and std only.
pub fn set_upper_case(&mut self) -> &mut Hex<'a>
grpc and http and std only.Select uppercase hex digits.
Applies to Display and output methods.
pub const fn with_prefix(self, prefix: bool) -> Hex<'a>
Available on crate features grpc and http and std only.
pub const fn with_prefix(self, prefix: bool) -> Hex<'a>
grpc and http and std only.Enable or disable the 0x prefix for Display and output
methods. The prefix itself is lowercase regardless of the digit case.
pub fn set_prefix(&mut self, prefix: bool) -> &mut Hex<'a>
Available on crate features grpc and http and std only.
pub fn set_prefix(&mut self, prefix: bool) -> &mut Hex<'a>
grpc and http and std only.Enable or disable the 0x prefix for Display and output
methods. The prefix itself is lowercase regardless of the digit case.
pub const fn with_custom_prefix(self, prefix: &'a str) -> Hex<'a>
Available on crate features grpc and http and std only.
pub const fn with_custom_prefix(self, prefix: &'a str) -> Hex<'a>
grpc and http and std only.Set a custom literal prefix without allocating.
pub fn set_custom_prefix(&mut self, prefix: &'a str) -> &mut Hex<'a>
Available on crate features grpc and http and std only.
pub fn set_custom_prefix(&mut self, prefix: &'a str) -> &mut Hex<'a>
grpc and http and std only.Set a custom literal prefix without allocating.
pub const fn with_separator(self, separator: &'a str) -> Hex<'a>
Available on crate features grpc and http and std only.
pub const fn with_separator(self, separator: &'a str) -> Hex<'a>
grpc and http and std only.Set a literal separator between bytes without allocating.
See Format::with_separator for details.
pub fn set_separator(&mut self, separator: &'a str) -> &mut Hex<'a>
Available on crate features grpc and http and std only.
pub fn set_separator(&mut self, separator: &'a str) -> &mut Hex<'a>
grpc and http and std only.Set a literal separator between bytes without allocating.
See Format::with_separator for details.
pub const fn with_format(self, format: Format<'a>) -> Hex<'a>
Available on crate features grpc and http and std only.
pub const fn with_format(self, format: Format<'a>) -> Hex<'a>
grpc and http and std only.Apply shared encoding/decoding configuration without allocating.
pub fn set_format(&mut self, format: Format<'a>) -> &mut Hex<'a>
Available on crate features grpc and http and std only.
pub fn set_format(&mut self, format: Format<'a>) -> &mut Hex<'a>
grpc and http and std only.Apply shared encoding/decoding configuration without allocating.
pub const fn encoded_len(&self) -> usize
Available on crate features grpc and http and std only.
pub const fn encoded_len(&self) -> usize
grpc and http and std only.Number of UTF-8 bytes in the output, including prefix and separators.
§Panics
Panics if the encoded length exceeds usize::MAX. Output methods that
reserve or check destination space have the same restriction.
pub fn to_vec(&self) -> Vec<u8> ⓘ
Available on crate features grpc and http and std only.
pub fn to_vec(&self) -> Vec<u8> ⓘ
grpc and http and std only.Encode into a new vector of UTF-8 bytes (digits, prefix, and separators).
pub fn append_to_vec(&self, output: &mut Vec<u8>)
Available on crate features grpc and http and std only.
pub fn append_to_vec(&self, output: &mut Vec<u8>)
grpc and http and std only.Append encoded bytes, reserving space without clearing existing data.
pub fn append_to_string(&self, output: &mut String)
Available on crate features grpc and http and std only.
pub fn append_to_string(&self, output: &mut String)
grpc and http and std only.Append hex text, reserving space without clearing existing contents.
pub fn encode_to_slice<'out>(
&self,
output: &'out mut [u8],
) -> Result<&'out mut [u8], BufferTooSmall>
Available on crate features grpc and http and std only.
pub fn encode_to_slice<'out>( &self, output: &'out mut [u8], ) -> Result<&'out mut [u8], BufferTooSmall>
grpc and http and std only.Encode into the start of output and return the written subslice.
Does not allocate. Extra destination bytes are left untouched. If the destination is too small, returns an error without changing any bytes.
pub fn write_to<W>(&self, writer: &mut W) -> Result<(), Error>
Available on crate features grpc and http and std only.
pub fn write_to<W>(&self, writer: &mut W) -> Result<(), Error>
grpc and http and std only.Write configured hex text without allocating an intermediate string.
The destination can allocate as it grows. Writer errors are propagated;
on failure, the destination may contain a partially written result.
For a std::io::Write destination, use write!(writer, "{view}")
with the I/O trait in scope instead.
Trait Implementations§
impl<'a> Copy for Hex<'a>
§impl Serialize for Hex<'_>
impl Serialize for Hex<'_>
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
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§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more§impl<T, U> RamaTryFrom<T> for Uwhere
U: TryFrom<T>,
impl<T, U> RamaTryFrom<T> for Uwhere
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn rama_try_from(value: T) -> Result<U, <U as RamaTryFrom<T>>::Error>
§impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for Twhere
U: RamaTryFrom<T, CrateMarker>,
impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for Twhere
U: RamaTryFrom<T, CrateMarker>,
type Error = <U as RamaTryFrom<T, CrateMarker>>::Error
fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T, CrateMarker>>::Error>
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.