Skip to content

Commit c203d4e

Browse files
author
Thomas Bahn
committed
Add more simple conversions
Added functions: - `AsciiStr::as_mut_slice`, the non-mutable variant was already there. - `AsciiStr::as_ptr` - `AsciiStr::as_mut_ptr`
1 parent e994a97 commit c203d4e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/ascii_str.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,34 @@ impl AsciiStr {
2828
unsafe { mem::transmute(&self.slice) }
2929
}
3030

31+
/// Returns the entire string as slice of `Ascii` characters.
3132
pub fn as_slice(&self) -> &[Ascii] {
3233
&self.slice
3334
}
3435

36+
/// Returns the entire string as mutable slice of `Ascii` characters.
37+
pub fn as_mut_slice(&mut self) -> &mut [Ascii] {
38+
&mut self.slice
39+
}
40+
41+
/// Returns a raw pointer to the `AsciiStr`'s buffer.
42+
///
43+
/// The caller must ensure that the slice outlives the pointer this function returns, or else it
44+
/// will end up pointing to garbage. Modifying the `AsciiStr` may cause it's buffer to be
45+
/// reallocated, which would also make any pointers to it invalid.
46+
pub fn as_ptr(&self) -> *const Ascii {
47+
self.as_slice().as_ptr()
48+
}
49+
50+
/// Returns an unsafe mutable pointer to the `AsciiStr`'s buffer.
51+
///
52+
/// The caller must ensure that the slice outlives the pointer this function returns, or else it
53+
/// will end up pointing to garbage. Modifying the `AsciiStr` may cause it's buffer to be
54+
/// reallocated, which would also make any pointers to it invalid.
55+
pub fn as_mut_ptr(&mut self) -> *mut Ascii {
56+
self.as_mut_slice().as_mut_ptr()
57+
}
58+
3559
/// Copies the content of this `AsciiStr` into an owned `AsciiString`.
3660
pub fn to_ascii_string(&self) -> AsciiString {
3761
AsciiString::from(self.slice.to_vec())

0 commit comments

Comments
 (0)