Skip to main content

SimpleUnc

Struct SimpleUnc 

Source
pub struct SimpleUnc {
    pub allow_long: bool,
    pub map_to_drive: bool,
    pub skip_dunce: bool,
    pub _unused: bool,
}
Expand description

Simplifies Win32 File Namespaces paths (the “\\?\” prefix) for better readability and compatibility.

SimpleUnc::default().canonicalize(path);

is a snap-in replacement of fs::canonicalize.

C:\dirZ:\x (network)
fs::canonicalize\\?\C:\dir\\?\UNC\server\share\x
SimpleUncC:\dir\\server\share\x
SimpleUnc with map_to_driveC:\dirZ:\x

Fields§

§allow_long: bool

When false, the simplification is disabled if the result is a long path (longer than 260 characters).

Long paths may not be supported by some programs and APIs. In such cases, the Win32 File Namespaces (the “\\?\” prefix) may be able to work around the limitation.

On the other hand, other programs such as PowerShell v7 can’t handle the “\\?\” prefix, but it can handle long paths. Set to true for such cases.

§map_to_drive: bool

Map to network share drive names when possible.

let path = "file.txt";
let unc = SimpleUnc { map_to_drive: true, ..Default::default() };
let canonicalized = unc.canonicalize(path)?;

If the file.txt is in a network drive, the result is Z:\dir\file.txt instead of \\server\share\dir\file.txt.

The following code tries to preserve the original form of the path.

SimpleUnc {
    map_to_drive: !path.as_os_str().as_encoded_bytes().starts_with(br"\\"),
    ..Default::default()
}.canonicalize(path)?;
§skip_dunce: bool

The dunce simplification is applied by default. Set to true to skip it.

§_unused: bool

It is highly recommended to always use , ..Default::default(). Otherwise builds fail when new fields are added.

This field is not used in any ways, but exists to allow using , ..Default::default() even when all other fields are specified.

Implementations§

Source§

impl SimpleUnc

Source

pub fn canonicalize(&self, path: impl AsRef<Path>) -> Result<PathBuf>

Calls fs::canonicalize and simplify.

On other platforms than Windows, this is equivalent to fs::canonicalize.

Source

pub fn simplify<'a>(&self, path: &'a Path) -> Result<Option<Cow<'a, Path>>>

Try to simplify the given path.

Returns Ok(None) if no simplification is applied, or on other platforms than Windows.

Source

pub fn refresh() -> Result<()>

Refreshes the cached information.

Source

pub fn display<'a>(&'a self, path: &'a Path) -> Display<'a>

Returns an object that implements Display.

§Examples
let path = Path::new("file").canonicalize()?;
println!("{}", SimpleUnc::default().display(&path));

Trait Implementations§

Source§

impl Debug for SimpleUnc

Source§

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

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

impl Default for SimpleUnc

Source§

fn default() -> SimpleUnc

Returns the “default value” for a type. Read more

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> 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, 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.