- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
          Add  impl AddAssign<char> for Cow<'_, str>
          #66215
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
76a3b55
              ed52372
              342277f
              9b55f6b
              cd2ad6d
              12a32e6
              543a0b6
              ea5f056
              7d87504
              d62e867
              f2ac395
              0704d9b
              5623044
              5d2184b
              47d0179
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -2,7 +2,7 @@ | |
|  | ||
| #![stable(feature = "rust1", since = "1.0.0")] | ||
|  | ||
| use core::cmp::Ordering; | ||
| use core::cmp::{Ordering, max}; | ||
| use core::hash::{Hash, Hasher}; | ||
| use core::ops::{Add, AddAssign, Deref}; | ||
|  | ||
|  | @@ -444,6 +444,17 @@ impl<'a> Add<Cow<'a, str>> for Cow<'a, str> { | |
| } | ||
| } | ||
|  | ||
| #[stable(feature = "cow_str_add_char", since = "1.41.0")] | ||
| impl<'a> Add<char> for Cow<'a, str> { | ||
| type Output = Cow<'a, str>; | ||
|  | ||
| #[inline] | ||
| fn add(mut self, rhs: char) -> Self::Output { | ||
| self += rhs; | ||
| self | ||
| } | ||
| } | ||
|  | ||
| #[stable(feature = "cow_add", since = "1.14.0")] | ||
| impl<'a> AddAssign<&'a str> for Cow<'a, str> { | ||
| fn add_assign(&mut self, rhs: &'a str) { | ||
|  | @@ -479,3 +490,18 @@ impl<'a> AddAssign<Cow<'a, str>> for Cow<'a, str> { | |
| } | ||
| } | ||
| } | ||
|  | ||
| #[stable(feature = "cow_str_add_assign_char", since = "1.41.0")] | ||
| impl AddAssign<char> for Cow<'_, str> { | ||
| fn add_assign(&mut self, rhs: char) { | ||
| if let Cow::Borrowed(lhs) = *self { | ||
| let base_capacity = lhs.len() + rhs.len_utf8(); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't quite right -- the amortized_new_size heuristic expects to receive the currently used capacity (how much is already used) and the requested extra capacity (how much is being added) as separate data points. In this AddAssign impl the currently used capacity is the length of the lhs Cow<str> and how much is being added is the size of the rhs char. Adding these together ahead of time would make the heuristic no longer applicable, or behave not as intended. | ||
| //Attempt amortized memory allocation | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to avoid duplicating this sizing heuristic from the existing amortized_new_size in raw_vec.rs? I thought you had used amortized_new_size successfully in an earlier revision. If we make changes to the heuristic in the future, it should only be in one place. | ||
| let new_optimal_size = max(base_capacity * 2, base_capacity); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look right to me -- we know base_capacity >= 0 so  | ||
| let mut s = String::with_capacity(new_optimal_size); | ||
| s.push_str(lhs); | ||
| *self = Cow::Owned(s); | ||
| } | ||
| self.to_mut().push(rhs); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.