Skip to main content

CharsetConverter

Struct CharsetConverter 

Source
pub struct CharsetConverter<D, E>{ /* private fields */ }
Expand description

Converts units encoded with one charset into units encoded with another charset.

The converter owns a CharsetDecoder for the source charset and a CharsetEncoder for the target charset. A decoded character may be kept pending between calls when the target output buffer is full.

§Type Parameters

  • D: Low-level charset codec used by the source decoder.
  • E: Low-level charset codec used by the target encoder.
use qubit_text_codec::{Coder, CharsetDecoder, CharsetEncoder, CharsetConverter, Utf8Codec, Utf16U16Codec};

let mut converter = CharsetConverter::new(
    CharsetDecoder::new(Utf8Codec),
    CharsetEncoder::new(Utf16U16Codec),
);
let mut output = [0_u16; 2];

let progress = converter
    .convert("AB".as_bytes(), 0, &mut output, 0)
    .expect("convert bytes to utf-16");

assert_eq!(2, progress.read());
assert_eq!(2, progress.written());
assert_eq!([65, 66], output);

Implementations§

Source§

impl<D, E> CharsetConverter<D, E>

Source

pub fn from_codecs(source: D, target: E) -> Self

Creates a charset converter from raw source and target codecs.

§Parameters
  • source: Source charset codec.
  • target: Target charset codec.
§Returns

Returns a converter with default decoder and encoder policies.

Source

pub fn new(decoder: CharsetDecoder<D>, encoder: CharsetEncoder<E>) -> Self

Creates a charset converter from a decoder and an encoder.

§Parameters
  • decoder: Decoder configured for the source charset.
  • encoder: Encoder configured for the target charset.
§Returns

Returns a converter that composes the supplied decoder and encoder.

Source

pub const fn decoder(&self) -> &CharsetDecoder<D>

Returns the source decoder.

§Returns

Returns a shared reference to the configured decoder.

Source

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

Returns the target encoder.

§Returns

Returns a shared reference to the configured encoder.

Source

pub fn decoder_mut(&mut self) -> &mut CharsetDecoder<D>

Returns a mutable source decoder.

§Returns

Returns a mutable reference to the configured decoder.

Source

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

Returns a mutable target encoder.

§Returns

Returns a mutable reference to the configured encoder.

Trait Implementations§

Source§

impl<D, E> Clone for CharsetConverter<D, E>

Source§

fn clone(&self) -> CharsetConverter<D, E>

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<D, E> Coder<<D as CharsetCodec>::Unit, <E as CharsetCodec>::Unit> for CharsetConverter<D, E>

Source§

fn max_output_len(&self, input_len: usize) -> Option<usize>

Returns the target-side upper bound for converted output units.

Source§

fn reset(&mut self)

Clears any pending decoded character.

Source§

fn convert( &mut self, input: &[D::Unit], input_index: usize, output: &mut [E::Unit], output_index: usize, ) -> Result<CoderProgress, Self::Error>

Converts source units to target units through the configured decoder and encoder.

Source§

fn finish( &mut self, output: &mut [E::Unit], output_index: usize, ) -> Result<CoderProgress, Self::Error>

Flushes one pending decoded character, if any.

§Parameters
  • output: Complete output slice visible to the converter.
  • output_index: Absolute output index where writing starts.
§Returns

Returns completed progress when no pending character exists. Returns NeedOutput when pending output cannot be flushed due to missing output capacity.

§Errors

Returns CharsetConvertError::Encode when encoding the pending character violates target charset policy.

Source§

type Error = CharsetConvertError

Error reported for semantic conversion failures.
Source§

impl<D, E> Debug for CharsetConverter<D, E>

Source§

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

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

impl<D, E> PartialEq for CharsetConverter<D, E>

Source§

fn eq(&self, other: &CharsetConverter<D, E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<D, E> Eq for CharsetConverter<D, E>
where D: CharsetCodec + Eq, E: CharsetCodec + Eq,

Source§

impl<D, E> StructuralPartialEq for CharsetConverter<D, E>

Auto Trait Implementations§

§

impl<D, E> Freeze for CharsetConverter<D, E>
where D: Freeze, E: Freeze,

§

impl<D, E> RefUnwindSafe for CharsetConverter<D, E>

§

impl<D, E> Send for CharsetConverter<D, E>
where D: Send, E: Send, <E as CharsetCodec>::Unit: Send,

§

impl<D, E> Sync for CharsetConverter<D, E>
where D: Sync, E: Sync, <E as CharsetCodec>::Unit: Sync,

§

impl<D, E> Unpin for CharsetConverter<D, E>
where D: Unpin, E: Unpin, <E as CharsetCodec>::Unit: Unpin,

§

impl<D, E> UnsafeUnpin for CharsetConverter<D, E>
where D: UnsafeUnpin, E: UnsafeUnpin,

§

impl<D, E> UnwindSafe for CharsetConverter<D, E>

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.