Skip to content
dream-path
api

NormalizedPath

The owned key: constructors, accessors, inspection, conversions and trait implementations.

struct NormalizedPath(BString)

An owned, normalized virtual resource path, for storing and looking up. Every way of making one from arbitrary input normalizes it; the two ways of adopting bytes as they are require them to be normalized already.

Clone, Debug, Default (the empty path), Eq, Hash, Ord and PartialOrd, all over the normalized bytes. Ordering is byte order.

Making one#

fn new(path: impl AsRef<[u8]>) -> NormalizedPath

Normalizes path.

impl From<&[u8] | &str | &BStr | Vec<u8> | String | BString> for NormalizedPath

Each normalizes, like new.

fn try_from_normalized_bytes(path: Vec<u8>) -> Result<NormalizedPath, Vec<u8>>

Adopts path without copying if it is normalized, and hands it back unchanged as the error if it is not.

fn from_normalized_bytes_unchecked(path: Vec<u8>) -> NormalizedPath

Adopts path without checking; debug builds assert it. Bytes that are not normalized break nothing in memory, only lookups: the key will not match its normalized spelling.

Reading it#

fn as_bytes(&self) -> &[u8]
fn as_bstr(&self) -> &BStr
fn to_str(&self) -> Result<&str, Utf8Error>

The key as &str when its bytes are valid UTF-8. Valid UTF-8 can still hold NUL and control bytes; it is not a C string or a display string.

fn len(&self) -> usize
fn is_empty(&self) -> bool

Length in bytes, and whether it is the empty path.

Inspecting#

fn parent(&self) -> Option<&BStr>
fn file_name(&self) -> Option<&BStr>
fn extension(&self) -> Option<&BStr>

The same as parent_normalized, file_name_normalized and extension_normalized on the key’s bytes.

Borrowing and converting#

ImplementationUse
Borrow<[u8]>, Borrow<BStr>Look a key up in a HashMap or BTreeMap by normalized bytes, without building a key
AsRef<[u8]>, AsRef<BStr>Pass a key wherever bytes are accepted
From<NormalizedPath> for Vec<u8>, for BStringMove the bytes out without copying