Skip to main content

Utf8Builder

Struct Utf8Builder 

Source
pub struct Utf8Builder { /* private fields */ }
Expand description

A builder for building and validating UTF-8 data from chunks.

Implementations§

Source§

impl Utf8Builder

Source

pub const fn new() -> Self

Constructs a new, empty Utf8Builder.

Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty Utf8Builder with a specific capacity.

Source

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

Reserves capacity for at least additional more elements to be inserted in the given Utf8Builder.

Source

pub fn clear(&mut self)

Clears the builder, removing all data but keeping the allocated capacity.

Source

pub fn len(&self) -> usize

Returns the number of bytes in the buffer, including the bytes of the pending incomplete character.

Source

pub fn is_empty(&self) -> bool

Returns true if the builder contains no data.

Source

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

Returns the current data as a byte slice, including the bytes of the pending incomplete character.

Source§

impl Utf8Builder

Source

pub const fn is_valid(&self) -> bool

Returns true if there is no pending incomplete character.

Invalid bytes are never stored, so the data in the buffer is always valid UTF-8 when this returns true.

Source

pub fn finalize(self) -> Result<String, Utf8Error>

Consumes the builder and returns the built String.

Returns an error if the data ends with an incomplete character. The data is dropped in that case, so use into_bytes instead if the raw data is still needed.

Source

pub fn into_bytes(self) -> Vec<u8>

Consumes the builder and returns the raw data, even if it ends with an incomplete character.

Source§

impl Utf8Builder

Source

pub fn push(&mut self, b: u8) -> Result<(), Utf8Error>

Pushes a byte.

Returns an error if the byte is not allowed at the current position. Nothing is stored in that case, so a correct byte can still be pushed afterwards.

Source

pub fn push_str(&mut self, s: &str) -> Result<(), Utf8Error>

Pushes a &str.

Returns an error if there is a pending incomplete character. The builder state is unchanged in that case.

Source

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

Pushes a char.

Returns an error if there is a pending incomplete character. The builder state is unchanged in that case.

Source

pub fn push_chunk(&mut self, chunk: &[u8]) -> Result<(), Utf8Error>

Pushes a chunk.

The chunk does not have to be complete UTF-8 data, because a character can be split across chunks. Returns an error if the chunk contains invalid data. In that case, the leading valid part of the chunk is still stored, so the builder remains usable. However, if the error happens while completing a pending incomplete character, nothing is stored.

Trait Implementations§

Source§

impl Clone for Utf8Builder

Source§

fn clone(&self) -> Utf8Builder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Utf8Builder

Source§

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

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

impl Default for Utf8Builder

Source§

fn default() -> Utf8Builder

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

impl From<&str> for Utf8Builder

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Utf8Builder

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl Write for Utf8Builder

Writing fails if there is a pending incomplete character.

Source§

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

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

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

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

Auto Trait Implementations§

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