html5ever::tendril

Struct Tendril

Source
#[repr(C)]
pub struct Tendril<F, A = NonAtomic>
where F: Format, A: Atomicity,
{ /* private fields */ }
Expand description

Compact string type for zero-copy parsing.

Tendrils have the semantics of owned strings, but are sometimes views into shared buffers. When you mutate a Tendril, an owned copy is made if necessary. Further mutations occur in-place until the string becomes shared, e.g. with clone() or subtendril().

Buffer sharing is accomplished through thread-local (non-atomic) reference counting, which has very low overhead. The Rust type system will prevent you at compile time from sending a Tendril between threads. We plan to relax this restriction in the future; see README.md.

Whereas String allocates in the heap for any non-empty string, Tendril can store small strings (up to 8 bytes) in-line, without a heap allocation. Tendril is also smaller than String on 64-bit platforms — 16 bytes versus 24.

The type parameter F specifies the format of the tendril, for example UTF-8 text or uninterpreted bytes. The parameter will be instantiated with one of the marker types from tendril::fmt. See the StrTendril and ByteTendril type aliases for two examples.

The type parameter A indicates the atomicity of the tendril; it is by default NonAtomic, but can be specified as Atomic to get a tendril which implements Send (viz. a thread-safe tendril).

The maximum length of a Tendril is 4 GB. The library will panic if you attempt to go over the limit.

Implementations§

Source§

impl<F, A> Tendril<F, A>
where F: Format, A: Atomicity,

Source

pub fn new() -> Tendril<F, A>

Create a new, empty Tendril in any format.

Source

pub fn with_capacity(capacity: u32) -> Tendril<F, A>

Create a new, empty Tendril with a specified capacity.

Source

pub fn reserve(&mut self, additional: u32)

Reserve space for additional bytes.

This is only a suggestion. There are cases where Tendril will decline to allocate until the buffer is actually modified.

Source

pub fn len32(&self) -> u32

Get the length of the Tendril.

This is named not to conflict with len() on the underlying slice, if any.

Source

pub fn is_shared(&self) -> bool

Is the backing buffer shared?

Source

pub fn is_shared_with(&self, other: &Tendril<F, A>) -> bool

Is the backing buffer shared with this other Tendril?

Source

pub fn clear(&mut self)

Truncate to length 0 without discarding any owned storage.

Source

pub fn try_from_byte_slice(x: &[u8]) -> Result<Tendril<F, A>, ()>

Build a Tendril by copying a byte slice, if it conforms to the format.

Source

pub fn as_bytes(&self) -> &Tendril<Bytes, A>

View as uninterpreted bytes.

Source

pub fn into_bytes(self) -> Tendril<Bytes, A>

Convert into uninterpreted bytes.

Source

pub fn into_send(self) -> SendTendril<F>

Convert self into a type which is Send.

If the tendril is owned or inline, this is free, but if it’s shared this will entail a copy of the contents.

Source

pub fn as_superset<Super>(&self) -> &Tendril<Super, A>
where F: SubsetOf<Super>, Super: Format,

View as a superset format, for free.

Source

pub fn into_superset<Super>(self) -> Tendril<Super, A>
where F: SubsetOf<Super>, Super: Format,

Convert into a superset format, for free.

Source

pub fn try_as_subset<Sub>(&self) -> Result<&Tendril<Sub, A>, ()>
where Sub: SubsetOf<F>,

View as a subset format, if the Tendril conforms to that subset.

Source

pub fn try_into_subset<Sub>(self) -> Result<Tendril<Sub, A>, Tendril<F, A>>
where Sub: SubsetOf<F>,

Convert into a subset format, if the Tendril conforms to that subset.

Source

pub fn try_reinterpret_view<Other>(&self) -> Result<&Tendril<Other, A>, ()>
where Other: Format,

View as another format, if the bytes of the Tendril are valid for that format.

Source

