Skip to content

Commit

Permalink
Relaxed ?Sized requirement for decode and encode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
frol committed Mar 7, 2020
1 parent a6f7a6e commit a38c0bd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
15 changes: 6 additions & 9 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl error::Error for DecodeError {
///}
///```
#[cfg(any(feature = "alloc", feature = "std", test))]
pub fn decode<T: ?Sized + AsRef<[u8]>>(input: &T) -> Result<Vec<u8>, DecodeError> {
pub fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
decode_config(input, STANDARD)
}

Expand All @@ -102,10 +102,7 @@ pub fn decode<T: ?Sized + AsRef<[u8]>>(input: &T) -> Result<Vec<u8>, DecodeError
///}
///```
#[cfg(any(feature = "alloc", feature = "std", test))]
pub fn decode_config<T: ?Sized + AsRef<[u8]>>(
input: &T,
config: Config,
) -> Result<Vec<u8>, DecodeError> {
pub fn decode_config<T: AsRef<[u8]>>(input: T, config: Config) -> Result<Vec<u8>, DecodeError> {
let mut buffer = Vec::<u8>::with_capacity(input.as_ref().len() * 4 / 3);

decode_config_buf(input, config, &mut buffer).map(|_| buffer)
Expand Down Expand Up @@ -133,8 +130,8 @@ pub fn decode_config<T: ?Sized + AsRef<[u8]>>(
///}
///```
#[cfg(any(feature = "alloc", feature = "std", test))]
pub fn decode_config_buf<T: ?Sized + AsRef<[u8]>>(
input: &T,
pub fn decode_config_buf<T: AsRef<[u8]>>(
input: T,
config: Config,
buffer: &mut Vec<u8>,
) -> Result<(), DecodeError> {
Expand Down Expand Up @@ -169,8 +166,8 @@ pub fn decode_config_buf<T: ?Sized + AsRef<[u8]>>(
/// input, rounded up, or in other words `(input_len + 3) / 4 * 3`.
///
/// If the slice is not large enough, this will panic.
pub fn decode_config_slice<T: ?Sized + AsRef<[u8]>>(
input: &T,
pub fn decode_config_slice<T: AsRef<[u8]>>(
input: T,
config: Config,
output: &mut [u8],
) -> Result<usize, DecodeError> {
Expand Down
12 changes: 4 additions & 8 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::convert::TryInto;
///}
///```
#[cfg(any(feature = "alloc", feature = "std", test))]
pub fn encode<T: ?Sized + AsRef<[u8]>>(input: &T) -> String {
pub fn encode<T: AsRef<[u8]>>(input: T) -> String {
encode_config(input, STANDARD)
}

Expand All @@ -41,7 +41,7 @@ pub fn encode<T: ?Sized + AsRef<[u8]>>(input: &T) -> String {
///}
///```
#[cfg(any(feature = "alloc", feature = "std", test))]
pub fn encode_config<T: ?Sized + AsRef<[u8]>>(input: &T, config: Config) -> String {
pub fn encode_config<T: AsRef<[u8]>>(input: T, config: Config) -> String {
let mut buf = match encoded_size(input.as_ref().len(), config) {
Some(n) => vec![0; n],
None => panic!("integer overflow when calculating buffer size"),
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn encode_config<T: ?Sized + AsRef<[u8]>>(input: &T, config: Config) -> Stri
///}
///```
#[cfg(any(feature = "alloc", feature = "std", test))]
pub fn encode_config_buf<T: ?Sized + AsRef<[u8]>>(input: &T, config: Config, buf: &mut String) {
pub fn encode_config_buf<T: AsRef<[u8]>>(input: T, config: Config, buf: &mut String) {
let input_bytes = input.as_ref();

{
Expand Down Expand Up @@ -115,11 +115,7 @@ pub fn encode_config_buf<T: ?Sized + AsRef<[u8]>>(input: &T, config: Config, buf
/// assert_eq!(s, base64::decode(&buf).unwrap().as_slice());
/// }
/// ```
pub fn encode_config_slice<T: ?Sized + AsRef<[u8]>>(
input: &T,
config: Config,
output: &mut [u8],
) -> usize {
pub fn encode_config_slice<T: AsRef<[u8]>>(input: T, config: Config, output: &mut [u8]) -> usize {
let input_bytes = input.as_ref();

let encoded_size = encoded_size(input_bytes.len(), config)
Expand Down

0 comments on commit a38c0bd

Please sign in to comment.