Struct vulkanalia::vk::StringArray
source · #[repr(transparent)]pub struct StringArray<const N: usize>(/* private fields */);
Expand description
An array containing a null-terminated string.
Equality / Hashing
For the purposes of comparing and hashing array strings, any characters after the first null terminator are ignored. The below example demonstrates this property with two strings that differ in the characters that come after the first null terminators.
let string1 = StringArray::<3>::new([0x61, 0, 0]);
let string2 = StringArray::<3>::new([0x61, 0, 0x61]);
assert_eq!(string1, string2);
let mut hasher1 = DefaultHasher::new();
string1.hash(&mut hasher1);
let mut hasher2 = DefaultHasher::new();
string2.hash(&mut hasher2);
assert_eq!(hasher1.finish(), hasher2.finish());
Implementations§
source§impl<const N: usize> StringArray<N>
impl<const N: usize> StringArray<N>
sourcepub fn new(array: [i8; N]) -> StringArray<N>
pub fn new(array: [i8; N]) -> StringArray<N>
Constructs a string array from a character array.
Panics
characters
does not contain a null-terminator
sourcepub const fn from_bytes(bytes: &[u8]) -> StringArray<N>
pub const fn from_bytes(bytes: &[u8]) -> StringArray<N>
Constructs a string array from a byte string.
If the byte string is longer than N - 1
, then the byte string will
be truncated to fit inside of the constructed string array (the last
character is reserved for a null terminator). The constructed string
array will always be null-terminated regardless if the byte string is
or is not null-terminated.
sourcepub fn from_cstr(cstr: &CStr) -> StringArray<N>
pub fn from_cstr(cstr: &CStr) -> StringArray<N>
Constructs a string array from a borrowed C string.
If the borrowed C string is longer than N - 1
, then the borrowed C
string will be truncated to fit inside of the constructed string array
(the last character is reserved for a null terminator).
sourcepub unsafe fn from_ptr(ptr: *const i8) -> StringArray<N>
pub unsafe fn from_ptr(ptr: *const i8) -> StringArray<N>
Constructs a string array from a pointer to a null-terminated string.
If the null-terminated string is longer than N - 1
, then the
null-terminated string will be truncated to fit inside of the
constructed string array (the last character is reserved for a null
terminator).
Safety
ptr
must be a pointer to a null-terminated string
sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub fn to_string_lossy(&self) -> Cow<'_, str>
Converts this string array to a UTF-8 string (lossily).
Methods from Deref<Target = [i8; N]>§
sourcepub fn as_ascii(&self) -> Option<&[AsciiChar; N]>
🔬This is a nightly-only experimental API. (ascii_char
)
pub fn as_ascii(&self) -> Option<&[AsciiChar; N]>
ascii_char
)Converts this array of bytes into a array of ASCII characters,
or returns None
if any of the characters is non-ASCII.
Examples
#![feature(ascii_char)]
#![feature(const_option)]
const HEX_DIGITS: [std::ascii::Char; 16] =
*b"0123456789abcdef".as_ascii().unwrap();
assert_eq!(HEX_DIGITS[1].as_str(), "1");
assert_eq!(HEX_DIGITS[10].as_str(), "a");
sourcepub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]
🔬This is a nightly-only experimental API. (ascii_char
)
pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]
ascii_char
)Converts this array of bytes into a array of ASCII characters, without checking whether they’re valid.
Safety
Every byte in the array must be in 0..=127
, or else this is UB.
1.57.0 · sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice containing the entire array. Equivalent to &s[..]
.
sourcepub fn each_ref(&self) -> [&T; N]
🔬This is a nightly-only experimental API. (array_methods
)
pub fn each_ref(&self) -> [&T; N]
array_methods
)Borrows each element and returns an array of references with the same
size as self
.
Example
#![feature(array_methods)]
let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);
This method is particularly useful if combined with other methods, like
map
. This way, you can avoid moving the original
array if its elements are not Copy
.
#![feature(array_methods)]
let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);
// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
sourcepub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
🔬This is a nightly-only experimental API. (split_array
)
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
split_array
)Divides one array reference into two at an index.
The first will contain all indices from [0, M)
(excluding
the index M
itself) and the second will contain all
indices from [M, N)
(excluding the index N
itself).
Panics
Panics if M > N
.
Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.split_array_ref::<0>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<2>();
assert_eq!(left, &[1, 2]);
assert_eq!(right, &[3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<6>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}
sourcepub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
🔬This is a nightly-only experimental API. (split_array
)
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
split_array
)Divides one array reference into two at an index from the end.
The first will contain all indices from [0, N - M)
(excluding
the index N - M
itself) and the second will contain all
indices from [N - M, N)
(excluding the index N
itself).
Panics
Panics if M > N
.
Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.rsplit_array_ref::<0>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}
{
let (left, right) = v.rsplit_array_ref::<2>();
assert_eq!(left, &[1, 2, 3, 4]);
assert_eq!(right, &[5, 6]);
}
{
let (left, right) = v.rsplit_array_ref::<6>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
Trait Implementations§
source§impl<const N: usize> Clone for StringArray<N>
impl<const N: usize> Clone for StringArray<N>
source§fn clone(&self) -> StringArray<N>
fn clone(&self) -> StringArray<N>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<const N: usize> Debug for StringArray<N>
impl<const N: usize> Debug for StringArray<N>
source§impl<const N: usize> Default for StringArray<N>
impl<const N: usize> Default for StringArray<N>
source§fn default() -> StringArray<N>
fn default() -> StringArray<N>
source§impl<const N: usize> Deref for StringArray<N>
impl<const N: usize> Deref for StringArray<N>
source§impl<const N: usize> Display for StringArray<N>
impl<const N: usize> Display for StringArray<N>
source§impl<const N: usize> Hash for StringArray<N>
impl<const N: usize> Hash for StringArray<N>
source§impl<const N: usize> PartialEq<StringArray<N>> for StringArray<N>
impl<const N: usize> PartialEq<StringArray<N>> for StringArray<N>
source§fn eq(&self, other: &StringArray<N>) -> bool
fn eq(&self, other: &StringArray<N>) -> bool
self
and other
values to be equal, and is used
by ==
.