pub fn try_reinterpret<Other>(self) -> Result<Tendril<Other, A>, Tendril<F, A>>
where Other: Format,

Convert into another format, if the Tendril conforms to that format.

This only re-validates the existing bytes under the new format. It will not change the byte content of the tendril!

See the encode and decode methods for character encoding conversion.

Source

pub fn try_push_bytes(&mut self, buf: &[u8]) -> Result<(), ()>

Push some bytes onto the end of the Tendril, if they conform to the format.

Source

pub fn push_tendril(&mut self, other: &Tendril<F, A>)

Push another Tendril onto the end of this one.

Source

pub fn try_subtendril( &self, offset: u32, length: u32, ) -> Result<Tendril<F, A>, SubtendrilError>

Attempt to slice this Tendril as a new Tendril.

This will share the buffer when possible. Mutating a shared buffer will copy the contents.

The offset and length are in bytes. The function will return Err if these are out of bounds, or if the resulting slice does not conform to the format.

Source

pub fn subtendril(&self, offset: u32, length: u32) -> Tendril<F, A>

Slice this Tendril as a new Tendril.

Panics on bounds or validity check failure.

Source

pub fn try_pop_front(&mut self, n: u32) -> Result<(), SubtendrilError>

Try to drop n bytes from the front.

Returns Err if the bytes are not available, or the suffix fails validation.

Source

pub fn pop_front(&mut self, n: u32)

Drop n bytes from the front.

Panics if the bytes are not available, or the suffix fails validation.

Source

pub fn try_pop_back(&mut self, n: u32) -> Result<(), SubtendrilError>

Drop n bytes from the back.

Returns Err if the bytes are not available, or the prefix fails validation.

Source

pub fn pop_back(&mut self, n: u32)

Drop n bytes from the back.

Panics if the bytes are not available, or the prefix fails validation.

Source

pub unsafe fn reinterpret_view_without_validating<Other>( &self, ) -> &Tendril<Other, A>
where Other: Format,

View as another format, without validating.

Source

pub unsafe fn reinterpret_without_validating<Other>(self) -> Tendril<Other, A>
where Other: Format,

Convert into another format, without validating.

Source

pub unsafe fn from_byte_slice_without_validating(x: &[u8]) -> Tendril<F, A>

Build a Tendril by copying a byte slice, without validating.

Source

pub unsafe fn push_bytes_without_validating(&mut self, buf: &[u8])

Push some bytes onto the end of the Tendril, without validating.

Source

pub unsafe fn unsafe_subtendril( &self, offset: u32, length: u32, ) -> Tendril<F, A>

Slice this Tendril as a new Tendril.

Does not check validity or bounds!

Source

pub unsafe fn unsafe_pop_front(&mut self, n: u32)

Drop n bytes from the front.

Does not check validity or bounds!

Source

pub unsafe fn unsafe_pop_back(&mut self, n: u32)

Drop n bytes from the back.

Does not check validity or bounds!

Source§

impl<F, A> Tendril<F, A>
where F: SliceFormat, A: Atomicity,

Source

pub fn from_slice(x: &<F as SliceFormat>::Slice) -> Tendril<F, A>

Build a Tendril by copying a slice.

Source

pub fn push_slice(&mut self, x: &<F as SliceFormat>::Slice)

Push a slice onto the end of the Tendril.

Source§

impl<F, A> Tendril<F, A>
where F: for<'a> CharFormat<'a>, A: Atomicity,

Source

pub fn pop_front_char<'a>(&'a mut self) -> Option<char>

Remove and return the first character, if any.

Source

pub fn pop_front_char_run<'a, C, R>( &'a mut self, classify: C, ) -> Option<(Tendril<F, A>, R)>
where C: FnMut(char) -> R, R: PartialEq,

Remove and return a run of characters at the front of the Tendril which are classified the same according to the function classify.

Returns None on an empty string.

Source

pub fn try_push_char(&mut self, c: char) -> Result<(), ()>

Push a character, if it can be represented in this format.

