Skip to content

Commit

Permalink
chore: move entend method under Extend trait
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyboyQCD committed Nov 15, 2024
1 parent 61fe735 commit 5740506
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions core/string/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,6 @@ impl<D: Copy> JsStringBuilder<D> {
}
}

/// Extends `JsStringBuilder` with the contents of an iterator.
#[inline]
pub fn extend<I: IntoIterator<Item = D>>(&mut self, iter: I) {
let iterator = iter.into_iter();
let (lower_bound, _) = iterator.size_hint();
let require_cap = self.len() + lower_bound;
self.allocate_if_needed(require_cap);
iterator.for_each(|c| self.push(c));
}

/// Similar to [`Vec::reserve`]
///
/// Reserves capacity for at least `additional` more elements to be inserted
Expand Down Expand Up @@ -444,6 +434,17 @@ impl<D: Copy> AddAssign<&[D]> for JsStringBuilder<D> {
}
}

impl<D: Copy> Extend<D> for JsStringBuilder<D> {
#[inline]
fn extend<I: IntoIterator<Item = D>>(&mut self, iter: I) {
let iterator = iter.into_iter();
let (lower_bound, _) = iterator.size_hint();
let require_cap = self.len() + lower_bound;
self.allocate_if_needed(require_cap);
iterator.for_each(|c| self.push(c));
}
}

impl<D: Copy> FromIterator<D> for JsStringBuilder<D> {
fn from_iter<T: IntoIterator<Item = D>>(iter: T) -> Self {
let mut builder = Self::new();
Expand Down Expand Up @@ -634,6 +635,7 @@ impl<'seg, 'ref_str: 'seg> CommonJsStringBuilder<'seg> {
pub fn reserve(&mut self, additional: usize) {
self.segments.reserve(additional);
}

/// Similar to `Vec::reserve_exact`.
///
/// Reserves the minimum capacity for the inner vector.
Expand Down

0 comments on commit 5740506

Please sign in to comment.