Skip to content

Commit 0a52494

Browse files
committed
Auto merge of #31335 - SimonSapin:ascii-into, r=alexcrichton
The default implementations (with `where Self: Sized`) are so that methods that take `self` by value can exist in a trait that’s implemented for dynamically-sized types (`str` and `[u8]`). CC #27809 (comment) CC @alexcrichton
2 parents a93bb13 + 700ac0e commit 0a52494

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

src/libstd/ascii.rs

+106
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,112 @@ pub trait AsciiExt {
160160
/// ```
161161
#[unstable(feature = "ascii", issue = "27809")]
162162
fn make_ascii_lowercase(&mut self);
163+
164+
/// Converts this type to its ASCII upper case,
165+
/// consuming the value to avoid allocating memory where `to_ascii_uppercase` would.
166+
///
167+
/// See `to_ascii_uppercase` for more information.
168+
///
169+
/// # Examples
170+
///
171+
/// ```
172+
/// #![feature(ascii)]
173+
///
174+
/// use std::ascii::AsciiExt;
175+
///
176+
/// let ascii: String = "a".to_owned();
177+
///
178+
/// let upper = ascii.into_ascii_uppercase();
179+
///
180+
/// assert_eq!(upper, "A");
181+
/// ```
182+
#[unstable(feature = "ascii", issue = "27809")]
183+
fn into_ascii_uppercase(self) -> Self::Owned where Self: Sized {
184+
self.to_ascii_uppercase()
185+
}
186+
187+
/// Converts this type to its ASCII lower case,
188+
/// consuming the value to avoid allocating memory where `to_ascii_lowercase` would.
189+
///
190+
/// See `to_ascii_lowercase` for more information.
191+
///
192+
/// # Examples
193+
///
194+
/// ```
195+
/// #![feature(ascii)]
196+
///
197+
/// use std::ascii::AsciiExt;
198+
///
199+
/// let ascii: String = "A".to_owned();
200+
///
201+
/// let lower = ascii.into_ascii_lowercase();
202+
///
203+
/// assert_eq!(lower, "a");
204+
/// ```
205+
#[unstable(feature = "ascii", issue = "27809")]
206+
fn into_ascii_lowercase(self) -> Self::Owned where Self: Sized {
207+
self.to_ascii_lowercase()
208+
}
209+
}
210+
211+
/// Implement `into_ascii_lowercase` and `into_ascii_uppercase` without memory allocation,
212+
/// defer other methods to `str`.
213+
#[unstable(feature = "ascii", issue = "27809")]
214+
impl AsciiExt for String {
215+
type Owned = Self;
216+
217+
#[inline] fn is_ascii(&self) -> bool { (**self).is_ascii() }
218+
#[inline] fn to_ascii_uppercase(&self) -> Self { (**self).to_ascii_uppercase() }
219+
#[inline] fn to_ascii_lowercase(&self) -> Self { (**self).to_ascii_lowercase() }
220+
#[inline] fn eq_ignore_ascii_case(&self, o: &Self) -> bool { (**self).eq_ignore_ascii_case(o) }
221+
#[inline] fn make_ascii_uppercase(&mut self) { (**self).make_ascii_uppercase() }
222+
#[inline] fn make_ascii_lowercase(&mut self) { (**self).make_ascii_lowercase() }
223+
224+
fn into_ascii_lowercase(mut self) -> Self {
225+
unsafe {
226+
for byte in self.as_mut_vec() {
227+
*byte = byte.to_ascii_lowercase()
228+
}
229+
}
230+
self
231+
}
232+
233+
fn into_ascii_uppercase(mut self) -> Self {
234+
unsafe {
235+
for byte in self.as_mut_vec() {
236+
*byte = byte.to_ascii_uppercase()
237+
}
238+
}
239+
self
240+
}
241+
}
242+
243+
/// Implement `into_ascii_lowercase` and `into_ascii_uppercase` without memory allocation,
244+
/// defer other methods to `[u8]`.
245+
#[unstable(feature = "ascii", issue = "27809")]
246+
impl AsciiExt for Vec<u8> {
247+
type Owned = Self;
248+
249+
#[inline] fn is_ascii(&self) -> bool { (**self).is_ascii() }
250+
#[inline] fn to_ascii_uppercase(&self) -> Self { (**self).to_ascii_uppercase() }
251+
#[inline] fn to_ascii_lowercase(&self) -> Self { (**self).to_ascii_lowercase() }
252+
#[inline] fn eq_ignore_ascii_case(&self, o: &Self) -> bool { (**self).eq_ignore_ascii_case(o) }
253+
#[inline] fn make_ascii_uppercase(&mut self) { (**self).make_ascii_uppercase() }
254+
#[inline] fn make_ascii_lowercase(&mut self) { (**self).make_ascii_lowercase() }
255+
256+
fn into_ascii_lowercase(mut self) -> Self {
257+
for byte in &mut self {
258+
*byte = byte.to_ascii_lowercase()
259+
}
260+
self
261+
}
262+
263+
fn into_ascii_uppercase(mut self) -> Self {
264+
for byte in &mut self {
265+
*byte = byte.to_ascii_uppercase()
266+
}
267+
self
268+
}
163269
}
164270

165271
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)