Struct rkyv::ffi::ArchivedCString
source · [−]#[repr(transparent)]pub struct ArchivedCString(_);
Implementations
sourceimpl ArchivedCString
impl ArchivedCString
sourcepub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Returns the contents of this CString as a slice of bytes.
The returned slice does not contain the trailing nul terminator, and it is guaranteed to
not have any interior nul bytes. If you need the nul terminator, use
as_bytes_with_nul
instead.
sourcepub fn as_bytes_with_nul(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_bytes_with_nul(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Equivalent to as_bytes
except that the returned slice
includes the trailing nul terminator.
sourcepub fn pin_mut_c_str(self: Pin<&mut Self>) -> Pin<&mut CStr>
pub fn pin_mut_c_str(self: Pin<&mut Self>) -> Pin<&mut CStr>
Extracts a pinned mutable CStr
slice containing the entire string.
sourcepub unsafe fn resolve_from_c_str(
c_str: &CStr,
pos: usize,
resolver: CStringResolver,
out: *mut Self
)
pub unsafe fn resolve_from_c_str(
c_str: &CStr,
pos: usize,
resolver: CStringResolver,
out: *mut Self
)
Resolves an archived C string from the given C string and parameters.
Safety
pos
must be the position ofout
within the archiveresolver
must be the result of serializing a C string
sourcepub fn serialize_from_c_str<S: Serializer + ?Sized>(
c_str: &CStr,
serializer: &mut S
) -> Result<CStringResolver, S::Error>
pub fn serialize_from_c_str<S: Serializer + ?Sized>(
c_str: &CStr,
serializer: &mut S
) -> Result<CStringResolver, S::Error>
Serializes a C string.
Methods from Deref<Target = CStr>
1.0.0 · sourcepub fn as_ptr(&self) -> *const i8
pub fn as_ptr(&self) -> *const i8
Returns the inner pointer to this C string.
The returned pointer will be valid for as long as self
is, and points
to a contiguous region of memory terminated with a 0 byte to represent
the end of the string.
WARNING
The returned pointer is read-only; writing to it (including passing it to C code that writes to it) causes undefined behavior.
It is your responsibility to make sure that the underlying memory is not
freed too early. For example, the following code will cause undefined
behavior when ptr
is used inside the unsafe
block:
use std::ffi::CString;
let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
unsafe {
// `ptr` is dangling
*ptr;
}
This happens because the pointer returned by as_ptr
does not carry any
lifetime information and the CString
is deallocated immediately after
the CString::new("Hello").expect("CString::new failed").as_ptr()
expression is evaluated.
To fix the problem, bind the CString
to a local variable:
use std::ffi::CString;
let hello = CString::new("Hello").expect("CString::new failed");
let ptr = hello.as_ptr();
unsafe {
// `ptr` is valid because `hello` is in scope
*ptr;
}
This way, the lifetime of the CString
in hello
encompasses
the lifetime of ptr
and the unsafe
block.
1.0.0 · sourcepub fn to_bytes(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn to_bytes(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Converts this C string to a byte slice.
The returned slice will not contain the trailing nul terminator that this C string has.
Note: This method is currently implemented as a constant-time cast, but it is planned to alter its definition in the future to perform the length calculation whenever this method is called.
Examples
use std::ffi::CStr;
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
assert_eq!(cstr.to_bytes(), b"foo");
1.0.0 · sourcepub fn to_bytes_with_nul(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn to_bytes_with_nul(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Converts this C string to a byte slice containing the trailing 0 byte.
This function is the equivalent of CStr::to_bytes
except that it
will retain the trailing nul terminator instead of chopping it off.
Note: This method is currently implemented as a 0-cost cast, but it is planned to alter its definition in the future to perform the length calculation whenever this method is called.
Examples
use std::ffi::CStr;
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
assert_eq!(cstr.to_bytes_with_nul(), b"foo\0");
1.4.0 · sourcepub fn to_str(&self) -> Result<&str, Utf8Error>
pub fn to_str(&self) -> Result<&str, Utf8Error>
Yields a &str
slice if the CStr
contains valid UTF-8.
If the contents of the CStr
are valid UTF-8 data, this
function will return the corresponding &str
slice. Otherwise,
it will return an error with details of where UTF-8 validation failed.
Examples
use std::ffi::CStr;
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
assert_eq!(cstr.to_str(), Ok("foo"));
1.4.0 · sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub fn to_string_lossy(&self) -> Cow<'_, str>
Converts a CStr
into a Cow<str>
.
If the contents of the CStr
are valid UTF-8 data, this
function will return a Cow::Borrowed(&str)
with the corresponding &str
slice. Otherwise, it will
replace any invalid UTF-8 sequences with
U+FFFD REPLACEMENT CHARACTER
and return a
Cow::Owned(&str)
with the result.
Examples
Calling to_string_lossy
on a CStr
containing valid UTF-8:
use std::borrow::Cow;
use std::ffi::CStr;
let cstr = CStr::from_bytes_with_nul(b"Hello World\0")
.expect("CStr::from_bytes_with_nul failed");
assert_eq!(cstr.to_string_lossy(), Cow::Borrowed("Hello World"));
Calling to_string_lossy
on a CStr
containing invalid UTF-8:
use std::borrow::Cow;
use std::ffi::CStr;
let cstr = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0")
.expect("CStr::from_bytes_with_nul failed");
assert_eq!(
cstr.to_string_lossy(),
Cow::Owned(String::from("Hello �World")) as Cow<'_, str>
);
Trait Implementations
sourceimpl Borrow<CStr> for ArchivedCString
impl Borrow<CStr> for ArchivedCString
sourceimpl Debug for ArchivedCString
impl Debug for ArchivedCString
sourceimpl Deref for ArchivedCString
impl Deref for ArchivedCString
sourceimpl<'a, D: Fallible + ?Sized> DeserializeWith<ArchivedCString, Cow<'a, CStr>, D> for AsOwned
impl<'a, D: Fallible + ?Sized> DeserializeWith<ArchivedCString, Cow<'a, CStr>, D> for AsOwned
sourcefn deserialize_with(
field: &ArchivedCString,
deserializer: &mut D
) -> Result<Cow<'a, CStr>, D::Error>
fn deserialize_with(
field: &ArchivedCString,
deserializer: &mut D
) -> Result<Cow<'a, CStr>, D::Error>
Deserializes the field type F
using the given deserializer.
sourceimpl Hash for ArchivedCString
impl Hash for ArchivedCString
sourceimpl Index<RangeFull> for ArchivedCString
impl Index<RangeFull> for ArchivedCString
sourceimpl Ord for ArchivedCString
impl Ord for ArchivedCString
sourceimpl PartialEq<&'_ CStr> for ArchivedCString
impl PartialEq<&'_ CStr> for ArchivedCString
sourceimpl PartialEq<ArchivedCString> for ArchivedCString
impl PartialEq<ArchivedCString> for ArchivedCString
sourceimpl PartialEq<ArchivedCString> for &CStr
impl PartialEq<ArchivedCString> for &CStr
sourceimpl PartialEq<ArchivedCString> for CString
impl PartialEq<ArchivedCString> for CString
sourceimpl PartialEq<CString> for ArchivedCString
impl PartialEq<CString> for ArchivedCString
sourceimpl PartialOrd<ArchivedCString> for ArchivedCString
impl PartialOrd<ArchivedCString> for ArchivedCString
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Eq for ArchivedCString
Auto Trait Implementations
impl RefUnwindSafe for ArchivedCString
impl Send for ArchivedCString
impl Sync for ArchivedCString
impl !Unpin for ArchivedCString
impl UnwindSafe for ArchivedCString
Blanket Implementations
sourceimpl<T> ArchivePointee for T
impl<T> ArchivePointee for T
type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
sourcefn pointer_metadata(_: &Self::ArchivedMetadata) -> <Self as Pointee>::Metadata
fn pointer_metadata(_: &Self::ArchivedMetadata) -> <Self as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more