Skip to content

Commit 875d06a

Browse files
committed
Add back AsciiStr::new(), now as const fn
1 parent 41d4a00 commit 875d06a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/ascii_str.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use alloc::borrow::ToOwned;
33
#[cfg(feature = "alloc")]
44
use alloc::boxed::Box;
5-
use core::fmt;
5+
use core::{fmt, mem};
66
use core::ops::{Index, IndexMut};
77
use core::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
88
use core::slice::{self, Iter, IterMut, SliceIndex};
@@ -28,6 +28,23 @@ pub struct AsciiStr {
2828
}
2929

3030
impl AsciiStr {
31+
/// Coerces into an `AsciiStr` slice.
32+
///
33+
/// # Examples
34+
/// ```
35+
/// # use ascii::{AsciiChar, AsciiStr};
36+
/// const HELLO: &AsciiStr = AsciiStr::new(
37+
/// &[AsciiChar::H, AsciiChar::e, AsciiChar::l, AsciiChar::l, AsciiChar::o]
38+
/// );
39+
///
40+
/// assert_eq!(HELLO.as_str(), "Hello");
41+
/// ```
42+
#[inline]
43+
#[must_use]
44+
pub const fn new(s: &[AsciiChar]) -> &Self {
45+
unsafe { mem::transmute(s) }
46+
}
47+
3148
/// Converts `&self` to a `&str` slice.
3249
#[inline]
3350
#[must_use]

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