Enum typed_path::windows::WindowsComponent
source · pub enum WindowsComponent<'a> {
Prefix(WindowsPrefixComponent<'a>),
RootDir,
CurDir,
ParentDir,
Normal(&'a [u8]),
}
Expand description
Byte slice version of std::path::Component
that represents a Windows-specific component
Variants§
Implementations§
source§impl<'a> WindowsComponent<'a>
impl<'a> WindowsComponent<'a>
sourcepub fn as_path<T>(&self) -> &Path<T>where
T: for<'enc> Encoding<'enc>,
pub fn as_path<T>(&self) -> &Path<T>where T: for<'enc> Encoding<'enc>,
Returns path representing this specific component
sourcepub fn prefix(self) -> Option<WindowsPrefixComponent<'a>>
pub fn prefix(self) -> Option<WindowsPrefixComponent<'a>>
Converts from WindowsComponent
to Option<WindowsPrefixComponent>
Converts self
into an Option<WindowsPrefixComponent>
, consuming self
, and
discarding if not a WindowsPrefixComponent
sourcepub fn prefix_kind(self) -> Option<WindowsPrefix<'a>>
pub fn prefix_kind(self) -> Option<WindowsPrefix<'a>>
Converts from WindowsComponent
to Option<WindowsPrefix>
Converts self
into an Option<WindowsPrefix>
, consuming self
, and
discarding if not a WindowsPrefixComponent
whose kind
method we invoke
Trait Implementations§
source§impl AsRef<[u8]> for WindowsComponent<'_>
impl AsRef<[u8]> for WindowsComponent<'_>
source§impl<'a> Clone for WindowsComponent<'a>
impl<'a> Clone for WindowsComponent<'a>
source§fn clone(&self) -> WindowsComponent<'a>
fn clone(&self) -> WindowsComponent<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Component<'a> for WindowsComponent<'a>
impl<'a> Component<'a> for WindowsComponent<'a>
source§fn as_bytes(&self) -> &'a [u8] ⓘ
fn as_bytes(&self) -> &'a [u8] ⓘ
Extracts the underlying [[u8]
] slice
Examples
use typed_path::{Component, WindowsPath};
let path = WindowsPath::new(br"C:\tmp\foo\..\bar.txt");
let components: Vec<_> = path.components().map(|comp| comp.as_bytes()).collect();
assert_eq!(&components, &[
b"C:".as_slice(),
br"\".as_slice(),
b"tmp".as_slice(),
b"foo".as_slice(),
b"..".as_slice(),
b"bar.txt".as_slice(),
]);
source§fn is_root(&self) -> bool
fn is_root(&self) -> bool
Root is one of two situations
- Is the root separator, e.g.
\windows
- Is a non-disk prefix, e.g.
\\server\share
Examples
use typed_path::{Component, windows::WindowsComponent};
use std::convert::TryFrom;
let root_dir = WindowsComponent::try_from(br"\").unwrap();
assert!(root_dir.is_root());
let non_disk_prefix = WindowsComponent::try_from(br"\\?\pictures").unwrap();
assert!(non_disk_prefix.is_root());
let disk_prefix = WindowsComponent::try_from(b"C:").unwrap();
assert!(!disk_prefix.is_root());
let normal = WindowsComponent::try_from(b"file.txt").unwrap();
assert!(!normal.is_root());
source§fn is_normal(&self) -> bool
fn is_normal(&self) -> bool
Returns true if component is normal
Examples
use typed_path::{Component, windows::WindowsComponent};
use std::convert::TryFrom;
let normal = WindowsComponent::try_from(b"file.txt").unwrap();
assert!(normal.is_normal());
let root_dir = WindowsComponent::try_from(br"\").unwrap();
assert!(!root_dir.is_normal());
source§impl<'a> Debug for WindowsComponent<'a>
impl<'a> Debug for WindowsComponent<'a>
source§impl<'a> Hash for WindowsComponent<'a>
impl<'a> Hash for WindowsComponent<'a>
source§impl<'a> Ord for WindowsComponent<'a>
impl<'a> Ord for WindowsComponent<'a>
source§fn cmp(&self, other: &WindowsComponent<'a>) -> Ordering
fn cmp(&self, other: &WindowsComponent<'a>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<'a> PartialEq<WindowsComponent<'a>> for WindowsComponent<'a>
impl<'a> PartialEq<WindowsComponent<'a>> for WindowsComponent<'a>
source§fn eq(&self, other: &WindowsComponent<'a>) -> bool
fn eq(&self, other: &WindowsComponent<'a>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialOrd<WindowsComponent<'a>> for WindowsComponent<'a>
impl<'a> PartialOrd<WindowsComponent<'a>> for WindowsComponent<'a>
source§fn partial_cmp(&self, other: &WindowsComponent<'a>) -> Option<Ordering>
fn partial_cmp(&self, other: &WindowsComponent<'a>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'a> TryFrom<&'a [u8]> for WindowsComponent<'a>
impl<'a> TryFrom<&'a [u8]> for WindowsComponent<'a>
source§fn try_from(path: &'a [u8]) -> Result<Self, Self::Error>
fn try_from(path: &'a [u8]) -> Result<Self, Self::Error>
Parses the byte slice into a WindowsComponent
Examples
use typed_path::windows::{WindowsComponent, WindowsPrefix};
use std::convert::TryFrom;
// Supports parsing Windows prefixes
let component = WindowsComponent::try_from(b"c:").unwrap();
assert_eq!(component.prefix_kind(), Some(WindowsPrefix::Disk(b'C')));
// Supports parsing standard windows path components
assert_eq!(WindowsComponent::try_from(br"\"), Ok(WindowsComponent::RootDir));
assert_eq!(WindowsComponent::try_from(b"."), Ok(WindowsComponent::CurDir));
assert_eq!(WindowsComponent::try_from(b".."), Ok(WindowsComponent::ParentDir));
assert_eq!(WindowsComponent::try_from(br"file.txt"), Ok(WindowsComponent::Normal(b"file.txt")));
assert_eq!(WindowsComponent::try_from(br"dir\"), Ok(WindowsComponent::Normal(b"dir")));
// Parsing more than one component will fail
assert!(WindowsComponent::try_from(br"\file").is_err());
source§impl<'a> TryFrom<&'a str> for WindowsComponent<'a>
impl<'a> TryFrom<&'a str> for WindowsComponent<'a>
source§impl<'a> TryFrom<Component<'a>> for WindowsComponent<'a>
impl<'a> TryFrom<Component<'a>> for WindowsComponent<'a>
source§fn try_from(component: StdComponent<'a>) -> Result<Self, Self::Error>
fn try_from(component: StdComponent<'a>) -> Result<Self, Self::Error>
Attempts to convert a std::path::Component
into a WindowsComponent
, returning a
result containing the new component when successful or the original component when failed
Examples
use std::convert::TryFrom;
use std::ffi::OsStr;
use std::path::Component;
use typed_path::windows::WindowsComponent;
let component = WindowsComponent::try_from(Component::RootDir).unwrap();
assert_eq!(component, WindowsComponent::RootDir);
let component = WindowsComponent::try_from(Component::CurDir).unwrap();
assert_eq!(component, WindowsComponent::CurDir);
let component = WindowsComponent::try_from(Component::ParentDir).unwrap();
assert_eq!(component, WindowsComponent::ParentDir);
let component = WindowsComponent::try_from(Component::Normal(OsStr::new("file.txt"))).unwrap();
assert_eq!(component, WindowsComponent::Normal(b"file.txt"));
Alongside the traditional path components, the Component::Prefix
variant is also
supported, but only when compiling on Windows. When on a non-Windows platform, the
conversion will always fail.
source§impl<'a> TryFrom<WindowsComponent<'a>> for StdComponent<'a>
impl<'a> TryFrom<WindowsComponent<'a>> for StdComponent<'a>
source§fn try_from(component: WindowsComponent<'a>) -> Result<Self, Self::Error>
fn try_from(component: WindowsComponent<'a>) -> Result<Self, Self::Error>
Attempts to convert a WindowsComponent
into a std::path::Component
, returning a
result containing the new path when successful or the original path when failed
Examples
use std::convert::TryFrom;
use std::ffi::OsStr;
use std::path::Component;
use typed_path::windows::WindowsComponent;
let component = Component::try_from(WindowsComponent::RootDir).unwrap();
assert_eq!(component, Component::RootDir);
let component = Component::try_from(WindowsComponent::CurDir).unwrap();
assert_eq!(component, Component::CurDir);
let component = Component::try_from(WindowsComponent::ParentDir).unwrap();
assert_eq!(component, Component::ParentDir);
let component = Component::try_from(WindowsComponent::Normal(b"file.txt")).unwrap();
assert_eq!(component, Component::Normal(OsStr::new("file.txt")));
Alongside the traditional path components, the Component::Prefix
variant is also
supported, but only when compiling on Windows. When on a non-Windows platform, the
conversion will always fail.