Skip to content

Commit e39560a

Browse files
author
Thomas Bahn
committed
Merge pull request #18 from tormol/str_into_from
Impl From<[Ascii]> and Into<str,[u8],[Ascii]> for AsciiStr
2 parents 0ac0224 + 07a841a commit e39560a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/ascii_str.rs

+29
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,35 @@ impl AsRef<str> for AsciiStr {
198198
}
199199
}
200200

201+
impl<'a> From<&'a[Ascii]> for &'a AsciiStr {
202+
fn from(slice: &[Ascii]) -> &AsciiStr {
203+
unsafe{ mem::transmute(slice) }
204+
}
205+
}
206+
impl From<Box<[Ascii]>> for Box<AsciiStr> {
207+
fn from(owned: Box<[Ascii]>) -> Box<AsciiStr> {
208+
unsafe{ mem::transmute(owned) }
209+
}
210+
}
211+
212+
macro_rules! impl_into {
213+
($wider: ty) => {
214+
impl<'a> From<&'a AsciiStr> for &'a$wider {
215+
fn from(slice: &AsciiStr) -> &$wider {
216+
unsafe{ mem::transmute(slice) }
217+
}
218+
}
219+
impl From<Box<AsciiStr>> for Box<$wider> {
220+
fn from(owned: Box<AsciiStr>) -> Box<$wider> {
221+
unsafe{ mem::transmute(owned) }
222+
}
223+
}
224+
}
225+
}
226+
impl_into! {[Ascii]}
227+
impl_into! {[u8]}
228+
impl_into! {str}
229+
201230
impl fmt::Display for AsciiStr {
202231
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
203232
fmt::Display::fmt(self.as_str(), f)

0 commit comments

Comments
 (0)