pub struct Utf8WindowsPrefixComponent<'a> { /* private fields */ }
Expand description
A structure wrapping a Windows path prefix as well as its unparsed string
representation. str
version of std::path::PrefixComponent
.
In addition to the parsed Utf8WindowsPrefix
information returned by kind
,
Utf8WindowsPrefixComponent
also holds the raw and unparsed str
slice,
returned by as_str
.
Instances of this struct
can be obtained by matching against the
Utf8WindowsPrefix
variant on Utf8WindowsComponent
.
This is available for use on all platforms despite being a Windows-specific format.
§Examples
use typed_path::{Utf8WindowsPath, Utf8WindowsComponent, Utf8WindowsPrefix};
let path = Utf8WindowsPath::new(r"c:\you\later\");
match path.components().next().unwrap() {
Utf8WindowsComponent::Prefix(prefix_component) => {
// Notice that the disk kind uses an uppercase letter, but the raw slice
// underneath has a lowercase drive letter
assert_eq!(Utf8WindowsPrefix::Disk('C'), prefix_component.kind());
assert_eq!("c:", prefix_component.as_str());
}
_ => unreachable!(),
}
Implementations§
Source§impl<'a> Utf8WindowsPrefixComponent<'a>
impl<'a> Utf8WindowsPrefixComponent<'a>
Sourcepub fn kind(&self) -> Utf8WindowsPrefix<'a>
pub fn kind(&self) -> Utf8WindowsPrefix<'a>
Returns the parsed prefix data
See Utf8WindowsPrefix
’s documentation for more information on the different
kinds of prefixes.
Sourcepub fn as_str(&self) -> &'a str
pub fn as_str(&self) -> &'a str
Returns the raw str
slice for this prefix
§Examples
use typed_path::Utf8WindowsPrefixComponent;
use std::convert::TryFrom;
// Disk will include the drive letter and :
let component = Utf8WindowsPrefixComponent::try_from("C:").unwrap();
assert_eq!(component.as_str(), "C:");
// UNC will include server & share
let component = Utf8WindowsPrefixComponent::try_from(r"\\server\share").unwrap();
assert_eq!(component.as_str(), r"\\server\share");
// Device NS will include device
let component = Utf8WindowsPrefixComponent::try_from(r"\\.\BrainInterface").unwrap();
assert_eq!(component.as_str(), r"\\.\BrainInterface");
// Verbatim will include component
let component = Utf8WindowsPrefixComponent::try_from(r"\\?\pictures").unwrap();
assert_eq!(component.as_str(), r"\\?\pictures");
// Verbatim UNC will include server & share
let component = Utf8WindowsPrefixComponent::try_from(r"\\?\UNC\server\share").unwrap();
assert_eq!(component.as_str(), r"\\?\UNC\server\share");
// Verbatim disk will include drive letter and :
let component = Utf8WindowsPrefixComponent::try_from(r"\\?\C:").unwrap();
assert_eq!(component.as_str(), r"\\?\C:");
Sourcepub fn from_utf8(
component: &WindowsPrefixComponent<'a>,
) -> Result<Self, Utf8Error>
pub fn from_utf8( component: &WindowsPrefixComponent<'a>, ) -> Result<Self, Utf8Error>
Converts a non-UTF-8 WindowsPrefixComponent
to a UTF-8 Utf8WindowsPrefixComponent
by checking that the component contains valid UTF-8.
§Errors
Returns Err
if the prefix component is not UTF-8 with a description as to why the
provided component is not UTF-8.
See the docs for Utf8Error
for more details on the kinds of
errors that can be returned.
Sourcepub unsafe fn from_utf8_unchecked(
component: &WindowsPrefixComponent<'a>,
) -> Self
pub unsafe fn from_utf8_unchecked( component: &WindowsPrefixComponent<'a>, ) -> Self
Converts a non-UTF-8 WindowsPrefixComponent
to a UTF-8 Utf8WindowsPrefixComponent
without checking that the string contains valid UTF-8.
See the safe version, from_utf8
, for more information.
§Safety
The component passed in must be valid UTF-8.
Trait Implementations§
Source§impl<'a> Clone for Utf8WindowsPrefixComponent<'a>
impl<'a> Clone for Utf8WindowsPrefixComponent<'a>
Source§fn clone(&self) -> Utf8WindowsPrefixComponent<'a>
fn clone(&self) -> Utf8WindowsPrefixComponent<'a>
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a> Debug for Utf8WindowsPrefixComponent<'a>
impl<'a> Debug for Utf8WindowsPrefixComponent<'a>
Source§impl Hash for Utf8WindowsPrefixComponent<'_>
impl Hash for Utf8WindowsPrefixComponent<'_>
Source§impl Ord for Utf8WindowsPrefixComponent<'_>
impl Ord for Utf8WindowsPrefixComponent<'_>
Source§impl<'a> PartialEq for Utf8WindowsPrefixComponent<'a>
impl<'a> PartialEq for Utf8WindowsPrefixComponent<'a>
Source§fn eq(&self, other: &Utf8WindowsPrefixComponent<'a>) -> bool
fn eq(&self, other: &Utf8WindowsPrefixComponent<'a>) -> bool
self
and other
values to be equal, and is used by ==
.Source§impl<'a> PartialOrd for Utf8WindowsPrefixComponent<'a>
impl<'a> PartialOrd for Utf8WindowsPrefixComponent<'a>
Source§impl<'a> TryFrom<&'a str> for Utf8WindowsPrefixComponent<'a>
impl<'a> TryFrom<&'a str> for Utf8WindowsPrefixComponent<'a>
Source§fn try_from(path: &'a str) -> Result<Self, Self::Error>
fn try_from(path: &'a str) -> Result<Self, Self::Error>
Parses the str slice into a Utf8WindowsPrefixComponent
§Examples
use typed_path::{Utf8WindowsPrefix, Utf8WindowsPrefixComponent};
use std::convert::TryFrom;
let component = Utf8WindowsPrefixComponent::try_from("C:").unwrap();
assert_eq!(component.kind(), Utf8WindowsPrefix::Disk('C'));
let component = Utf8WindowsPrefixComponent::try_from(r"\\.\BrainInterface").unwrap();
assert_eq!(component.kind(), Utf8WindowsPrefix::DeviceNS("BrainInterface"));
let component = Utf8WindowsPrefixComponent::try_from(r"\\server\share").unwrap();
assert_eq!(component.kind(), Utf8WindowsPrefix::UNC("server", "share"));
let component = Utf8WindowsPrefixComponent::try_from(r"\\?\UNC\server\share").unwrap();
assert_eq!(component.kind(), Utf8WindowsPrefix::VerbatimUNC("server", "share"));
let component = Utf8WindowsPrefixComponent::try_from(r"\\?\C:").unwrap();
assert_eq!(component.kind(), Utf8WindowsPrefix::VerbatimDisk('C'));
let component = Utf8WindowsPrefixComponent::try_from(r"\\?\pictures").unwrap();
assert_eq!(component.kind(), Utf8WindowsPrefix::Verbatim("pictures"));
// Parsing something that is not a prefix will fail
assert!(Utf8WindowsPrefixComponent::try_from("hello").is_err());
// Parsing more than a prefix will fail
assert!(Utf8WindowsPrefixComponent::try_from(r"C:\path").is_err());