Skip to content

Commit

Permalink
Make most of encode() non-generic to avoid heavy monomorphization c…
Browse files Browse the repository at this point in the history
…osts
  • Loading branch information
jyn514 committed Dec 22, 2021
1 parent d6b3ece commit 1673277
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3268,7 +3268,11 @@ impl Encoding {
/// ```
#[cfg(feature = "alloc")]
pub fn encode<'a>(&'static self, string: impl Into<Cow<'a, str>>) -> (Cow<'a, [u8]>, &'static Encoding, bool) {
let string = string.into();
self.encode_(string.into())
}

/// Non-generic version of `encode`, to avoid monomorphizing a large amount of code many times.
fn encode_<'a>(&'static self, string: Cow<'a, str>) -> (Cow<'a, [u8]>, &'static Encoding, bool) {
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()),
Expand Down

0 comments on commit 1673277

Please sign in to comment.