From 9fc54645f1eaad9115700707ba22436fa72db957 Mon Sep 17 00:00:00 2001 From: tormol Date: Sun, 20 Sep 2020 14:45:13 +0200 Subject: [PATCH] Add back AsciiStr::new(), now as const fn --- src/ascii_str.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ascii_str.rs b/src/ascii_str.rs index e8a6e12..d5eac7f 100644 --- a/src/ascii_str.rs +++ b/src/ascii_str.rs @@ -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}; @@ -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]