pub struct Format<const N: usize> { /* private fields */ }Expand description
Formatted string.
This is a low-level construct which allows to efficiently work with strings
that contain a predefined number of components separated by : characters.
If a component contains a : itself, it is percent-encoded, indicated by
a flag. This is slower, but not expected to be common.
Formatted strings are optimized for very fast conversion with FromStr
or gradual construction through Format::builder, which both produce an
immutable instance. To make sure cloning is fast, it’s recommended to wrap
the encapsulating type, Selector or Id, in an Arc.
This implementation is currently limited to 64 spans, which should probably
be sufficient for all use cases that can ever happen. For our means, an u8
would be more than enough, but since Rust will align the field to 64 bits
anyway, there’s no point in being cheap.
§Examples
use zrx_id::format::Format;
// Create formatted string builder
let mut builder = Format::<3>::builder();
builder.set(0, "a");
builder.set(1, "b");
builder.set(2, "c");
// Create formatted string from builder
let format = builder.build()?;
// Obtain string representation
assert_eq!(format.as_str(), "a:b:c");Implementations§
Source§impl<const N: usize> Format<N>
impl<const N: usize> Format<N>
Sourcepub fn builder<'a>() -> Builder<'a, N>
pub fn builder<'a>() -> Builder<'a, N>
Creates a formatted string builder.
§Examples
use zrx_id::format::Format;
// Create formatted string builder
let mut builder = Format::<3>::builder();Sourcepub fn to_builder(&self) -> Builder<'_, N>
pub fn to_builder(&self) -> Builder<'_, N>
Creates a formatted string builder from the formatted string.
This method creates a builder from the current formatted string, which allows to modify components and build a new formatted string.
§Examples
use zrx_id::format::Format;
// Create formatted string from string
let format: Format::<3> = "a:b:c".parse()?;
// Create formatted string builder
let mut builder = format.to_builder();
builder.set(2, "d");
// Create formatted string from builder
let format = builder.build()?;
assert_eq!(format.as_str(), "a:b:d");Source§impl<const N: usize> Format<N>
impl<const N: usize> Format<N>
Sourcepub fn get(&self, index: usize) -> Cow<'_, str>
pub fn get(&self, index: usize) -> Cow<'_, str>
Returns the value at the index.
If the value is not percent-encoded, which means it does not contain a
: character, a borrowed reference is returned which is essentially a
zero-cost operation and expected to be the common case. Otherwise, the
value is percent-decoded and an owned value is returned.
§Panics
Panics if the index is out of bounds. Since Format is a low-level
construct, we don’t expect this to happen, as indices should be known.
§Examples
use zrx_id::format::Format;
// Create formatted string builder
let mut builder = Format::<3>::builder();
builder.set(0, "a");
builder.set(1, "b");
builder.set(2, "c");
// Create formatted string from builder
let format = builder.build()?;
// Obtain value at index
assert_eq!(format.get(0), "a");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the string representation.
§Examples
use zrx_id::format::Format;
// Create formatted string builder
let mut builder = Format::<3>::builder();
builder.set(0, "a");
builder.set(1, "b");
builder.set(2, "c");
// Create formatted string from builder
let format = builder.build()?;
// Obtain string representation
assert_eq!(format.as_str(), "a:b:c");Trait Implementations§
Source§impl AsRef<Format<7>> for Id
impl AsRef<Format<7>> for Id
Source§fn as_ref(&self) -> &Format<7>
fn as_ref(&self) -> &Format<7>
Returns the formatted string.
Note that it’s normally not necessary to access the formatted string
directly, as all components can be accessed via the respective methods.
We need to access the underlying formatted string in our internal APIs,
e.g., to compute the Specificity for the given Id.
Source§impl AsRef<Format<7>> for Selector
impl AsRef<Format<7>> for Selector
Source§fn as_ref(&self) -> &Format<7>
fn as_ref(&self) -> &Format<7>
Returns the formatted string.
Note that it’s normally not necessary to access the formatted string
directly, as all components can be accessed via the respective methods.
We need to access the underlying formatted string in our internal APIs,
e.g., to compute the Specificity for the given Selector.
Source§impl<const N: usize> FromStr for Format<N>
impl<const N: usize> FromStr for Format<N>
Source§fn from_str(value: &str) -> Result<Self>
fn from_str(value: &str) -> Result<Self>
Attempts to create a formatted string from a string.
§Errors
If the span number is off, Error::Mismatch is returned.
§Examples
use zrx_id::format::Format;
// Create formatted string from string
let format: Format::<3> = "a:b:c".parse()?;
assert_eq!(format.as_str(), "a:b:c");Source§impl<const N: usize> Ord for Format<N>
impl<const N: usize> Ord for Format<N>
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Orders two formatted strings.
§Examples
use zrx_id::format::Format;
// Create and compare formatted strings
let a: Format::<3> = "b:c:d".parse()?;
let b: Format::<3> = "a:b:c".parse()?;
assert!(a > b);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> PartialEq for Format<N>
impl<const N: usize> PartialEq for Format<N>
Source§impl<const N: usize> PartialOrd for Format<N>
impl<const N: usize> PartialOrd for Format<N>
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Orders two formatted strings.
§Examples
use zrx_id::format::Format;
// Create and compare formatted strings
let a: Format::<3> = "b:c:d".parse()?;
let b: Format::<3> = "a:b:c".parse()?;
assert!(a > b);impl<const N: usize> Eq for Format<N>
Auto Trait Implementations§
impl<const N: usize> Freeze for Format<N>
impl<const N: usize> RefUnwindSafe for Format<N>
impl<const N: usize> Send for Format<N>
impl<const N: usize> Sync for Format<N>
impl<const N: usize> Unpin for Format<N>
impl<const N: usize> UnsafeUnpin for Format<N>
impl<const N: usize> UnwindSafe for Format<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> Pointable for T
impl<T> Pointable for T
Source§impl<K, V> TryAsStorage<K> for V
impl<K, V> TryAsStorage<K> for V
Source§fn try_as_storage(
item: &(dyn Any + 'static),
) -> Result<<V as TryAsStorage<K>>::Target<'_>, Error>
fn try_as_storage( item: &(dyn Any + 'static), ) -> Result<<V as TryAsStorage<K>>::Target<'_>, Error>
Attempts to convert into a storage reference.
§Errors
The following errors might be returned:
Error::Downcast: Item cannot be downcast.
§Examples
use std::any::Any;
use zrx_storage::convert::TryAsStorage;
use zrx_storage::Storage;
// Create storage and initial state
let mut storage = Storage::default();
storage.insert("key", 42);
// Obtain type-erased reference
let item: &dyn Any = &storage;
// Obtain storage reference
let storage = <i32>::try_as_storage(item)?;Source§impl<K, V> TryAsStorageMut<K> for V
impl<K, V> TryAsStorageMut<K> for V
Source§fn try_as_storage_mut(
item: &mut (dyn Any + 'static),
) -> Result<<V as TryAsStorageMut<K>>::Target<'_>, Error>
fn try_as_storage_mut( item: &mut (dyn Any + 'static), ) -> Result<<V as TryAsStorageMut<K>>::Target<'_>, Error>
Attempts to convert into a mutable storage reference.
§Errors
The following errors might be returned:
Error::Downcast: Item cannot be downcast.
§Examples
use std::any::Any;
use zrx_storage::convert::TryAsStorageMut;
use zrx_storage::Storage;
// Create storage and initial state
let mut storage = Storage::default();
storage.insert("key", 42);
// Obtain mutable type-erased reference
let item: &mut dyn Any = &mut storage;
// Obtain mutable storage reference
let storage = <i32>::try_as_storage_mut(item)?;