KokaKiwi / rust-hex

A basic crate to encode values to hexadecimal representation. Originally extracted from rustc-serialize.
https://crates.io/crates/hex
Apache License 2.0
201 stars 55 forks source link

Add encode_to_slice #32

Closed Luro02 closed 4 years ago

Luro02 commented 4 years ago

This is the counterpart to decode_to_slice.

Things to consider

Should the function accept a trait as input?

This would be the most generic approach:

pub fn encode_to_slice<T>(input: &T, output: &mut [u8]) -> Result<(), FromHexError>
where
    T: ::core::iter::IntoIter<Item = u8>,

or the same signature, that encode has?

pub fn encode_to_slice<T: AsRef<[u8]>>(input: &T, output: &mut [u8]) -> Result<(), FromHexError>

I personally would prefer to keep the signature, that it has now.

pub fn encode_to_slice(input: &[u8], output: &mut [u8]) -> Result<(), FromHexError>;
KokaKiwi commented 4 years ago

Personally i would prefer having a signature similar to decode_to_slice (and thus encode and decode as well), as in accepting a type implementing AsRef<[u8]>, for consistency.