Skip to content

Commit

Permalink
Add back AsciiStr::new(), now as const fn
Browse files Browse the repository at this point in the history
  • Loading branch information
tormol committed Jun 5, 2022
1 parent 95ba94e commit df1d68d
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/ascii_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use alloc::borrow::ToOwned;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use core::fmt;
use core::{fmt, mem};
use core::ops::{Index, IndexMut};
use core::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
use core::slice::{self, Iter, IterMut, SliceIndex};
Expand All @@ -28,6 +28,23 @@ pub struct AsciiStr {
}

impl AsciiStr {
/// Coerces into an `AsciiStr` slice.
///
/// # Examples
/// ```
/// # use ascii::{AsciiChar, AsciiStr};
/// const HELLO: &AsciiStr = AsciiStr::new(
/// &[AsciiChar::H, AsciiChar::e, AsciiChar::l, AsciiChar::l, AsciiChar::o]
/// );
///
/// assert_eq!(HELLO.as_str(), "Hello");
/// ```
#[inline]
#[must_use]
pub const fn new(s: &[AsciiChar]) -> &Self {
unsafe { mem::transmute(s) }
}

/// Converts `&self` to a `&str` slice.
#[inline]
#[must_use]
Expand Down

0 comments on commit df1d68d

Please sign in to comment.