Struct PotentialUtf8

struct PotentialUtf8(120)

A byte slice that is expected to be a UTF-8 string but does not enforce that invariant.

Use this type instead of str if you don't need to enforce UTF-8 during deserialization. For example, strings that are keys of a map don't need to ever be reified as strs.

PotentialUtf8 derefs to [u8]. To obtain a str, use [Self::try_as_str()].

The main advantage of this type over [u8] is that it serializes as a string in human-readable formats like JSON.

Examples

Using an PotentialUtf8 as the key of a ZeroMap:

use potential_utf::PotentialUtf8;
use zerovec::ZeroMap;

// This map is cheap to deserialize, as we don't need to perform UTF-8 validation.
let map: ZeroMap<PotentialUtf8, u8> = [
    (PotentialUtf8::from_bytes(b"abc"), 11),
    (PotentialUtf8::from_bytes(b"def"), 22),
    (PotentialUtf8::from_bytes(b"ghi"), 33),
]
.into_iter()
.collect();

let key = "abc";
let value = map.get_copied(PotentialUtf8::from_str(key));
assert_eq!(Some(11), value);

Implementations

impl PotentialUtf8

const fn from_bytes(other: &[u8]) -> &Self

Create a PotentialUtf8 from a byte slice.

const fn from_str(s: &str) -> &Self

Create a PotentialUtf8 from a string slice.

const fn as_bytes(self: &Self) -> &[u8]

Get the bytes from a [`PotentialUtf8].

fn try_as_str(self: &Self) -> Result<&str, Utf8Error>

Attempt to convert a PotentialUtf8 to a str.

Examples

use potential_utf::PotentialUtf8;

static A: &PotentialUtf8 = PotentialUtf8::from_bytes(b"abc");

let b = A.try_as_str().unwrap();
assert_eq!(b, "abc");

impl Debug for PotentialUtf8

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Deref for PotentialUtf8

fn deref(self: &Self) -> &<Self as >::Target

impl Eq for PotentialUtf8

impl Freeze for PotentialUtf8

impl Ord for PotentialUtf8

fn cmp(self: &Self, other: &PotentialUtf8) -> Ordering

impl PartialEq for PotentialUtf8

fn eq(self: &Self, other: &PotentialUtf8) -> bool

impl PartialEq for PotentialUtf8

fn eq(self: &Self, other: &str) -> bool

impl PartialOrd for PotentialUtf8

fn partial_cmp(self: &Self, other: &PotentialUtf8) -> Option<Ordering>

impl PartialOrd for PotentialUtf8

fn partial_cmp(self: &Self, other: &str) -> Option<Ordering>

impl RefUnwindSafe for PotentialUtf8

impl Send for PotentialUtf8

impl Sized for PotentialUtf8

impl StructuralPartialEq for PotentialUtf8

impl Sync for PotentialUtf8

impl Unpin for PotentialUtf8

impl UnsafeUnpin for PotentialUtf8

impl UnwindSafe for PotentialUtf8

impl VarULE for PotentialUtf8

fn validate_bytes(_: &[u8]) -> Result<(), UleError>
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self

impl<P, T> Receiver for PotentialUtf8

impl<T> Any for PotentialUtf8

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for PotentialUtf8

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for PotentialUtf8

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> EncodeAsVarULE for PotentialUtf8

fn encode_var_ule_as_slices<R>(self: &Self, cb: impl FnOnce(&[&[u8]]) -> R) -> R