Skip to main content

StrView

Struct StrView 

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

Truly zero-copy string reference.

This is just a pointer and length (16 bytes on 64-bit systems), making it extremely cheap to copy and pass around. Unlike &str, it can be stored in structs without lifetime issues when the underlying buffer is known to outlive the view.

§Safety

The underlying data must:

  • Be valid UTF-8
  • Remain valid for the lifetime 'a
  • Not be modified while the view exists

Implementations§

Source§

impl<'a> StrView<'a>

Source

pub const fn new(s: &'a str) -> Self

Create a new string view from a string slice.

Source

pub const fn empty() -> Self

Create an empty string view.

Source

pub const unsafe fn from_raw_parts(ptr: *const u8, len: usize) -> Self

Create a string view from raw parts.

§Safety

The caller must ensure:

  • ptr points to valid UTF-8 data
  • The data is at least len bytes
  • The data remains valid for lifetime 'a
Source

pub fn as_str(&self) -> &'a str

Get the string slice this view represents.

Source

pub fn as_bytes(&self) -> &'a [u8]

Get the underlying bytes.

Source

pub const fn len(&self) -> usize

Get the length in bytes.

Source

pub const fn is_empty(&self) -> bool

Check if the view is empty.

Source

pub fn slice(&self, range: Range<usize>) -> StrView<'a>

Get a subview by byte range.

§Panics

Panics if:

  • The range is out of bounds
  • The range doesn’t lie on UTF-8 character boundaries
Source

pub fn try_slice(&self, range: Range<usize>) -> Option<StrView<'a>>

Try to get a subview by byte range.

Returns None if the range is invalid or doesn’t lie on UTF-8 character boundaries.

Source

pub unsafe fn slice_unchecked(&self, range: Range<usize>) -> StrView<'a>

Get a subview by byte range without bounds checking.

§Safety

The range must be within bounds and on UTF-8 character boundaries.

Source

pub fn split_once(&self, c: char) -> Option<(StrView<'a>, StrView<'a>)>

Split the view at the first occurrence of a character.

Source

pub fn strip_prefix(&self, prefix: &str) -> Option<StrView<'a>>

Strip a prefix from the view.

Source

pub fn strip_suffix(&self, suffix: &str) -> Option<StrView<'a>>

Strip a suffix from the view.

Source

pub fn trim(&self) -> StrView<'a>

Trim whitespace from both ends.

Source

pub fn starts_with(&self, prefix: &str) -> bool

Check if the view starts with a prefix.

Source

pub fn ends_with(&self, suffix: &str) -> bool

Check if the view ends with a suffix.

Source

pub fn contains(&self, pattern: &str) -> bool

Check if the view contains a pattern.

Source

pub fn find(&self, pattern: &str) -> Option<usize>

Find the first occurrence of a pattern.

Source

pub const fn as_ptr(&self) -> *const u8

Get the raw pointer to the string data.

Source§

impl<'a> StrView<'a>

Source

pub fn lines(&self) -> Lines<'a>

Iterate over lines in the string view.

Source§

impl<'a> StrView<'a>

Source

pub fn validate<V: Validate>(&self, validator: &V) -> Score<'static>

Validate this string view with the given validator.

Trait Implementations§

Source§

impl<'a> AsRef<[u8]> for StrView<'a>

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> AsRef<str> for StrView<'a>

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Clone for StrView<'a>

Source§

fn clone(&self) -> StrView<'a>

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<'a> Debug for StrView<'a>

Source§

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

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

impl<'a> Default for StrView<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> Display for StrView<'a>

Source§

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

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

impl<'a> From<&'a String> for StrView<'a>

Source§

fn from(s: &'a String) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a str> for StrView<'a>

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl<'a> Hash for StrView<'a>

Source§

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

Feeds this value into the given Hasher. Read more
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<'a> Ord for StrView<'a>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
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<'a> PartialEq<&str> for StrView<'a>

Source§

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

Tests for self and other values to be equal, and is used by ==.
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<'a> PartialEq<StrView<'a>> for str

Source§

fn eq(&self, other: &StrView<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
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<'a> PartialEq<str> for StrView<'a>

Source§

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

Tests for self and other values to be equal, and is used by ==.
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<'a> PartialEq for StrView<'a>

Source§

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

Tests for self and other values to be equal, and is used by ==.
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<'a> PartialOrd for StrView<'a>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
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<'a> Copy for StrView<'a>

Source§

impl<'a> Eq for StrView<'a>

Source§

impl<'a> Send for StrView<'a>

Source§

impl<'a> Sync for StrView<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for StrView<'a>

§

impl<'a> RefUnwindSafe for StrView<'a>

§

impl<'a> Unpin for StrView<'a>

§

impl<'a> UnsafeUnpin for StrView<'a>

§

impl<'a> UnwindSafe for StrView<'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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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<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.