Skip to main content

Format

Struct Format 

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

Source

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

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>

Source

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

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

Source§

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

Source§

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> Clone for Format<N>

Source§

fn clone(&self) -> Format<N>

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<const N: usize> Debug for Format<N>

Source§

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

Formats the formatted string for debugging.

Source§

impl<const N: usize> Display for Format<N>

Source§

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

Formats the formatted string for display.

Source§

impl<const N: usize> FromStr for Format<N>

Source§

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§

type Err = Error

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

impl<const N: usize> Hash for Format<N>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Hashes the formatted string.

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize> Ord for Format<N>

Source§

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) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const N: usize> PartialEq for Format<N>

Source§

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

Compares two formatted strings for equality.

§Examples
use zrx_id::format::Format;

// Create and compare formatted strings
let a: Format::<3> = "a:b:c".parse()?;
let b: Format::<3> = "a:b:c".parse()?;
assert_eq!(a, b);
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<const N: usize> PartialOrd for Format<N>

Source§

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

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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<K, V> TryAsStorage<K> for V
where K: Key, V: Value,

Source§

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:

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

type Target<'a> = &'a Storage<K, V>

Target type of conversion.
Source§

impl<K, V> TryAsStorageMut<K> for V
where K: Key, V: Value,

Source§

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:

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

type Target<'a> = &'a mut Storage<K, V>

Target type of conversion.
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> Id for T
where T: Clone + Debug + Display + Eq + Hash + Ord + Send + Sync + 'static,

Source§

impl<T> Key for T
where T: Clone + Debug + Eq + Hash + Ord + 'static,

Source§

impl<T> Value for T
where T: Debug + Eq + 'static,