1616//! Encode and decode with caller-owned buffers:
1717//!
1818//! ```
19- //! use base64_ng::{STANDARD, encoded_len };
19+ //! use base64_ng::{STANDARD, checked_encoded_len };
2020//!
2121//! let input = b"hello";
22- //! let mut encoded = [0u8; encoded_len(5, true)];
22+ //! const ENCODED_CAPACITY: usize = match checked_encoded_len(5, true) {
23+ //! Some(len) => len,
24+ //! None => panic!("encoded length overflow"),
25+ //! };
26+ //! let mut encoded = [0u8; ENCODED_CAPACITY];
2327//! let encoded_len = STANDARD.encode_slice(input, &mut encoded).unwrap();
2428//! assert_eq!(&encoded[..encoded_len], b"aGVsbG8=");
2529//!
@@ -679,25 +683,22 @@ pub const URL_SAFE_NO_PAD: Engine<UrlSafe, false> = Engine::new();
679683
680684/// Returns the encoded length for an input length and padding policy.
681685///
682- /// # Panics
683- ///
684- /// Panics if the encoded length would overflow `usize`. Use
685- /// [`checked_encoded_len`] when handling untrusted length metadata without an
686- /// actual input slice.
686+ /// This function returns [`EncodeError::LengthOverflow`] instead of panicking.
687+ /// Use [`checked_encoded_len`] when an `Option<usize>` is more convenient.
687688///
688689/// # Examples
689690///
690691/// ```
691692/// use base64_ng::encoded_len;
692693///
693- /// assert_eq!(encoded_len(5, true), 8);
694- /// assert_eq!(encoded_len(5, false), 7);
694+ /// assert_eq!(encoded_len(5, true).unwrap(), 8);
695+ /// assert_eq!(encoded_len(5, false).unwrap(), 7);
696+ /// assert!(encoded_len(usize::MAX, true).is_err());
695697/// ```
696- #[ must_use]
697- pub const fn encoded_len ( input_len : usize , padded : bool ) -> usize {
698+ pub const fn encoded_len ( input_len : usize , padded : bool ) -> Result < usize , EncodeError > {
698699 match checked_encoded_len ( input_len, padded) {
699- Some ( len) => len,
700- None => panic ! ( "encoded base64 length overflows usize" ) ,
700+ Some ( len) => Ok ( len) ,
701+ None => Err ( EncodeError :: LengthOverflow ) ,
701702 }
702703}
703704
@@ -869,8 +870,7 @@ where
869870 }
870871
871872 /// Returns the encoded length for this engine's padding policy.
872- #[ must_use]
873- pub const fn encoded_len ( & self , input_len : usize ) -> usize {
873+ pub const fn encoded_len ( & self , input_len : usize ) -> Result < usize , EncodeError > {
874874 encoded_len ( input_len, PAD )
875875 }
876876
@@ -890,10 +890,10 @@ where
890890
891891 /// Encodes a fixed-size input into a fixed-size output array in const contexts.
892892 ///
893- /// Stable Rust does not yet allow this API to return `[u8; encoded_len(N)]`
894- /// directly. Instead, the caller supplies the output length through the
895- /// destination type and this function panics during const evaluation if the
896- /// length is wrong.
893+ /// Stable Rust does not yet allow this API to return an array whose length
894+ /// is computed from `INPUT_LEN` directly. Instead, the caller supplies the
895+ /// output length through the destination type and this function panics
896+ /// during const evaluation if the length is wrong.
897897 ///
898898 /// # Panics
899899 ///
0 commit comments