Skip to main content

CharsetStringEncoder

Struct CharsetStringEncoder 

Source
pub struct CharsetStringEncoder<C>
where C: CharsetCodec,
{ /* private fields */ }
Expand description

Convenience encoder for complete Rust strings.

CharsetStringEncoder owns a CharsetEncoder and adapts &str input to the char slices expected by the transcode layer. Lower-level streaming code should use CharsetEncoder directly; this type is for closed, in-memory string conversions.

§Type Parameters

  • C: Charset codec used to encode Unicode scalar values into target units.

§Examples

use qubit_codec_text::Utf8Codec;
use qubit_io_text::CharsetStringEncoder;

let mut encoder = CharsetStringEncoder::new(Utf8Codec);
let encoded = encoder.encode_str("中文")?;
assert_eq!("中文".as_bytes(), encoded.as_slice());

Implementations§

Source§

impl<C> CharsetStringEncoder<C>
where C: CharsetCodec,

Source

pub fn new(codec: C) -> Self

Creates a string encoder with the charset default replacement policy.

§Parameters
  • codec: Charset codec used to encode output units.
§Returns

Returns a string encoder using UnmappableAction::Replace.

§Panics

Panics with the same conditions as CharsetEncoder::new: replacement mode requires either the default replacement character or the fallback ? to be encodable by codec.

Source

pub fn with_policy( codec: C, policy: CharsetEncodePolicy, ) -> Result<Self, CharsetEncodeError>

Creates a string encoder with an explicit unmappable-input policy.

§Parameters
  • codec: Charset codec used to encode output units.
  • policy: Unmappable-input policy used by the encoder.
§Returns

Returns a string encoder configured with policy.

§Errors

Returns CharsetEncodeError when policy uses replacement and the replacement character cannot be encoded by codec.

Source

pub const fn unmappable_action(&self) -> UnmappableAction

Returns the configured unmappable-character action.

Source

pub const fn replacement(&self) -> char

Returns the configured replacement character.

Source

pub const fn encoder(&self) -> &CharsetEncoder<C>

Returns the wrapped charset encoder.

Source

pub fn encoder_mut(&mut self) -> &mut CharsetEncoder<C>

Returns a mutable reference to the wrapped charset encoder.

Source

pub fn into_encoder(self) -> CharsetEncoder<C>

Consumes this string encoder and returns the wrapped charset encoder.

Source

pub fn encode_str( &mut self, input: &str, ) -> Result<Vec<C::Unit>, CharsetEncodeError>
where C::Unit: Default,

Encodes a complete string into an owned output buffer.

§Parameters
  • input: UTF-8 string whose Unicode scalar values are encoded.
§Returns

Returns an owned buffer containing the encoded units.

§Errors

Returns CharsetEncodeError when sizing or reserving the output, or when encoder reset, transcoding, or finalization fails.

Source

pub fn encode_str_into( &mut self, input: &str, output: &mut [C::Unit], output_index: usize, ) -> Result<usize, CharsetEncodeError>

Encodes a complete string into an existing output slice.

The encoded stream starts at output_index, and the return value is the number of target units written from that index.

§Parameters
  • input: UTF-8 string whose Unicode scalar values are encoded.
  • output: Complete output slice visible to the encoder.
  • output_index: Absolute index where the encoded stream starts.
§Returns

Returns the number of units written to output.

§Errors

Returns CharsetEncodeError when output_index is outside output, the slice cannot hold the complete encoded stream, output sizing overflows, or encoding fails. When the output is too small, the prefix written before the failing character remains in output.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.