Source§

impl<F, A> Tendril<F, A>
where A: Atomicity, F: SliceFormat<Slice = [u8]>,

Source

pub unsafe fn push_uninitialized(&mut self, n: u32)

Push “uninitialized bytes” onto the end.

Really, this grows the tendril without writing anything to the new area. It’s only defined for byte tendrils because it’s only useful if you plan to then mutate the buffer.

Source§

impl<A> Tendril<UTF8, A>
where A: Atomicity,

Source

pub fn push_char(&mut self, c: char)

Push a character onto the end.

Source

pub fn from_char(c: char) -> Tendril<UTF8, A>

Create a Tendril from a single character.

Source

pub fn format(args: Arguments<'_>) -> Tendril<UTF8, A>

Helper for the format_tendril! macro.

Source§

impl<A> Tendril<Bytes, A>
where A: Atomicity,

Source

pub fn decode_utf8_lossy<F>(self, push_utf8: F) -> Option<IncompleteUtf8>
where F: FnMut(Tendril<UTF8, A>),

Trait Implementations§

Source§

impl<F, A> AsRef<<F as SliceFormat>::Slice> for Tendril<F, A>
where F: SliceFormat, A: Atomicity,

Source§

fn as_ref(&self) -> &<F as SliceFormat>::Slice

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

impl<F, A> Borrow<[u8]> for Tendril<F, A>
where F: SliceFormat, A: Atomicity,

Source§

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

Immutably borrows from an owned value. Read more
Source§

impl<F, A> Clone for Tendril<F, A>
where F: Format, A: Atomicity,

Source§

fn clone(&self) -> Tendril<F, A>

Returns a copy 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<F, A> Debug for Tendril<F, A>

Source§

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

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

impl<F, A> Default for Tendril<F, A>
where F: Format, A: Atomicity,

Source§

fn default() -> Tendril<F, A>

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

impl<F, A> Deref for Tendril<F, A>
where F: SliceFormat, A: Atomicity,

Source§

type Target = <F as SliceFormat>::Slice

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<F as SliceFormat>::Slice

Dereferences the value.
Source§

impl<F, A> DerefMut for Tendril<F, A>
where F: SliceFormat, A: Atomicity,

Source§

fn deref_mut(&mut self) -> &mut <F as SliceFormat>::Slice

Mutably dereferences the value.
Source§

impl<A> Display for Tendril<UTF8, A>
where A: Atomicity,

Source§

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

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

impl<F, A> Drop for Tendril<F, A>
where F: Format, A: Atomicity,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, A> Extend<&'a [u8]> for Tendril<Bytes, A>
where A: Atomicity,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = &'a [u8]>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, F, A> Extend<&'a Tendril<F, A>> for Tendril<F, A>
where F: Format + 'a, A: Atomicity,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = &'a Tendril<F, A>>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, A> Extend<&'a str> for Tendril<UTF8, A>
where A: Atomicity,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = &'a str>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, A> Extend<&'a u8> for Tendril<Bytes, A>
where A: Atomicity,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = &'a u8>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<A> Extend<char> for Tendril<UTF8, A>
where A: Atomicity,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = char>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<A> Extend<u8> for Tendril<Bytes, A>
where A: Atomicity,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = u8>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, F, A> From<&'a <F as SliceFormat>::Slice> for Tendril<F, A>
where F: SliceFormat, A: Atomicity,

Source§

fn from(input: &<F as SliceFormat>::Slice) -> Tendril<F, A>

Converts to this type from the input type.
Source§

impl<F, A> From<SendTendril<F>> for Tendril<F, A>
where F: Format, A: Atomicity,

Source§

fn from(send: SendTendril<F>) -> Tendril<F, A>

Converts to this type from the input type.
Source§

impl<A> From<String> for Tendril<UTF8, A>
where A: Atomicity,

Source§

fn from(input: String) -> Tendril<UTF8, A>

Converts to this type from the input type.
Source§

impl<F, A> From<Tendril<F, A>> for SendTendril<F>
where F: Format, A: Atomicity,

Source§

fn from(tendril: Tendril<F, A>) -> SendTendril<F>

Converts to this type from the input type.
Source§

impl<'a, A> FromIterator<&'a [u8]> for Tendril<Bytes, A>
where A: Atomicity,

Source§

fn from_iter<I>(iterable: I) -> Tendril<Bytes, A>
where I: IntoIterator<Item = &'a [u8]>,

Creates a value from an iterator. Read more
Source§

impl<'a, F, A> FromIterator<&'a Tendril<F, A>> for Tendril<F, A>
where F: Format + 'a, A: Atomicity,

Source§

fn from_iter<I>(iterable: I) -> Tendril<F, A>
where I: IntoIterator<Item = &'a Tendril<F, A>>,

Creates a value from an iterator. Read more
Source§

impl<'a, A> FromIterator<&'a str> for Tendril<UTF8, A>
where A: Atomicity,

Source§

fn from_iter<I>(iterable: I) -> Tendril<UTF8, A>
where I: IntoIterator<Item = &'a str>,

Creates a value from an iterator. Read more
Source§

impl<'a, A> FromIterator<&'a u8> for Tendril<Bytes, A>
where A: Atomicity,

Source§

fn from_iter<I>(iterable: I) -> Tendril<Bytes, A>
where I: IntoIterator<Item = &'a u8>,

Creates a value from an iterator. Read more
Source§

impl<A> FromIterator<char> for Tendril<UTF8, A>
where A: Atomicity,

Source§

fn from_iter<I>(iterable: I) -> Tendril<UTF8, A>
where I: IntoIterator<Item = char>,

Creates a value from an iterator. Read more
Source§

impl<A> FromIterator<u8> for Tendril<Bytes, A>
where A: Atomicity,

Source§

fn from_iter<I>(iterable: I) -> Tendril<Bytes, A>
where I: IntoIterator<Item = u8>,

Creates a value from an iterator. Read more
Source§

impl<A> FromStr for Tendril<UTF8, A>
where A: Atomicity,

Source§

type Err = ()

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

fn from_str(s: &str) -> Result<Tendril<UTF8, A>, ()>

Parses a string s to return a value of this type. Read more
Source§

impl<F, A> Hash for Tendril<F, A>
where F: Format, A: Atomicity,

Source§

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

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<F, A> Ord for Tendril<F, A>
where F: SliceFormat, <F as SliceFormat>::Slice: Ord, A: Atomicity,

Source§

fn cmp(&self, other: &Tendril<F, A>) -> 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<F, A> PartialEq for Tendril<F, A>
where F: Format, A: Atomicity,

Source§

fn eq(&self, other: &Tendril<F, A>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &Tendril<F, A>) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<F, A> PartialOrd for Tendril<F, A>

Source§

fn partial_cmp(&self, other: &Tendril<F, A>) -> 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> Write for Tendril<Bytes, A>
where A: Atomicity,

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Error>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn flush(&mut self) -> Result<(), Error>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
Source§

impl<A> Write for Tendril<UTF8, A>
where A: Atomicity,

Source§

fn write_str(&mut self, s: &str) -> Result<(), Error>

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more
Source§

impl<F, A> Eq for Tendril<F, A>
where F: Format, A: Atomicity,

Source§

impl<F, A> Send for Tendril<F, A>
where F: Format, A: Atomicity + Sync,

Auto Trait Implementations§

§

impl<F, A = NonAtomic> !Freeze for Tendril<F, A>

§

impl<F, A = NonAtomic> !RefUnwindSafe for Tendril<F, A>

§

impl<F, A = NonAtomic> !Sync for Tendril<F, A>

§

impl<F, A> Unpin for Tendril<F, A>
where A: Unpin,

§

impl<F, A> UnwindSafe for Tendril<F, 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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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§

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