Skip to content

Commit 8f24ff2

Browse files
tormolThomas Bahn
authored andcommitted
Make many AsciiChar functions take self by value and not by reference
It doesn't make sense to borrow copyable types. This is a breaking change, but `a.f()` should be unaffected.
1 parent 2ce5fb7 commit 8f24ff2

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

RELEASES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Unreleased
22
==========
33
* Return `FromAsciiError` instead of the input when `AsciiString::from_ascii()` or `into_ascii_string()` fails.
4+
* `AsciiChar::is_*()` and `::as_{byte,char}()` take `self` by value instead of by reference.
45
* Make `AsciiChar` comparable with `char` and `u8`.
56
* Add `AsciiChar::as_printable_char()` and the free functions `caret_encode()` and `caret_decode()`.
67
* Implement some methods from `AsciiExt` and `Error` (which are not in libcore) directly in `no_std` mode:

src/ascii_char.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -305,48 +305,48 @@ impl AsciiChar {
305305

306306
/// Converts an ASCII character into a `u8`.
307307
#[inline]
308-
pub fn as_byte(&self) -> u8 {
309-
*self as u8
308+
pub fn as_byte(self) -> u8 {
309+
self as u8
310310
}
311311

312312
/// Converts an ASCII character into a `char`.
313313
#[inline]
314-
pub fn as_char(&self) -> char {
314+
pub fn as_char(self) -> char {
315315
self.as_byte() as char
316316
}
317317

318318
// the following methods are like ctype, and the implementation is inspired by musl
319319

320320
/// Check if the character is a letter (a-z, A-Z)
321321
#[inline]
322-
pub fn is_alphabetic(&self) -> bool {
322+
pub fn is_alphabetic(self) -> bool {
323323
let c = self.as_byte() | 0b010_0000;// Turns uppercase into lowercase.
324324
c >= b'a' && c <= b'z'
325325
}
326326

327327
/// Check if the character is a number (0-9)
328328
#[inline]
329-
pub fn is_digit(&self) -> bool {
330-
self >= &AsciiChar::_0 && self <= &AsciiChar::_9
329+
pub fn is_digit(self) -> bool {
330+
self >= AsciiChar::_0 && self <= AsciiChar::_9
331331
}
332332

333333
/// Check if the character is a letter or number
334334
#[inline]
335-
pub fn is_alphanumeric(&self) -> bool {
335+
pub fn is_alphanumeric(self) -> bool {
336336
self.is_alphabetic() || self.is_digit()
337337
}
338338

339339
/// Check if the character is a space or horizontal tab
340340
#[inline]
341-
pub fn is_blank(&self) -> bool {
342-
*self == AsciiChar::Space || *self == AsciiChar::Tab
341+
pub fn is_blank(self) -> bool {
342+
self == AsciiChar::Space || self == AsciiChar::Tab
343343
}
344344

345345
/// Check if the character is a ' ', '\t', '\n' or '\r'
346346
#[inline]
347-
pub fn is_whitespace(&self) -> bool {
348-
self.is_blank() || *self == AsciiChar::LineFeed
349-
|| *self == AsciiChar::CarriageReturn
347+
pub fn is_whitespace(self) -> bool {
348+
self.is_blank() || self == AsciiChar::LineFeed
349+
|| self == AsciiChar::CarriageReturn
350350
}
351351

352352
/// Check if the character is a control character
@@ -360,8 +360,8 @@ impl AsciiChar {
360360
/// assert_eq!('\n'.to_ascii_char().unwrap().is_control(), true);
361361
/// ```
362362
#[inline]
363-
pub fn is_control(&self) -> bool {
364-
*self < AsciiChar::Space || *self == AsciiChar::DEL
363+
pub fn is_control(self) -> bool {
364+
self < AsciiChar::Space || self == AsciiChar::DEL
365365
}
366366

367367
/// Checks if the character is printable (except space)
@@ -374,7 +374,7 @@ impl AsciiChar {
374374
/// assert_eq!('\n'.to_ascii_char().unwrap().is_graph(), false);
375375
/// ```
376376
#[inline]
377-
pub fn is_graph(&self) -> bool {
377+
pub fn is_graph(self) -> bool {
378378
self.as_byte().wrapping_sub(b' '+1) < 0x5E
379379
}
380380

@@ -388,7 +388,7 @@ impl AsciiChar {
388388
/// assert_eq!('\n'.to_ascii_char().unwrap().is_print(), false);
389389
/// ```
390390
#[inline]
391-
pub fn is_print(&self) -> bool {
391+
pub fn is_print(self) -> bool {
392392
self.as_byte().wrapping_sub(b' ') < 0x5F
393393
}
394394

@@ -402,7 +402,7 @@ impl AsciiChar {
402402
/// assert_eq!('@'.to_ascii_char().unwrap().is_lowercase(), false);
403403
/// ```
404404
#[inline]
405-
pub fn is_lowercase(&self) -> bool {
405+
pub fn is_lowercase(self) -> bool {
406406
self.as_byte().wrapping_sub(b'a') < 26
407407
}
408408

@@ -416,7 +416,7 @@ impl AsciiChar {
416416
/// assert_eq!('@'.to_ascii_char().unwrap().is_uppercase(), false);
417417
/// ```
418418
#[inline]
419-
pub fn is_uppercase(&self) -> bool {
419+
pub fn is_uppercase(self) -> bool {
420420
self.as_byte().wrapping_sub(b'A') < 26
421421
}
422422

@@ -431,7 +431,7 @@ impl AsciiChar {
431431
/// assert_eq!('~'.to_ascii_char().unwrap().is_punctuation(), true);
432432
/// ```
433433
#[inline]
434-
pub fn is_punctuation(&self) -> bool {
434+
pub fn is_punctuation(self) -> bool {
435435
self.is_graph() && !self.is_alphanumeric()
436436
}
437437

@@ -447,7 +447,7 @@ impl AsciiChar {
447447
/// assert_eq!(' '.to_ascii_char().unwrap().is_hex(), false);
448448
/// ```
449449
#[inline]
450-
pub fn is_hex(&self) -> bool {
450+
pub fn is_hex(self) -> bool {
451451
self.is_digit() || (self.as_byte() | 0x20u8).wrapping_sub(b'a') < 6
452452
}
453453

@@ -551,27 +551,27 @@ impl AsciiExt for AsciiChar {
551551
macro_rules! impl_into_partial_eq_ord {($wider:ty, $to_wider:expr) => {
552552
impl From<AsciiChar> for $wider {
553553
fn from(a: AsciiChar) -> $wider {
554-
$to_wider(&a)
554+
$to_wider(a)
555555
}
556556
}
557557
impl PartialEq<$wider> for AsciiChar {
558558
fn eq(&self, rhs: &$wider) -> bool {
559-
$to_wider(self) == *rhs
559+
$to_wider(*self) == *rhs
560560
}
561561
}
562562
impl PartialEq<AsciiChar> for $wider {
563563
fn eq(&self, rhs: &AsciiChar) -> bool {
564-
*self == $to_wider(rhs)
564+
*self == $to_wider(*rhs)
565565
}
566566
}
567567
impl PartialOrd<$wider> for AsciiChar {
568568
fn partial_cmp(&self, rhs: &$wider) -> Option<Ordering> {
569-
$to_wider(self).partial_cmp(rhs)
569+
$to_wider(*self).partial_cmp(rhs)
570570
}
571571
}
572572
impl PartialOrd<AsciiChar> for $wider {
573573
fn partial_cmp(&self, rhs: &AsciiChar) -> Option<Ordering> {
574-
self.partial_cmp(&$to_wider(rhs))
574+
self.partial_cmp(&$to_wider(*rhs))
575575
}
576576
}
577577
}}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy