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,
impl<C> CharsetStringEncoder<C>where
C: CharsetCodec,
Sourcepub fn new(codec: C) -> Self
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.
Sourcepub fn with_policy(
codec: C,
policy: CharsetEncodePolicy,
) -> Result<Self, CharsetEncodeError>
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.
Sourcepub const fn unmappable_action(&self) -> UnmappableAction
pub const fn unmappable_action(&self) -> UnmappableAction
Returns the configured unmappable-character action.
Sourcepub const fn replacement(&self) -> char
pub const fn replacement(&self) -> char
Returns the configured replacement character.
Sourcepub const fn encoder(&self) -> &CharsetEncoder<C>
pub const fn encoder(&self) -> &CharsetEncoder<C>
Returns the wrapped charset encoder.
Sourcepub fn encoder_mut(&mut self) -> &mut CharsetEncoder<C>
pub fn encoder_mut(&mut self) -> &mut CharsetEncoder<C>
Returns a mutable reference to the wrapped charset encoder.
Sourcepub fn into_encoder(self) -> CharsetEncoder<C>
pub fn into_encoder(self) -> CharsetEncoder<C>
Consumes this string encoder and returns the wrapped charset encoder.
Sourcepub fn encode_str(
&mut self,
input: &str,
) -> Result<Vec<C::Unit>, CharsetEncodeError>
pub fn encode_str( &mut self, input: &str, ) -> Result<Vec<C::Unit>, CharsetEncodeError>
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.
Sourcepub fn encode_str_into(
&mut self,
input: &str,
output: &mut [C::Unit],
output_index: usize,
) -> Result<usize, CharsetEncodeError>
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.