Skip to content

Commit

Permalink
Merge pull request #126 from frol/feature/relax-Sized-requirements
Browse files Browse the repository at this point in the history
Relaxed ?Sized requirement for decode and encode functions
  • Loading branch information
marshallpierce committed Mar 8, 2020
2 parents 15982c1 + a38c0bd commit da57bf5
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
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
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 @@ -71,7 +71,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 @@ -114,11 +114,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 da57bf5

Please sign in to comment.