Struct WindowsPrefixComponent

Source
pub struct WindowsPrefixComponent<'a> { /* private fields */ }
Expand description

A structure wrapping a Windows path prefix as well as its unparsed string representation. Byte slice version of std::path::PrefixComponent.

In addition to the parsed WindowsPrefix information returned by kind, WindowsPrefixComponent also holds the raw and unparsed [[u8]] slice, returned by as_bytes.

Instances of this struct can be obtained by matching against the WindowsPrefix variant on WindowsComponent.

This is available for use on all platforms despite being a Windows-specific format.

§Examples

use typed_path::{WindowsPath, WindowsComponent, WindowsPrefix};

let path = WindowsPath::new(r"c:\you\later\");
match path.components().next().unwrap() {
    WindowsComponent::Prefix(prefix_component) => {
        // Notice that the disk kind uses an uppercase letter, but the raw slice
        // underneath has a lowercase drive letter
        assert_eq!(WindowsPrefix::Disk(b'C'), prefix_component.kind());
        assert_eq!(b"c:".as_slice(), prefix_component.as_bytes());
    }
    _ => unreachable!(),
}

Implementations§

Source§

impl<'a> WindowsPrefixComponent<'a>

Source

pub fn kind(&self) -> WindowsPrefix<'a>

Returns the parsed prefix data

See WindowsPrefix’s documentation for more information on the different kinds of prefixes.

Source

pub fn len(&self) -> usize

Returns the size of the prefix in bytes

Source

pub fn as_bytes(&self) -> &'a [u8]

Returns the raw [[u8]] slice for this prefix

§Examples
use typed_path::WindowsPrefixComponent;
use std::convert::TryFrom;

// Disk will include the drive letter and :
let component = WindowsPrefixComponent::try_from(b"C:").unwrap();
assert_eq!(component.as_bytes(), b"C:");

// UNC will include server & share
let component = WindowsPrefixComponent::try_from(br"\\server\share").unwrap();
assert_eq!(component.as_bytes(), br"\\server\share");

// Device NS will include device
let component = WindowsPrefixComponent::try_from(br"\\.\BrainInterface").unwrap();
assert_eq!(component.as_bytes(), br"\\.\BrainInterface");

// Verbatim will include component
let component = WindowsPrefixComponent::try_from(br"\\?\pictures").unwrap();
assert_eq!(component.as_bytes(), br"\\?\pictures");

// Verbatim UNC will include server & share
let component = WindowsPrefixComponent::try_from(br"\\?\UNC\server\share").unwrap();
assert_eq!(component.as_bytes(), br"\\?\UNC\server\share");

// Verbatim disk will include drive letter and :
let component = WindowsPrefixComponent::try_from(br"\\?\C:").unwrap();
assert_eq!(component.as_bytes(), br"\\?\C:");

Trait Implementations§

Source§

impl<'a> Clone for WindowsPrefixComponent<'a>

Source§

fn clone(&self) -> WindowsPrefixComponent<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for WindowsPrefixComponent<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Hash for WindowsPrefixComponent<'_>

Source§

fn hash<H: Hasher>(&self, h: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for WindowsPrefixComponent<'_>

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq for WindowsPrefixComponent<'a>

Source§

fn eq(&self, other: &WindowsPrefixComponent<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for WindowsPrefixComponent<'a>

Source§

fn partial_cmp(&self, other: &WindowsPrefixComponent<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> TryFrom<&'a [u8]> for WindowsPrefixComponent<'a>

Source§

fn try_from(path: &'a [u8]) -> Result<Self, Self::Error>

Parses the byte slice into a WindowsPrefixComponent

§Examples
use typed_path::{WindowsPrefix, WindowsPrefixComponent};
use std::convert::TryFrom;

let component = WindowsPrefixComponent::try_from(b"C:").unwrap();
assert_eq!(component.kind(), WindowsPrefix::Disk(b'C'));

let component = WindowsPrefixComponent::try_from(br"\\.\BrainInterface").unwrap();
assert_eq!(component.kind(), WindowsPrefix::DeviceNS(b"BrainInterface"));

let component = WindowsPrefixComponent::try_from(br"\\server\share").unwrap();
assert_eq!(component.kind(), WindowsPrefix::UNC(b"server", b"share"));

let component = WindowsPrefixComponent::try_from(br"\\?\UNC\server\share").unwrap();
assert_eq!(component.kind(), WindowsPrefix::VerbatimUNC(b"server", b"share"));

let component = WindowsPrefixComponent::try_from(br"\\?\C:").unwrap();
assert_eq!(component.kind(), WindowsPrefix::VerbatimDisk(b'C'));

let component = WindowsPrefixComponent::try_from(br"\\?\pictures").unwrap();
assert_eq!(component.kind(), WindowsPrefix::Verbatim(b"pictures"));

// Parsing something that is not a prefix will fail
assert!(WindowsPrefixComponent::try_from(b"hello").is_err());

// Parsing more than a prefix will fail
assert!(WindowsPrefixComponent::try_from(br"C:\path").is_err());
Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

impl<'a, const N: usize> TryFrom<&'a [u8; N]> for WindowsPrefixComponent<'a>

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_from(path: &'a [u8; N]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<&'a str> for WindowsPrefixComponent<'a>

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_from(s: &'a str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> Copy for WindowsPrefixComponent<'a>

Source§

impl<'a> Eq for WindowsPrefixComponent<'a>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.