Struct unixstring::UnixString [−][src]
#[non_exhaustive]pub struct UnixString { /* fields omitted */ }
Expand description
An FFI-friendly null-terminated byte string.
Implementations
Constructs a new, “empty” UnixString
.
This function allocates a byte in order to store its nul terminator.
If you are creating a UnixString
with the intention of writing stuff to it, you may be interested in UnixString::with_capacity
.
Extends the UnixString
with anything that implements AsRef
<OsStr
>.
This method fails if the given data has a zero byte anywhere but at its end.
use std::path::Path;
use unixstring::UnixString;
let mut unix_string = UnixString::new();
unix_string.push("/home/")?;
let username = Path::new("user");
unix_string.push(username)?;
assert_eq!(unix_string.as_str()?, "/home/user");
Extends the UnixString
with the given bytes.
This method fails if the bytes contain an interior zero byte (a zero byte not at the buffer’s final position)
use std::path::Path;
use unixstring::UnixString;
let mut unix_string = UnixString::new();
let abc = b"abc".to_vec();
unix_string.push_bytes(&abc)?;
assert_eq!(unix_string.into_bytes(), abc);
Creates a UnixString
given a Vec
of bytes.
This method will return an error if the given bytes have a zero byte, except if the zero byte is the last element of the Vec
.
use unixstring::UnixString;
let bytes_without_zero = b"abc".to_vec();
let bytes_with_nul_terminator = b"abc\0".to_vec();
let bytes_with_interior_nul = b"a\0bc".to_vec();
// Valid: no zero bytes were given
assert!(UnixString::from_bytes(bytes_without_zero).is_ok());
// Still valid: the zero byte is being used as the terminator
assert!(UnixString::from_bytes(bytes_with_nul_terminator).is_ok());
// Invalid: an interior nul byte was found
assert!(UnixString::from_bytes(bytes_with_interior_nul).is_err());
Constructs a new, empty UnixString
with the specified capacity.
The UnixString
’s inner vector will be able to hold exactly capacity
elements without
reallocating.
This function will always allocate enough to fit the null terminator byte, even if the given capacity is 0.
Panics
Panics if the new capacity exceeds isize::MAX - 1
bytes.
Clones a raw C string into an UnixString
.
The total size of the raw C string must be smaller than isize::MAX
bytes
in memory due to calling the slice::from_raw_parts
function.
Safety
This method is unsafe for a number of reasons:
- There is no guarantee to the validity of
ptr
. - There is no guarantee that the memory pointed to by
ptr
contains a valid nul terminator byte at the end of the string. - It is not guaranteed that the memory pointed by
ptr
won’t change before theUnixString
has been constructed.
See CStr::from_ptr
for more info.
Returns an inner pointer to the data this UnixString
contains.
The returned pointer will be valid for as long as the given UnixString
is, and points
to a null-terminated contiguous region of memory.
Note: The returned pointer is read-only and writing to it in any way causes undefined behavior.
You must ensure that the underlying memory is not
freed too early. If the UnixString
is deallocated then the pointer becomes dangling.
See CStr::as_ptr
for more info.
Converts the UnixString
to an OsStr
slice. This always succeeds and is zero cost. The terminating nul byte will not be included in the OsStr
slice.
use std::{convert::TryFrom, path::PathBuf};
use unixstring::UnixString;
let logs = PathBuf::from("/var/log/journal");
let unix_string = UnixString::try_from(logs.clone()).unwrap();
assert_eq!(
logs.as_os_str(),
unix_string.as_os_str()
)
Converts the UnixString
to a Path
slice. This always succeeds and is zero cost.
The terminating nul byte will not be included in the Path
slice.
use std::{convert::TryFrom, path::PathBuf};
use unixstring::UnixString;
let home_dir = PathBuf::from("/home/user");
let unix_string = UnixString::try_from(home_dir.clone()).unwrap();
assert_eq!(&home_dir, unix_string.as_path())
Converts the UnixString
to a CStr
slice. This always succeeds and is zero cost.
Tries to convert this UnixString
into a &str
.
The terminating nul byte will not be included in the &str
.
If this byte string is not valid UTF-8, then an error is returned indicating the first invalid byte found and the length of the error.
If instead you wish for a lossy conversion to &str, then use to_str_lossy
.
Extends a UnixString
by copying from a raw C string
Safety
This method is unsafe for a number of reasons:
- The total size of the raw C string must be smaller than
isize::MAX
bytes in memory. - There is no guarantee to the validity of
ptr
. - There is no guarantee that the memory pointed to by
ptr
contains a valid nul terminator byte at the end of the string. - It is not guaranteed that the memory pointed by
ptr
won’t change while this operation occurs.
This function uses CStr::from_ptr
internally, so check it out for more information.
This function will check if the inner bytes of this UnixString
(without its null terminator) consists of valid UTF-8, and if so, returns a String
reusing the UnixString
’s buffer.
If the inner bytes contain invalid UTF-8, then a new String
will be allocated with the invalid bytes replaced with the Unicode replacement codepoint.
Converts a UnixString
into a String if the bytes of the UnixString
are valid UTF-8.
If you are sure that the byte slice is valid UTF-8 and you don’t want to incur the overhead of the validity check, there is an unsafe version of this function, UnixString::into_string_unchecked
, which has the same behavior but skips the check.
If the validity check passes, the resulting String
will reuse the allocation of the UnixString
’s inner buffer and no copy will be done.
If you need a &str
instead of a String
, consider UnixString::as_str
.
Converts a UnixString
into a String
without checking that the
string contains valid UTF-8.
See the safe version, UnixString::into_string
, for more details.
Safety
This function is unsafe because it does not check that the bytes passed
to it are valid UTF-8. If this constraint is violated, it may cause
memory unsafety issues with future users of the String
, as the rest of
the standard library assumes that String
s are valid UTF-8.
Examples
use unixstring::UnixString;
let baby = UnixString::from_bytes(vec![0xF0, 0x9F, 0x91, 0xB6]).unwrap();
let baby = unsafe {
baby.into_string_unchecked()
};
assert_eq!("👶", baby);
This function will check if the inner bytes of this UnixString
(without its null terminator) consists of valid UTF-8, and if so, returns a string slice without copying.
If the inner bytes contain invalid UTF-8, then a new String
will be allocated with the invalid bytes replaced with the Unicode replacement codepoint.
Gets the underlying byte view of this UnixString
without the nul terminator.
use unixstring::UnixString;
let bytes = b"abc\0".to_vec();
let unix_string = UnixString::from_bytes(bytes).unwrap();
assert_eq!(
unix_string.as_bytes(),
&[b'a', b'b', b'c']
);
Converts a UnixString
into an OsString
.
This operation is zero-cost.
If you need a &OsStr
instead of an OsString
, consider UnixString::as_os_str
.
Converts a UnixString
into a PathBuf
.
This operation is zero-cost.
If you need a &Path
instead of a PathBuf
, consider UnixString::as_path
.
Converts a UnixString
into a PathBuf
.
This operation is zero-cost.
If you need a &CStr
instead of a CString
, consider UnixString::as_c_str
.
Gets the underlying byte view of this UnixString
including the nul terminator.
use unixstring::UnixString;
let bytes = b"abc\0".to_vec();
let unix_string = UnixString::from_bytes(bytes).unwrap();
assert_eq!(
unix_string.as_bytes_with_nul(),
&[b'a', b'b', b'c', 0]
);
Returns the inner representation of a UnixString
.
The UnixString
’s nul terminator byte will be included.
Returns the inner representation of a UnixString
with its nul-terminator removed.
Converts a CString
into an UnixString
.
This operation is zero-cost and does not fail.
This operation fails if the CString
has any interior zero byte but a zero byte at the last position is acceptable.
Converts a PathBuf
into an UnixString
.
A validation will be done to ensure that the PathBuf
has no interior zero bytes.
Other than that, this operation is zero-cost.
This operation fails if the PathBuf
has any interior zero byte but a zero byte at the last position is acceptable.
Converts a String
into an UnixString
.
A validation will be done to ensure that the String
has no interior zero bytes.
Other than that, this operation is zero-cost.
This operation fails if the String
has any interior zero byte but a zero byte at the last position is acceptable.
Converts an OsString
into an UnixString
.
A validation will be done to ensure that the OsString
has no interior zero bytes.
Other than that, this operation is zero-cost.
This operation fails if the OsString
has any interior zero byte but a zero byte at the last position is acceptable.
Trait Implementations
Performs the conversion.
Performs the conversion.
Performs the conversion.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Auto Trait Implementations
impl RefUnwindSafe for UnixString
impl Send for UnixString
impl Sync for UnixString
impl Unpin for UnixString
impl UnwindSafe for UnixString
Blanket Implementations
Mutably borrows from an owned value. Read more