### Heapless `no_std` Base64 Usage Source: https://docs.rs/base64ct/latest/base64ct Example demonstrating heapless Base64 encoding and decoding for `no_std` environments. ```APIDOC ## Heapless `no_std` Usage This section demonstrates how to perform Base64 encoding and decoding in a heapless `no_std` environment using pre-allocated buffers. ### Method `Base64::encode` and `Base64::decode` ### Parameters * **bytes** (slice of u8) - The byte slice to encode. * **enc_buf** (mutable slice of u8) - Buffer to store the encoded output. * **encoded** (string slice) - The Base64 encoded string to decode. * **dec_buf** (mutable slice of u8) - Buffer to store the decoded output. ### Request Example ```rust use base64ct::{Base64, Encoding}; const BUF_SIZE: usize = 128; let bytes = b"example bytestring!"; assert!(Base64::encoded_len(bytes) <= BUF_SIZE); let mut enc_buf = [0u8; BUF_SIZE]; let encoded = Base64::encode(bytes, &mut enc_buf).unwrap(); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let mut dec_buf = [0u8; BUF_SIZE]; let decoded = Base64::decode(encoded, &mut dec_buf).unwrap(); assert_eq!(decoded, bytes); ``` ### Response Example * **encoded** (string slice) - The Base64 encoded string. * **decoded** (slice of u8) - The decoded byte slice. ``` -------------------------------- ### Base64 Encoding and Decoding with Alloc Source: https://docs.rs/base64ct/latest/base64ct Example demonstrating how to encode and decode byte slices using the Base64 type with the `alloc` crate feature enabled. ```APIDOC ## Usage with Alloc This section shows how to use the `base64ct` crate for encoding and decoding when the `alloc` crate feature is enabled, allowing for heap allocations. ### Method `Base64::encode_string` and `Base64::decode_vec` ### Parameters * **bytes** (slice of u8) - The byte slice to encode. * **encoded** (string) - The Base64 encoded string. ### Request Example ```rust use base64ct::{Base64, Encoding}; let bytes = b"example bytestring!"; let encoded = Base64::encode_string(bytes); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let decoded = Base64::decode_vec(&encoded).unwrap(); assert_eq!(decoded, bytes); ``` ### Response Example * **encoded** (string) - The Base64 encoded string. * **decoded** (slice of u8) - The decoded byte slice. ``` -------------------------------- ### Finish Encoding - Get Base64 String Source: https://docs.rs/base64ct/latest/src/base64ct/encoder.rs.html Finalizes the Base64 encoding process, including any data in the block buffer, and returns the resulting Base64 encoded data as a string slice. Errors if the output buffer is not valid UTF-8. ```rust pub fn finish(self) -> Result<&'o str, Error> { self.finish_with_remaining().map(|(base64, _)| base64) } ``` -------------------------------- ### Get Base64 Encoded Length Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64UrlUnpadded.html Calculates the required buffer size for Base64 encoding of given bytes. No setup is required. ```rust fn encoded_len(bytes: &[u8]) -> usize ``` -------------------------------- ### Get Remaining Decoded Data Length Source: https://docs.rs/base64ct/latest/base64ct/struct.Decoder.html Gets the length of the remaining data after Base64 decoding. This length decreases each time data is decoded. ```rust pub fn remaining_len(&self) -> usize ``` -------------------------------- ### Get Current Write Position Source: https://docs.rs/base64ct/latest/src/base64ct/encoder.rs.html Returns the current position of the write cursor within the output buffer. ```rust pub fn position(&self) -> usize { self.position } ``` -------------------------------- ### Base64 Encoding and Decoding with Alloc Source: https://docs.rs/base64ct/latest/src/base64ct/lib.rs.html Demonstrates encoding a byte slice to a String and decoding a String back to a Vec using the `alloc` feature. Ensure the `alloc` feature is enabled for this functionality. ```rust #![cfg(feature = "alloc")] # { use base64ct::{Base64, Encoding}; let bytes = b"example bytestring!"; let encoded = Base64::encode_string(bytes); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let decoded = Base64::decode_vec(&encoded).unwrap(); assert_eq!(decoded, bytes); # } ``` -------------------------------- ### Heapless `no_std` Base64 Encoding and Decoding Source: https://docs.rs/base64ct Illustrates how to perform Base64 encoding and decoding in a `no_std` environment using pre-allocated buffers. ```APIDOC ## Heapless `no_std` Usage This example demonstrates Base64 encoding and decoding in a `no_std` environment, utilizing pre-allocated buffers to avoid dynamic memory allocation. ### Method `Base64::encode` and `Base64::decode` ### Request Example ```rust use base64ct::{Base64, Encoding}; const BUF_SIZE: usize = 128; let bytes = b"example bytestring!"; assert!(Base64::encoded_len(bytes) <= BUF_SIZE); let mut enc_buf = [0u8; BUF_SIZE]; let encoded = Base64::encode(bytes, &mut enc_buf).unwrap(); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let mut dec_buf = [0u8; BUF_SIZE]; let decoded = Base64::decode(encoded, &mut dec_buf).unwrap(); assert_eq!(decoded, bytes); ``` ### Response Example ```rust // The encoded string is "ZXhhbXBsZSBieXRlc3RyaW5nIQ==" // The decoded bytes match the original input bytes. ``` ``` -------------------------------- ### Get LineEnding Length Source: https://docs.rs/base64ct/latest/base64ct/enum.LineEnding.html Returns the encoded length of a LineEnding variant. This can be used to pre-allocate buffers or calculate offsets. ```rust pub fn len(self) -> usize ``` -------------------------------- ### Base64 Encoding and Decoding with Alloc Source: https://docs.rs/base64ct Demonstrates how to encode and decode byte slices to and from strings using the `Base64` type and the `Encoding` trait when the `alloc` crate feature is enabled. ```APIDOC ## Usage with Alloc This example shows how to use the `base64ct` crate for encoding and decoding when heap allocations are permitted (e.g., in standard `std` environments). ### Method `Base64::encode_string` and `Base64::decode_vec` ### Request Example ```rust use base64ct::{Base64, Encoding}; let bytes = b"example bytestring!"; let encoded = Base64::encode_string(bytes); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let decoded = Base64::decode_vec(&encoded).unwrap(); assert_eq!(decoded, bytes); ``` ### Response Example ```rust // The encoded string is "ZXhhbXBsZSBieXRlc3RyaW5nIQ==" // The decoded bytes match the original input bytes. ``` ``` -------------------------------- ### Get LineEnding as Bytes Source: https://docs.rs/base64ct/latest/base64ct/enum.LineEnding.html Retrieves the byte serialization of a LineEnding variant. This is useful for direct manipulation or comparison of line ending bytes. ```rust pub fn as_bytes(self) -> &'static [u8] ``` -------------------------------- ### Generic Conversions and Ownership Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64ShaCrypt.html Documentation for generic conversion traits like `From`, `Into`, `ToOwned`, `TryFrom`, and `TryInto` as implemented within the crate. ```APIDOC ## Trait: From ### Description Provides a way to convert a value of type `T` into another value of type `T`. ### Method N/A (Trait implementation) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A #### fn from(t: T) -> T Returns the argument unchanged. ``` ```APIDOC ## Trait: Into ### Description Provides a way to convert a value of type `T` into a value of type `U`, where `U` implements `From`. ### Method N/A (Trait implementation) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A #### fn into(self) -> U Calls `U::from(self)`. This conversion's behavior is determined by the `From for U` implementation. ``` ```APIDOC ## Trait: ToOwned ### Description Defines methods for creating owned data from borrowed data. ### Method N/A (Trait implementation) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A #### type Owned = T The type that represents owned data. #### fn to_owned(&self) -> T Creates owned data from borrowed data, typically by cloning. #### fn clone_into(&self, target: &mut T) Uses borrowed data to replace owned data, usually by cloning. ``` ```APIDOC ## Trait: TryFrom ### Description Provides a way to perform fallible conversions from a value of type `U` into a value of type `T`. ### Method N/A (Trait implementation) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A #### type Error = Infallible The type returned in the event of a conversion error (infallible means no error is expected). #### fn try_from(value: U) -> Result>::Error> Performs the conversion. Returns a `Result` which is `Ok(T)` on success or `Err(Error)` on failure. ``` ```APIDOC ## Trait: TryInto ### Description Provides a way to perform fallible conversions from a value of type `T` into a value of type `U`, where `U` implements `TryFrom`. ### Method N/A (Trait implementation) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A #### type Error = >::Error The type returned in the event of a conversion error. #### fn try_into(self) -> Result>::Error> Performs the conversion. Returns a `Result` which is `Ok(U)` on success or `Err(Error)` on failure. ``` -------------------------------- ### Encoder Initialization Source: https://docs.rs/base64ct/latest/src/base64ct/encoder.rs.html Methods to initialize a new Base64 encoder, either with or without line wrapping. ```APIDOC ## Encoder::new ### Description Creates a new encoder which writes output to the given byte slice without line wrapping. ### Parameters - **output** (&mut [u8]) - Required - The mutable byte slice to write encoded data into. ## Encoder::new_wrapped ### Description Creates a new encoder which writes line-wrapped output to the given byte slice. ### Parameters - **output** (&mut [u8]) - Required - The mutable byte slice to write encoded data into. - **width** (usize) - Required - The line width interval for wrapping (minimum 4). - **ending** (LineEnding) - Required - The line ending configuration. ``` -------------------------------- ### Encode and Decode with Alloc Source: https://docs.rs/base64ct/latest/base64ct/index.html Demonstrates heap-allocated encoding and decoding using the 'alloc' feature. ```rust use base64ct::{Base64, Encoding}; let bytes = b"example bytestring!"; let encoded = Base64::encode_string(bytes); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let decoded = Base64::decode_vec(&encoded).unwrap(); assert_eq!(decoded, bytes); ``` -------------------------------- ### Finish Encoding - Get String and Remaining Buffer Source: https://docs.rs/base64ct/latest/src/base64ct/encoder.rs.html Finalizes the Base64 encoding process, including any data in the block buffer. Returns a tuple containing the Base64 encoded string slice and a mutable slice of the remaining unused space in the output buffer. Errors if the output buffer is not valid UTF-8. ```rust pub fn finish_with_remaining(mut self) -> Result<(&'o str, &'o mut [u8]), Error> { if !self.block_buffer.is_empty() { let buffer_len = self.block_buffer.position; let block = self.block_buffer.bytes; self.perform_encode(&block[..buffer_len])?; } let (base64, remaining) = self.output.split_at_mut(self.position); Ok((str::from_utf8(base64)?, remaining)) } ``` -------------------------------- ### Decoder Initialization Source: https://docs.rs/base64ct/latest/base64ct/struct.Decoder.html Methods to initialize a new Decoder instance for either contiguous or line-wrapped Base64 data. ```APIDOC ## Decoder::new ### Description Create a new decoder for a byte slice containing contiguous (non-newline-delimited) Base64-encoded data. ### Parameters - **input** (&[u8]) - Required - Byte slice containing Base64 data. ### Response - **Result** - Returns the decoder instance or an Error::InvalidLength if the input is empty. ## Decoder::new_wrapped ### Description Create a new decoder for a byte slice containing Base64 which line wraps at the given line length. ### Parameters - **input** (&[u8]) - Required - Byte slice containing Base64 data. - **line_width** (usize) - Required - The width at which the input is wrapped. ### Response - **Result** - Returns the decoder instance or an Error::InvalidLength if input is empty or line_width is zero. ``` -------------------------------- ### Heapless no_std Usage Source: https://docs.rs/base64ct/latest/base64ct/index.html Demonstrates encoding and decoding using fixed-size buffers for no_std environments. ```rust use base64ct::{Base64, Encoding}; const BUF_SIZE: usize = 128; let bytes = b"example bytestring!"; assert!(Base64::encoded_len(bytes) <= BUF_SIZE); let mut enc_buf = [0u8; BUF_SIZE]; let encoded = Base64::encode(bytes, &mut enc_buf).unwrap(); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let mut dec_buf = [0u8; BUF_SIZE]; let decoded = Base64::decode(encoded, &mut dec_buf).unwrap(); assert_eq!(decoded, bytes); ``` -------------------------------- ### LineEnding Enum and Constants Source: https://docs.rs/base64ct/latest/src/base64ct/line_ending.rs.html Defines constants for Carriage Return (CR) and Line Feed (LF) byte values, and the LineEnding enum which represents different line ending types (CR, LF, CRLF). Includes implementation for default line ending based on the operating system and methods to get byte serialization and length. ```APIDOC ## Constants ### `CHAR_CR` - **Type**: `u8` - **Value**: `0x0d` - **Description**: Represents the Carriage Return character. ### `CHAR_LF` - **Type**: `u8` - **Value**: `0x0a` - **Description**: Represents the Line Feed character. ## Enum: `LineEnding` ### Description Represents variants of newline characters that can be used with Base64 encoding. Use `LineEnding::default()` to get an appropriate line ending for the current operating system. ### Variants - **`CR`**: Carriage return: `\r` (Pre-OS X Macintosh) - **`LF`**: Line feed: `\n` (Unix OSes) - **`CRLF`**: Carriage return + line feed: `\r\n` (Windows) ### `Default` Implementation - **Description**: Provides a default `LineEnding` based on the target operating system. - **Behavior**: Returns `LineEnding::CRLF` on Windows, and `LineEnding::LF` on other operating systems. ### Methods #### `as_bytes(self) -> &'static [u8]` - **Description**: Gets the byte serialization of this `LineEnding`. - **Returns**: A static byte slice representing the line ending. #### `len(self) -> usize` - **Description**: Gets the encoded length of this `LineEnding`. - **Returns**: The number of bytes in the line ending serialization. ``` -------------------------------- ### Initialize Encoder - With Line Wrapping Source: https://docs.rs/base64ct/latest/src/base64ct/encoder.rs.html Creates a new encoder that writes line-wrapped Base64 output to the given byte slice. Output is wrapped at the specified width using the provided line ending. The minimum allowed line width is 4. ```rust pub fn new_wrapped( output: &'o mut [u8], width: usize, ending: LineEnding, ) -> Result { let mut encoder = Self::new(output)?; encoder.line_wrapper = Some(LineWrapper::new(width, ending)?); Ok(encoder) } ``` -------------------------------- ### Heapless Base64 Encoding and Decoding (no_std) Source: https://docs.rs/base64ct/latest/src/base64ct/lib.rs.html Shows how to perform Base64 encoding and decoding in a `no_std` environment using pre-allocated buffers. This method is suitable for embedded systems or environments without dynamic memory allocation. ```rust use base64ct::{Base64, Encoding}; const BUF_SIZE: usize = 128; let bytes = b"example bytestring!"; assert!(Base64::encoded_len(bytes) <= BUF_SIZE); let mut enc_buf = [0u8; BUF_SIZE]; let encoded = Base64::encode(bytes, &mut enc_buf).unwrap(); assert_eq!(encoded, "ZXhhbXBsZSBieXRlc3RyaW5nIQ=="); let mut dec_buf = [0u8; BUF_SIZE]; let decoded = Base64::decode(encoded, &mut dec_buf).unwrap(); assert_eq!(decoded, bytes); ``` -------------------------------- ### TryInto Conversion Source: https://docs.rs/base64ct/latest/base64ct/enum.LineEnding.html Documentation for the try_into conversion method and its associated error type. ```APIDOC ## fn try_into(self) -> Result>::Error> ### Description Performs the conversion from the current instance into type U. ### Error Handling - **Error** (type) - The type returned in the event of a conversion error, defined as >::Error. ``` -------------------------------- ### PartialEq Implementation for Base64Pbkdf2 Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Pbkdf2.html Enables equality comparisons for Base64Pbkdf2 instances. ```APIDOC ### impl PartialEq for Base64Pbkdf2 #### fn eq(&self, other: &Base64Pbkdf2) -> bool Tests for `self` and `other` values to be equal. #### fn ne(&self, other: &Rhs) -> bool Tests for `!=`. ``` -------------------------------- ### CloneToUninit Trait Implementation (Nightly) Source: https://docs.rs/base64ct/latest/base64ct/enum.LineEnding.html An experimental nightly-only API for performing copy-assignment to uninitialized memory. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### Clone Implementation for Base64Pbkdf2 Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Pbkdf2.html Provides methods for cloning Base64Pbkdf2 instances. ```APIDOC ### impl Clone for Base64Pbkdf2 #### fn clone(&self) -> Base64Pbkdf2 Returns a duplicate of the value. #### fn clone_from(&mut self, source: &Self) Performs copy-assignment from `source`. ``` -------------------------------- ### Implement Default for Base64Crypt Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Crypt.html Provides the default constructor for Base64Crypt, returning its default value. ```rust fn default() -> Base64Crypt ``` -------------------------------- ### Initialize Decoder for Line-Wrapped Base64 Source: https://docs.rs/base64ct/latest/src/base64ct/decoder.rs.html Creates a new decoder for Base64 data that wraps at a specified line length. Trailing newlines are not supported. Returns an error if the input is empty or the line width is zero. ```rust pub fn new_wrapped(input: &'i [u8], line_width: usize) -> Result { let line_reader = LineReader::new_wrapped(input, line_width)?; let remaining_len = line_reader.decoded_len::()?; Ok(Self { line: Line::default(), line_reader, remaining_len, block_buffer: BlockBuffer::default(), encoding: PhantomData, }) } ``` -------------------------------- ### Create a 'By Reference' Adapter (std feature) Source: https://docs.rs/base64ct/latest/base64ct/struct.Decoder.html Available on `crate feature 'std'` only. Creates a 'by reference' adapter for the `Read` instance. ```rust fn by_ref(&mut self) -> &mut Self ``` -------------------------------- ### PartialOrd Implementation for Base64Pbkdf2 Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Pbkdf2.html Provides partial ordering comparisons for Base64Pbkdf2 instances. ```APIDOC ### impl PartialOrd for Base64Pbkdf2 #### fn partial_cmp(&self, other: &Base64Pbkdf2) -> Option This method returns an ordering between `self` and `other` values if one exists. #### fn lt(&self, other: &Rhs) -> bool Tests less than. #### fn le(&self, other: &Rhs) -> bool Tests less than or equal to. #### fn gt(&self, other: &Rhs) -> bool Tests greater than. #### fn ge(&self, other: &Rhs) -> bool Tests greater than or equal to. ``` -------------------------------- ### Initialize Encoder - No Line Wrapping Source: https://docs.rs/base64ct/latest/src/base64ct/encoder.rs.html Creates a new encoder that writes output directly to the provided byte slice without line wrapping. The output buffer must not be empty. ```rust pub fn new(output: &'o mut [u8]) -> Result { if output.is_empty() { return Err(InvalidLength); } Ok(Self { output, position: 0, block_buffer: BlockBuffer::default(), line_wrapper: None, encoding: PhantomData, }) } ``` -------------------------------- ### Implement PartialEq for Base64Bcrypt Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Bcrypt.html Implements equality comparison (`eq` and `ne`) for Base64Bcrypt, enabling checks for value equality. ```rust impl PartialEq for Base64Bcrypt fn eq(&self, other: &Base64Bcrypt) -> bool ``` ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### Identity Conversion (From/Into) Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Pbkdf2.html Demonstrates the identity conversion where a value is converted into itself using From and Into traits. ```APIDOC ## impl From for T ### Description Returns the argument unchanged. ### Method N/A (Implementation) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## impl Into for T where U: From, ### Description Calls `U::from(self)`. That is, this conversion is whatever the implementation of `From for U` chooses to do. ### Method N/A (Implementation) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Debug Implementation for Base64Pbkdf2 Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Pbkdf2.html Allows Base64Pbkdf2 instances to be formatted for debugging. ```APIDOC ### impl Debug for Base64Pbkdf2 #### fn fmt(&self, f: &mut Formatter<'_>) -> Result Formats the value using the given formatter. ``` -------------------------------- ### Ord Implementation for Base64Pbkdf2 Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Pbkdf2.html Provides ordering comparisons for Base64Pbkdf2 instances. ```APIDOC ### impl Ord for Base64Pbkdf2 #### fn cmp(&self, other: &Base64Pbkdf2) -> Ordering This method returns an `Ordering` between `self` and `other`. #### fn max(self, other: Self) -> Self Compares and returns the maximum of two values. #### fn min(self, other: Self) -> Self Compares and returns the minimum of two values. #### fn clamp(self, min: Self, max: Self) -> Self Restrict a value to a certain interval. ``` -------------------------------- ### Default Implementation for Base64Pbkdf2 Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Pbkdf2.html Provides a default value for Base64Pbkdf2. ```APIDOC ### impl Default for Base64Pbkdf2 #### fn default() -> Base64Pbkdf2 Returns the “default value” for a type. ``` -------------------------------- ### Implement CloneToUninit for T (Nightly) Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Crypt.html Provides an experimental nightly-only API for cloning a value into uninitialized memory. Requires the T: Clone trait bound. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### Default LineEnding Implementation Source: https://docs.rs/base64ct/latest/base64ct/enum.LineEnding.html Provides the default line ending for the current operating system. This ensures cross-platform compatibility. ```rust fn default() -> LineEnding ``` -------------------------------- ### Encoder API Source: https://docs.rs/base64ct/latest/base64ct/struct.Encoder.html Methods for initializing and using the stateful Base64 encoder. ```APIDOC ## Encoder ### Description Stateful Base64 encoder with support for buffered, incremental encoding. ### Methods #### new(output: &'o mut [u8]) -> Result Create a new encoder which writes output to the given byte slice. Output is not line-wrapped. #### new_wrapped(output: &'o mut [u8], width: usize, ending: LineEnding) -> Result Create a new encoder which writes line-wrapped output to the given byte slice. Minimum allowed line width is 4. #### encode(&mut self, input: &[u8]) -> Result<(), Error> Encode the provided buffer as Base64, writing it to the output buffer. #### position(&self) -> usize Get the position inside of the output buffer where the write cursor is currently located. #### finish(self) -> Result<&'o str, Error> Finish encoding data, returning the resulting Base64 as a `str`. #### finish_with_remaining(self) -> Result<(&'o str, &'o mut [u8]), Error> Finish encoding data, returning the resulting Base64 as a `str` along with the remaining space in the output buffer. ``` -------------------------------- ### Implement CloneFrom for Base64Crypt Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Crypt.html Provides the clone_from method for the Base64Crypt struct, enabling copy-assignment from another instance. ```rust fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### Hash Implementation for Base64Pbkdf2 Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Pbkdf2.html Enables hashing of Base64Pbkdf2 instances. ```APIDOC ### impl Hash for Base64Pbkdf2 #### fn hash<__H: Hasher>(&self, state: &mut __H) Feeds this value into the given `Hasher`. #### fn hash_slice(data: &[Self], state: &mut H) where H: Hasher, Feeds a slice of this type into the given `Hasher`. ``` -------------------------------- ### Implement PartialOrd for Base64Bcrypt Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Bcrypt.html Provides partial ordering comparisons for Base64Bcrypt instances, including `partial_cmp`, `lt`, `le`, `gt`, and `ge` methods. ```rust impl PartialOrd for Base64Bcrypt fn partial_cmp(&self, other: &Base64Bcrypt) -> Option ``` ```rust fn lt(&self, other: &Rhs) -> bool ``` ```rust fn le(&self, other: &Rhs) -> bool ``` ```rust fn gt(&self, other: &Rhs) -> bool ``` ```rust fn ge(&self, other: &Rhs) -> bool ``` -------------------------------- ### LineWrapper Implementation Source: https://docs.rs/base64ct/latest/src/base64ct/encoder.rs.html Handles wrapping Base64 output at a specified line width with configurable line endings. ```rust /// Helper for wrapping Base64 at a given line width. #[derive(Debug)] struct LineWrapper { /// Number of bytes remaining in the current line. remaining: usize, /// Column at which Base64 should be wrapped. width: usize, /// Newline characters to use at the end of each line. ending: LineEnding, } impl LineWrapper { /// Create a new linewrapper. fn new(width: usize, ending: LineEnding) -> Result { if width < MIN_LINE_WIDTH { return Err(InvalidLength); } Ok(Self { remaining: width, width, ending, }) } /// Wrap the number of blocks to encode near/at EOL. fn wrap_blocks(&self, blocks: &mut usize) -> Result<(), Error> { if blocks.checked_mul(4).ok_or(InvalidLength)? >= self.remaining { *blocks = self.remaining / 4; } Ok(()) } /// Insert newlines into the output buffer as needed. fn insert_newlines(&mut self, mut buffer: &mut [u8], len: &mut usize) -> Result<(), Error> { let mut buffer_len = *len; if buffer_len <= self.remaining { self.remaining = self .remaining .checked_sub(buffer_len) .ok_or(InvalidLength)?; return Ok(()); } buffer = &mut buffer[self.remaining..]; buffer_len = buffer_len .checked_sub(self.remaining) .ok_or(InvalidLength)?; // The `wrap_blocks` function should ensure the buffer is no larger than a Base64 block debug_assert!(buffer_len <= 4, "buffer too long: {buffer_len}"); // Ensure space in buffer to add newlines let buffer_end = buffer_len .checked_add(self.ending.len()) .ok_or(InvalidLength)?; if buffer_end >= buffer.len() { ``` -------------------------------- ### Trait Conversions and Ownership Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64.html Documentation for standard Rust traits used for type conversion and data ownership management. ```APIDOC ## Into ### Description Converts a value into another type U by calling U::from(self). ### Method fn into(self) -> U --- ## ToOwned ### Description Creates owned data from borrowed data, usually by cloning. ### Associated Types - **Owned** (T) - The resulting type after obtaining ownership. ### Methods - **to_owned(&self) -> T** - Creates owned data from borrowed data. - **clone_into(&self, target: &mut T)** - Uses borrowed data to replace owned data. --- ## TryFrom ### Description Performs a fallible conversion from type U to type T. ### Associated Types - **Error** (Infallible) - The type returned in the event of a conversion error. ### Methods - **try_from(value: U) -> Result>::Error>** - Performs the conversion. --- ## TryInto ### Description Performs a fallible conversion from type T to type U. ### Associated Types - **Error** (>::Error) - The type returned in the event of a conversion error. ### Methods - **try_into(self) -> Result>::Error>** - Performs the conversion. ``` -------------------------------- ### Base64Unpadded Trait Implementations Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Unpadded.html Overview of the trait implementations available for the Base64Unpadded struct. ```APIDOC ## Trait Implementations ### impl Clone for Base64Unpadded #### fn clone(&self) -> Base64Unpadded Returns a duplicate of the value. #### fn clone_from(&mut self, source: &Self) Performs copy-assignment from `source`. ### impl Debug for Base64Unpadded #### fn fmt(&self, f: &mut Formatter<'_>) -> Result Formats the value using the given formatter. ### impl Default for Base64Unpadded #### fn default() -> Base64Unpadded Returns the “default value” for a type. ### impl Hash for Base64Unpadded #### fn hash<__H: Hasher>(&self, state: &mut __H) Feeds this value into the given `Hasher`. #### fn hash_slice(data: &[Self], state: &mut H) where H: Hasher, Self: Sized, Feeds a slice of this type into the given `Hasher`. ### impl Ord for Base64Unpadded #### fn cmp(&self, other: &Base64Unpadded) -> Ordering This method returns an `Ordering` between `self` and `other`. #### fn max(self, other: Self) -> Self where Self: Sized, Compares and returns the maximum of two values. #### fn min(self, other: Self) -> Self where Self: Sized, Compares and returns the minimum of two values. #### fn clamp(self, min: Self, max: Self) -> Self where Self: Sized, Restrict a value to a certain interval. ### impl PartialEq for Base64Unpadded #### fn eq(&self, other: &Base64Unpadded) -> bool Tests for `self` and `other` values to be equal, and is used by `==`. #### fn ne(&self, other: &Rhs) -> bool Tests for `!=`. ### impl PartialOrd for Base64Unpadded #### fn partial_cmp(&self, other: &Base64Unpadded) -> Option This method returns an ordering between `self` and `other` values if one exists. #### fn lt(&self, other: &Rhs) -> bool Tests less than (for `self` and `other`) and is used by the `<` operator. #### fn le(&self, other: &Rhs) -> bool Tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. #### fn gt(&self, other: &Rhs) -> bool Tests greater than (for `self` and `other`) and is used by the `>` operator. #### fn ge(&self, other: &Rhs) -> bool Tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. ### impl Copy for Base64Unpadded ### impl Eq for Base64Unpadded ### impl StructuralPartialEq for Base64Unpadded ``` -------------------------------- ### Any Trait Implementation Source: https://docs.rs/base64ct/latest/base64ct/enum.LineEnding.html Provides runtime type information for any type that implements 'static. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Base64 Encoding and Decoding Structs Source: https://docs.rs/base64ct/latest/base64ct/all.html Overview of the available encoding structs provided by the base64ct crate. ```APIDOC ## Available Encoding Structs ### Description The base64ct crate provides several structs to handle different Base64 encoding standards and variations. ### Structs - **Base64**: Standard Base64 encoding. - **Base64Bcrypt**: Base64 variant for Bcrypt. - **Base64Crypt**: Base64 variant for Crypt. - **Base64Pbkdf2**: Base64 variant for Pbkdf2. - **Base64ShaCrypt**: Base64 variant for ShaCrypt. - **Base64Unpadded**: Standard Base64 without padding. - **Base64Url**: URL-safe Base64 encoding. - **Base64UrlUnpadded**: URL-safe Base64 without padding. ### Traits - **Encoding**: The primary trait implemented by the encoding structs for data transformation. ``` -------------------------------- ### Trait Implementations for Base64Url Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Url.html Details the standard Rust trait implementations for the Base64Url struct, including Clone, Debug, Default, Hash, Ord, PartialEq, and PartialOrd. ```APIDOC ## Trait Implementations for Base64Url ### `impl Clone for Base64Url` - `fn clone(&self) -> Base64Url`: Returns a duplicate of the value. - `fn clone_from(&mut self, source: &Self)`: Performs copy-assignment from `source`. ### `impl Debug for Base64Url` - `fn fmt(&self, f: &mut Formatter<'_>) -> Result`: Formats the value using the given formatter. ### `impl Default for Base64Url` - `fn default() -> Base64Url`: Returns the “default value” for a type. ### `impl Hash for Base64Url` - `fn hash<__H: Hasher>(&self, state: &mut __H)`: Feeds this value into the given `Hasher`. - `fn hash_slice(data: &[Self], state: &mut H)`: Feeds a slice of this type into the given `Hasher`. ### `impl Ord for Base64Url` - `fn cmp(&self, other: &Base64Url) -> Ordering`: Compares `self` and `other`. - `fn max(self, other: Self) -> Self`: Returns the maximum of two values. - `fn min(self, other: Self) -> Self`: Returns the minimum of two values. - `fn clamp(self, min: Self, max: Self) -> Self`: Restricts a value to a certain interval. ### `impl PartialEq for Base64Url` - `fn eq(&self, other: &Base64Url) -> bool`: Tests for equality. - `fn ne(&self, other: &Rhs) -> bool`: Tests for inequality. ### `impl PartialOrd for Base64Url` - `fn partial_cmp(&self, other: &Base64Url) -> Option`: Returns an ordering between `self` and `other` if one exists. - `fn lt(&self, other: &Rhs) -> bool`: Tests less than. - `fn le(&self, other: &Rhs) -> bool`: Tests less than or equal to. - `fn gt(&self, other: &Rhs) -> bool`: Tests greater than. - `fn ge(&self, other: &Rhs) -> bool`: Tests greater than or equal to. ### `impl Copy for Base64Url` ### `impl Eq for Base64Url` ### `impl StructuralPartialEq for Base64Url` ``` -------------------------------- ### TryFrom Conversion Source: https://docs.rs/base64ct/latest/base64ct/enum.LineEnding.html Documentation for the try_from conversion method used to perform fallible type conversions. ```APIDOC ## fn try_from(value: U) -> Result>::Error> ### Description Performs the conversion from type U to type T. ### Parameters - **value** (U) - Required - The input value to convert. ``` -------------------------------- ### Blanket Implementations Source: https://docs.rs/base64ct/latest/base64ct/struct.Decoder.html Standard library blanket implementations available for types within the rs_base64ct crate. ```APIDOC ## Blanket Implementations ### Description Common Rust standard library traits implemented for types in this crate. ### Traits - **Any**: Provides `type_id`. - **Borrow**: Provides `borrow`. - **BorrowMut**: Provides `borrow_mut`. - **CloneToUninit**: Provides `clone_to_uninit` (Nightly). - **From**: Provides `from`. - **Into**: Provides `into`. - **ToOwned**: Provides `to_owned` and `clone_into`. - **TryFrom**: Provides `try_from`. - **TryInto**: Provides `try_into`. ``` -------------------------------- ### Implement Clone for Base64Bcrypt Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Bcrypt.html Provides the `clone` and `clone_from` methods for the Base64Bcrypt struct, allowing for duplication and copy-assignment. ```rust impl Clone for Base64Bcrypt fn clone(&self) -> Base64Bcrypt ``` ```rust fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### Base64 Encoding/Decoding Traits Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Unpadded.html Demonstrates the use of the `Encoding` trait for Base64 operations. ```APIDOC ### impl Encoding for T where T: Alphabet, #### fn decode(src: impl AsRef<[u8]>, dst: &mut [u8]) -> Result<&[u8], Error> Decode a Base64 string into the provided destination buffer. #### fn decode_in_place(buf: &mut [u8]) -> Result<&[u8], InvalidEncodingError> Decode a Base64 string in-place. #### fn decode_vec(input: &str) -> Result, Error> Available on **crate feature`alloc`** only. Decode a Base64 string into a byte vector. #### fn encode<'a>( src: &[u8], dst: &'a mut [u8], ) -> Result<&'a str, InvalidLengthError> Encode the input byte slice as Base64. #### fn encode_string(input: &[u8]) -> String Available on **crate feature`alloc`** only. Encode input byte slice into a `String` containing Base64. ``` -------------------------------- ### Base64 Encoding Unit Tests Source: https://docs.rs/base64ct/latest/src/base64ct/encoder.rs.html Test cases for padded and unpadded base64 encoding, including multiline scenarios. ```rust 306#[cfg(test)] 307#[allow(clippy::unwrap_used)] 308mod tests { 309 use crate::{Base64, Base64Unpadded, Encoder, LineEnding, alphabet::Alphabet, test_vectors::*}; 310 311 #[test] 312 fn encode_padded() { 313 encode_test::(PADDED_BIN, PADDED_BASE64, None); 314 } 315 316 #[test] 317 fn encode_unpadded() { 318 encode_test::(UNPADDED_BIN, UNPADDED_BASE64, None); 319 } 320 321 #[test] 322 fn encode_multiline_padded() { 323 encode_test::(MULTILINE_PADDED_BIN, MULTILINE_PADDED_BASE64, Some(70)); 324 } 325 326 #[test] 327 fn encode_multiline_unpadded() { 328 encode_test::(MULTILINE_UNPADDED_BIN, MULTILINE_UNPADDED_BASE64, Some(70)); 329 } 330 331 #[test] 332 fn no_trailing_newline_when_aligned() { 333 let mut buffer = [0u8; 64]; 334 let mut encoder = Encoder::::new_wrapped(&mut buffer, 64, LineEnding::LF).unwrap(); 335 encoder.encode(&[0u8; 48]).unwrap(); 336 337 // Ensure no newline character is present in this case 338 assert_eq!( 339 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 340 encoder.finish().unwrap() 341 ); 342 } 343 344 /// Core functionality of an encoding test. 345 fn encode_test(input: &[u8], expected: &str, wrapped: Option) { 346 let mut buffer = [0u8; 1024]; 347 348 for chunk_size in 1..input.len() { 349 let mut encoder = match wrapped { 350 Some(line_width) => { 351 Encoder::::new_wrapped(&mut buffer, line_width, LineEnding::LF) 352 } 353 None => Encoder::::new(&mut buffer), 354 } 355 .unwrap(); 356 357 for chunk in input.chunks(chunk_size) { 358 encoder.encode(chunk).unwrap(); 359 } 360 361 assert_eq!(expected, encoder.finish().unwrap()); 362 } 363 } 364} ``` -------------------------------- ### Type Conversion: From and Into Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64UrlUnpadded.html Implements standard Rust type conversions using `From` and `Into` traits. ```APIDOC ## POST /websites/rs_base64ct/convert/from_into ### Description Implements `From` for `T` (returns the argument unchanged) and `Into` for `T` where `U: From` (calls `U::from(self)`). ### Method POST ### Endpoint /websites/rs_base64ct/convert/from_into ### Request Body - **t** (T) - Required - The value to be converted. - **u** (U) - Required - The target type for conversion. ``` -------------------------------- ### Initialize Decoder for Contiguous Base64 Source: https://docs.rs/base64ct/latest/src/base64ct/decoder.rs.html Creates a new decoder for Base64 data that is not line-wrapped. Returns an error if the input buffer is empty. ```rust pub fn new(input: &'i [u8]) -> Result { let line_reader = LineReader::new_unwrapped(input)?; let remaining_len = line_reader.decoded_len::()?; Ok(Self { line: Line::default(), line_reader, remaining_len, block_buffer: BlockBuffer::default(), encoding: PhantomData, }) } ``` -------------------------------- ### PartialEq Implementation for LineEnding Source: https://docs.rs/base64ct/latest/base64ct/enum.LineEnding.html Enables equality comparison between LineEnding values. Used by the `==` operator. ```rust fn eq(&self, other: &LineEnding) -> bool ``` -------------------------------- ### Implement Ord for Base64Bcrypt Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Bcrypt.html Provides ordering comparisons for Base64Bcrypt instances, including `cmp`, `max`, `min`, and `clamp` methods. ```rust impl Ord for Base64Bcrypt fn cmp(&self, other: &Base64Bcrypt) -> Ordering ``` ```rust fn max(self, other: Self) -> Self where Self: Sized, ``` ```rust fn min(self, other: Self) -> Self where Self: Sized, ``` ```rust fn clamp(self, min: Self, max: Self) -> Self where Self: Sized, ``` -------------------------------- ### Implement Default for Base64Bcrypt Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Bcrypt.html Provides a default value for the Base64Bcrypt struct using the `default` function. ```rust impl Default for Base64Bcrypt fn default() -> Base64Bcrypt ``` -------------------------------- ### clone_into Method Source: https://docs.rs/base64ct/latest/base64ct/struct.Base64Bcrypt.html The `clone_into()` method efficiently replaces the contents of an existing owned `target` with a clone of the borrowed data. This can be more performant than dropping the old and creating a new instance. ```rust fn clone_into(&self, target: &mut T); ``` -------------------------------- ### Read Fixed Array of Bytes (nightly) Source: https://docs.rs/base64ct/latest/base64ct/struct.Decoder.html This is a nightly-only experimental API. Reads and returns a fixed array of bytes from the source. ```rust fn read_array(&mut self) -> Result<[u8; N], Error> ``` -------------------------------- ### Manage BlockBuffer for 1-block input Source: https://docs.rs/base64ct/latest/src/base64ct/decoder.rs.html Handles partially decoded Base64 blocks. The buffer size is fixed at 3 bytes. ```rust #[derive(Clone, Default, Debug)] struct BlockBuffer { /// 3 decoded bytes from a 4-byte Base64-encoded input. decoded: [u8; Self::SIZE], /// Length of the buffer. length: usize, /// Position within the buffer. position: usize, } impl BlockBuffer { /// Size of the buffer in bytes. const SIZE: usize = 3; /// Fill the buffer by decoding up to 3 bytes of decoded Base64 input. fn fill(&mut self, decoded_input: &[u8]) -> Result<(), Error> { debug_assert!(self.is_empty()); if decoded_input.len() > Self::SIZE { return Err(InvalidLength); } self.position = 0; self.length = decoded_input.len(); self.decoded[..decoded_input.len()].copy_from_slice(decoded_input); Ok(()) } /// Take a specified number of bytes from the buffer. /// /// Returns as many bytes as possible, or an empty slice if the buffer has /// already been read to completion. fn take(&mut self, mut nbytes: usize) -> Result<&[u8], Error> { debug_assert!(self.position <= self.length); let start_pos = self.position; let remaining_len = self.length.checked_sub(start_pos).ok_or(InvalidLength)?; if nbytes > remaining_len { nbytes = remaining_len; } self.position = self.position.checked_add(nbytes).ok_or(InvalidLength)?; Ok(&self.decoded[start_pos..][..nbytes]) } /// Have all of the bytes in this buffer been consumed? fn is_empty(&self) -> bool { self.position == self.length } } ```