Skip to content

Commit 0323050

Browse files
tormolThomas Bahn
authored andcommitted
Future-proof some methods
* De-genericize AsciiStr::new() and from_ascii_unchecked() so that they can hopefully be made const fn sooner (due to needing fewer features). * Remove AsciiStr::new() because I want to make it similar to AsciiChar::new(), but also want to reserve the name for what becomes possible as const fn first. * Make AsciiStr::split() return impl Trait to make changing it to take a Pattern once that is stabilized easier. * Make AsciiStr::lines() return const fn just in case.
1 parent 10fa4be commit 0323050

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

src/ascii_char.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl AsciiChar {
367367
/// might not panic, creating a buffer overflow,
368368
/// and `Some(AsciiChar::from_ascii_unchecked(128))` might be `None`.
369369
#[inline]
370-
pub unsafe fn from_ascii_unchecked<C: ToAsciiChar>(ch: C) -> Self {
370+
pub unsafe fn from_ascii_unchecked(ch: u8) -> Self {
371371
ch.to_ascii_char_unchecked()
372372
}
373373

src/ascii_str.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ pub struct AsciiStr {
2525
}
2626

2727
impl AsciiStr {
28-
/// Coerces into an `AsciiStr` slice.
29-
pub fn new<S: AsRef<AsciiStr> + ?Sized>(s: &S) -> &AsciiStr {
30-
s.as_ref()
31-
}
32-
3328
/// Converts `&self` to a `&str` slice.
3429
#[inline]
3530
pub fn as_str(&self) -> &str {
@@ -85,7 +80,7 @@ impl AsciiStr {
8580
/// # Examples
8681
/// ```
8782
/// # use ascii::AsciiStr;
88-
/// let foo = AsciiStr::from_ascii("foo");
83+
/// let foo = AsciiStr::from_ascii(b"foo");
8984
/// let err = AsciiStr::from_ascii("Ŋ");
9085
/// assert_eq!(foo.unwrap().as_str(), "foo");
9186
/// assert_eq!(err.unwrap_err().valid_up_to(), 0);
@@ -104,15 +99,12 @@ impl AsciiStr {
10499
/// # Examples
105100
/// ```
106101
/// # use ascii::AsciiStr;
107-
/// let foo = unsafe{ AsciiStr::from_ascii_unchecked("foo") };
102+
/// let foo = unsafe { AsciiStr::from_ascii_unchecked(&b"foo"[..]) };
108103
/// assert_eq!(foo.as_str(), "foo");
109104
/// ```
110105
#[inline]
111-
pub unsafe fn from_ascii_unchecked<B: ?Sized>(bytes: &B) -> &AsciiStr
112-
where
113-
B: AsRef<[u8]>,
114-
{
115-
bytes.as_ref().as_ascii_str_unchecked()
106+
pub unsafe fn from_ascii_unchecked(bytes: &[u8]) -> &AsciiStr {
107+
bytes.as_ascii_str_unchecked()
116108
}
117109

118110
/// Returns the number of characters / bytes in this ASCII sequence.
@@ -167,7 +159,7 @@ impl AsciiStr {
167159
/// .collect::<Vec<_>>();
168160
/// assert_eq!(words, ["apple", "banana", "lemon"]);
169161
/// ```
170-
pub fn split(&self, on: AsciiChar) -> Split {
162+
pub fn split(&self, on: AsciiChar) -> impl DoubleEndedIterator<Item=&AsciiStr> {
171163
Split {
172164
on,
173165
ended: false,
@@ -181,7 +173,7 @@ impl AsciiStr {
181173
///
182174
/// The final line ending is optional.
183175
#[inline]
184-
pub fn lines(&self) -> Lines {
176+
pub fn lines(&self) -> impl DoubleEndedIterator<Item=&AsciiStr> {
185177
Lines {
186178
string: self,
187179
}
@@ -587,7 +579,7 @@ impl<'a> DoubleEndedIterator for CharsRef<'a> {
587579
///
588580
/// This type is created by [`AsciiChar::split()`](struct.AsciiChar.html#method.split).
589581
#[derive(Clone, Debug)]
590-
pub struct Split<'a> {
582+
struct Split<'a> {
591583
on: AsciiChar,
592584
ended: bool,
593585
chars: Chars<'a>
@@ -629,7 +621,7 @@ impl<'a> DoubleEndedIterator for Split<'a> {
629621

630622
/// An iterator over the lines of the internal character array.
631623
#[derive(Clone, Debug)]
632-
pub struct Lines<'a> {
624+
struct Lines<'a> {
633625
string: &'a AsciiStr,
634626
}
635627
impl<'a> Iterator for Lines<'a> {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ mod serialization;
5050

5151
pub use ascii_char::{AsciiChar, ToAsciiChar, ToAsciiCharError};
5252
pub use ascii_str::{AsciiStr, AsAsciiStr, AsMutAsciiStr, AsAsciiStrError};
53-
pub use ascii_str::{Chars, CharsMut, CharsRef, Lines, Split};
53+
pub use ascii_str::{Chars, CharsMut, CharsRef};
5454
#[cfg(feature = "std")]
5555
pub use ascii_string::{AsciiString, IntoAsciiString, FromAsciiError};
5656
pub use free_functions::{caret_encode, caret_decode};

0 commit comments

Comments
 (0)