Trait Component

Source
pub trait Component<'a>:
    AsRef<[u8]>
    + Clone
    + Debug
    + PartialEq
    + Eq
    + PartialOrd
    + Ord
    + Sealed {
    // Required methods
    fn as_bytes(&self) -> &'a [u8] ;
    fn is_root(&self) -> bool;
    fn is_normal(&self) -> bool;
    fn is_parent(&self) -> bool;
    fn is_current(&self) -> bool;
    fn is_valid(&self) -> bool;
    fn len(&self) -> usize;
    fn root() -> Self;
    fn parent() -> Self;
    fn current() -> Self;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

Interface representing a component in a Path

Required Methods§

Source

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

Extracts the underlying [[u8]] slice

Source

fn is_root(&self) -> bool

Returns true if this component is the root component, meaning there are no more components before this one

Use cases are for the root dir separator on Windows and Unix as well as Windows std::path::PrefixComponent

§Examples

/my/../path/./here.txt has the components on Unix of

  • UnixComponent::RootDir - is_root() == true
  • UnixComponent::ParentDir - is_root() == false
  • UnixComponent::CurDir - is_root() == false
  • UnixComponent::Normal(b"here.txt") - is_root() == false
Source

fn is_normal(&self) -> bool

Returns true if this component represents a normal part of the path

§Examples

/my/../path/./here.txt has the components on Unix of

  • UnixComponent::RootDir - is_normal() == false
  • UnixComponent::ParentDir - is_normal() == false
  • UnixComponent::CurDir - is_normal() == false
  • UnixComponent::Normal(b"here.txt") - is_normal() == true
Source

fn is_parent(&self) -> bool

Returns true if this component represents a relative representation of a parent directory

§Examples

/my/../path/./here.txt has the components on Unix of

  • UnixComponent::RootDir - is_parent() == false
  • UnixComponent::ParentDir - is_parent() == true
  • UnixComponent::CurDir - is_parent() == false
  • UnixComponent::Normal("here.txt") - is_parent() == false
Source

fn is_current(&self) -> bool

Returns true if this component represents a relative representation of the current directory

§Examples

/my/../path/./here.txt has the components on Unix of

  • UnixComponent::RootDir - is_current() == false
  • UnixComponent::ParentDir - is_current() == false
  • UnixComponent::CurDir - is_current() == true
  • UnixComponent::Normal("here.txt") - is_current() == false
Source

fn is_valid(&self) -> bool

Returns true if this component is valid. A component can only be invalid if it represents a normal component with bytes that are disallowed by the encoding.

§Examples
use typed_path::{Component, UnixComponent, WindowsComponent};

assert!(UnixComponent::RootDir.is_valid());
assert!(UnixComponent::ParentDir.is_valid());
assert!(UnixComponent::CurDir.is_valid());
assert!(UnixComponent::Normal(b"abc").is_valid());
assert!(!UnixComponent::Normal(b"\0").is_valid());

assert!(WindowsComponent::RootDir.is_valid());
assert!(WindowsComponent::ParentDir.is_valid());
assert!(WindowsComponent::CurDir.is_valid());
assert!(WindowsComponent::Normal(b"abc").is_valid());
assert!(!WindowsComponent::Normal(b"|").is_valid());
Source

fn len(&self) -> usize

Returns size of component in bytes

Source

fn root() -> Self

Returns a root Component.

Source

fn parent() -> Self

Returns a parent directory Component.

Source

fn current() -> Self

Returns a current directory Component.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if component represents an empty byte slice

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a> Component<'a> for UnixComponent<'a>

Source§

impl<'a> Component<'a> for WindowsComponent<'a>