Skip to content

Commit

Permalink
Allow passing String to Encoding::encode
Browse files Browse the repository at this point in the history
In the common case when converting from UTF8 to UTF8, or the string is all ASCII, this avoids an
extra heap allocation for the caller if they only have a `String` available.
Previously, they would have to call `encoding.encode(&string).into_owned()` to avoid lifetime errors.
  • Loading branch information
jyn514 committed Dec 22, 2021
1 parent b73366a commit d6b3ece
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3257,11 +3257,26 @@ impl Encoding {
///
/// Available to Rust only and only with the `alloc` feature enabled (enabled
/// by default).
///
/// # Examples
///
/// `encode` can be used with either `&str` or `String`.
/// ```
/// let encoding = encoding_rs::UTF_8;
/// let (bytes, output_encoding, saw_unmappable_chars) = encoding.encode("some string");
/// let (bytes, output_encoding, saw_unmappable_chars) = encoding.encode(String::from("some string"));
/// ```
#[cfg(feature = "alloc")]
pub fn encode<'a>(&'static self, string: &'a str) -> (Cow<'a, [u8]>, &'static Encoding, bool) {
pub fn encode<'a>(&'static self, string: impl Into<Cow<'a, str>>) -> (Cow<'a, [u8]>, &'static Encoding, bool) {
let string = string.into();
let to_cow_bytes = |string: Cow<'a, str>| match string {
Cow::Owned(string) => Cow::Owned(string.into_bytes()),
Cow::Borrowed(str) => Cow::Borrowed(str.as_bytes()),
};

let output_encoding = self.output_encoding();
if output_encoding == UTF_8 {
return (Cow::Borrowed(string.as_bytes()), output_encoding, false);
return (to_cow_bytes(string), output_encoding, false);
}
debug_assert!(output_encoding.is_potentially_borrowable());
let bytes = string.as_bytes();
Expand All @@ -3271,7 +3286,7 @@ impl Encoding {
ascii_valid_up_to(bytes)
};
if valid_up_to == bytes.len() {
return (Cow::Borrowed(bytes), output_encoding, false);
return (to_cow_bytes(string), output_encoding, false);
}
let mut encoder = output_encoding.new_encoder();
let mut vec: Vec<u8> = Vec::with_capacity(
Expand Down

0 comments on commit d6b3ece

Please sign in to comment.