Enum TypedPath

Source
pub enum TypedPath<'a> {
    Unix(&'a UnixPath),
    Windows(&'a WindowsPath),
}
Expand description

Represents a path with a known type that can be one of:

Variants§

§

Unix(&'a UnixPath)

§

Windows(&'a WindowsPath)

Implementations§

Source§

impl<'a> TypedPath<'a>

Source

pub fn new<S: AsRef<[u8]> + ?Sized>(s: &'a S, type: PathType) -> Self

Creates a new path with the given type as its encoding.

Source

pub fn unix<S: AsRef<[u8]> + ?Sized>(s: &'a S) -> Self

Creates a new typed Unix path.

Source

pub fn windows<S: AsRef<[u8]> + ?Sized>(s: &'a S) -> Self

Creates a new typed Windows path.

Source

pub fn derive<S: AsRef<[u8]> + ?Sized>(s: &'a S) -> Self

Creates a new typed path from a byte slice by determining if the path represents a Windows or Unix path. This is accomplished by first trying to parse as a Windows path. If the resulting path contains a prefix such as C: or begins with a \, it is assumed to be a WindowsPath; otherwise, the slice will be represented as a UnixPath.

§Examples
use typed_path::TypedPath;

assert!(TypedPath::derive(br#"C:\some\path\to\file.txt"#).is_windows());
assert!(TypedPath::derive(br#"\some\path\to\file.txt"#).is_windows());
assert!(TypedPath::derive(br#"/some/path/to/file.txt"#).is_unix());

// NOTE: If we don't start with a backslash, it's too difficult to
//       determine and we therefore just assume a Unix/POSIX path.
assert!(TypedPath::derive(br#"some\path\to\file.txt"#).is_unix());
assert!(TypedPath::derive(b"file.txt").is_unix());
assert!(TypedPath::derive(b"").is_unix());
Examples found in repository?
examples/typed.rs (line 5)
3fn main() {
4    // Try to be smart to figure out the path (Unix or Windows) automatically
5    let path = TypedPath::derive(r"/path/to/file.txt");
6
7    for component in path.components() {
8        println!("{}", String::from_utf8_lossy(component.as_bytes()));
9    }
10}
Source

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

Yields the underlying [[u8]] slice.

§Examples
use typed_path::TypedPath;

let bytes = TypedPath::derive("foo.txt").as_bytes().to_vec();
assert_eq!(bytes, b"foo.txt");
Source

pub fn to_str(&self) -> Option<&str>

Yields a &str slice if the TypedPath is valid unicode.

This conversion may entail doing a check for UTF-8 validity. Note that validation is performed because non-UTF-8 strings are perfectly valid for some OS.

§Examples
use typed_path::TypedPath;

let path = TypedPath::derive("foo.txt");
assert_eq!(path.to_str(), Some("foo.txt"));
Source

pub fn to_string_lossy(&self) -> Cow<'_, str>

Converts a TypedPath to a Cow<str>.

Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.

§Examples

Calling to_string_lossy on a TypedPath with valid unicode:

use typed_path::TypedPath;

let path = TypedPath::derive("foo.txt");
assert_eq!(path.to_string_lossy(), "foo.txt");

Had path contained invalid unicode, the to_string_lossy call might have returned "fo�.txt".

Source

pub fn to_path_buf(&self) -> TypedPathBuf

Converts a TypedPath into a TypedPathBuf.

§Examples
use typed_path::{TypedPath, TypedPathBuf};

let path_buf = TypedPath::derive("foo.txt").to_path_buf();
assert_eq!(path_buf, TypedPathBuf::from("foo.txt"));
Source

pub fn is_absolute(&self) -> bool

Returns true if the TypedPath is absolute, i.e., if it is independent of the current directory.

  • On Unix (UnixPath]), a path is absolute if it starts with the root, so is_absolute and has_root are equivalent.

  • On Windows (WindowsPath), a path is absolute if it has a prefix and starts with the root: c:\windows is absolute, while c:temp and \temp are not.

§Examples
use typed_path::TypedPath;

assert!(!TypedPath::derive("foo.txt").is_absolute());
Source

pub fn is_relative(&self) -> bool

Returns true if the TypedPath is relative, i.e., not absolute.

See is_absolute’s documentation for more details.

§Examples
use typed_path::TypedPath;

assert!(TypedPath::derive("foo.txt").is_relative());
Source

pub fn has_root(&self) -> bool

Returns true if the TypedPath has a root.

  • On Unix (UnixPath), a path has a root if it begins with /.

  • On Windows (WindowsPath), a path has a root if it:

    • has no prefix and begins with a separator, e.g., \windows
    • has a prefix followed by a separator, e.g., c:\windows but not c:windows
    • has any non-disk prefix, e.g., \\server\share
§Examples
use typed_path::TypedPath;

assert!(TypedPath::derive("/etc/passwd").has_root());
Source

pub fn parent(&self) -> Option<Self>

Returns the TypedPath without its final component, if there is one.

Returns None if the path terminates in a root or prefix.

§Examples
use typed_path::TypedPath;

let path = TypedPath::derive("/foo/bar");
let parent = path.parent().unwrap();
assert_eq!(parent, TypedPath::derive("/foo"));

let grand_parent = parent.parent().unwrap();
assert_eq!(grand_parent, TypedPath::derive("/"));
assert_eq!(grand_parent.parent(), None);
Source

pub fn ancestors(&self) -> TypedAncestors<'a>

Produces an iterator over TypedPath and its ancestors.

The iterator will yield the TypedPath that is returned if the parent method is used zero or more times. That means, the iterator will yield &self, &self.parent().unwrap(), &self.parent().unwrap().parent().unwrap() and so on. If the parent method returns None, the iterator will do likewise. The iterator will always yield at least one value, namely &self.

§Examples
use typed_path::TypedPath;

let mut ancestors = TypedPath::derive("/foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(TypedPath::derive("/foo/bar")));
assert_eq!(ancestors.next(), Some(TypedPath::derive("/foo")));
assert_eq!(ancestors.next(), Some(TypedPath::derive("/")));
assert_eq!(ancestors.next(), None);

let mut ancestors = TypedPath::derive("../foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(TypedPath::derive("../foo/bar")));
assert_eq!(ancestors.next(), Some(TypedPath::derive("../foo")));
assert_eq!(ancestors.next(), Some(TypedPath::derive("..")));
assert_eq!(ancestors.next(), Some(TypedPath::derive("")));
assert_eq!(ancestors.next(), None);
Source

pub fn file_name(&self) -> Option<&[u8]>

Returns the final component of the TypedPath, if there is one.

If the path is a normal file, this is the file name. If it’s the path of a directory, this is the directory name.

Returns None if the path terminates in ...

§Examples
use typed_path::TypedPath;

assert_eq!(Some(b"bin".as_slice()), TypedPath::derive("/usr/bin/").file_name());
assert_eq!(Some(b"foo.txt".as_slice()), TypedPath::derive("tmp/foo.txt").file_name());
assert_eq!(Some(b"foo.txt".as_slice()), TypedPath::derive("foo.txt/.").file_name());
assert_eq!(Some(b"foo.txt".as_slice()), TypedPath::derive("foo.txt/.//").file_name());
assert_eq!(None, TypedPath::derive("foo.txt/..").file_name());
assert_eq!(None, TypedPath::derive("/").file_name());
Source

pub fn strip_prefix( &self, base: impl AsRef<[u8]>, ) -> Result<TypedPath<'_>, StripPrefixError>

Returns a path that, when joined onto base, yields self.

§Difference from Path

Unlike Path::strip_prefix, this implementation only supports types that implement AsRef<[u8]> instead of AsRef<Path>.

§Errors

If base is not a prefix of self (i.e., starts_with returns false), returns Err.

§Examples
use typed_path::{TypedPath, TypedPathBuf};

let path = TypedPath::derive("/test/haha/foo.txt");

assert_eq!(path.strip_prefix("/"), Ok(TypedPath::derive("test/haha/foo.txt")));
assert_eq!(path.strip_prefix("/test"), Ok(TypedPath::derive("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/"), Ok(TypedPath::derive("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(TypedPath::derive("")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(TypedPath::derive("")));

assert!(path.strip_prefix("test").is_err());
assert!(path.strip_prefix("/haha").is_err());

let prefix = TypedPathBuf::from("/test/");
assert_eq!(path.strip_prefix(prefix), Ok(TypedPath::derive("haha/foo.txt")));
Source

pub fn starts_with(&self, base: impl AsRef<[u8]>) -> bool

Determines whether base is a prefix of self.

Only considers whole path components to match.

§Difference from Path

Unlike Path::starts_with, this implementation only supports types that implement AsRef<[u8]> instead of AsRef<Path>.

§Examples
use typed_path::TypedPath;

let path = TypedPath::derive("/etc/passwd");

assert!(path.starts_with("/etc"));
assert!(path.starts_with("/etc/"));
assert!(path.starts_with("/etc/passwd"));
assert!(path.starts_with("/etc/passwd/")); // extra slash is okay
assert!(path.starts_with("/etc/passwd///")); // multiple extra slashes are okay

assert!(!path.starts_with("/e"));
assert!(!path.starts_with("/etc/passwd.txt"));

assert!(!TypedPath::derive("/etc/foo.rs").starts_with("/etc/foo"));
Source

pub fn ends_with(&self, child: impl AsRef<[u8]>) -> bool

Determines whether child is a suffix of self.

Only considers whole path components to match.

§Difference from Path

Unlike Path::ends_with, this implementation only supports types that implement AsRef<[u8]> instead of AsRef<Path>.

§Examples
use typed_path::TypedPath;

let path = TypedPath::derive("/etc/resolv.conf");

assert!(path.ends_with("resolv.conf"));
assert!(path.ends_with("etc/resolv.conf"));
assert!(path.ends_with("/etc/resolv.conf"));

assert!(!path.ends_with("/resolv.conf"));
assert!(!path.ends_with("conf")); // use .extension() instead
Source

pub fn file_stem(&self) -> Option<&[u8]>

Extracts the stem (non-extension) portion of self.file_name.

The stem is:

  • None, if there is no file name;
  • The entire file name if there is no embedded .;
  • The entire file name if the file name begins with . and has no other .s within;
  • Otherwise, the portion of the file name before the final .
§Examples
use typed_path::TypedPath;

assert_eq!(b"foo", TypedPath::derive("foo.rs").file_stem().unwrap());
assert_eq!(b"foo.tar", TypedPath::derive("foo.tar.gz").file_stem().unwrap());
Source

pub fn extension(&self) -> Option<&[u8]>

Extracts the extension of self.file_name, if possible.

The extension is:

  • None, if there is no file name;
  • None, if there is no embedded .;
  • None, if the file name begins with . and has no other .s within;
  • Otherwise, the portion of the file name after the final .
§Examples
use typed_path::TypedPath;

// NOTE: A path cannot be created on its own without a defined encoding
assert_eq!(b"rs", TypedPath::derive("foo.rs").extension().unwrap());
assert_eq!(b"gz", TypedPath::derive("foo.tar.gz").extension().unwrap());
Source

pub fn normalize(&self) -> TypedPathBuf

Returns an owned TypedPathBuf by resolving .. and . segments.

When multiple, sequential path segment separation characters are found (e.g. / for Unix and either \ or / on Windows), they are replaced by a single instance of the platform-specific path segment separator (/ on Unix and \ on Windows).

§Examples
use typed_path::{TypedPath, TypedPathBuf};

assert_eq!(
    TypedPath::derive("foo/bar//baz/./asdf/quux/..").normalize(),
    TypedPathBuf::from("foo/bar/baz/asdf"),
);

When starting with a root directory, any .. segment whose parent is the root directory will be filtered out:

use typed_path::{TypedPath, TypedPathBuf};

// NOTE: A path cannot be created on its own without a defined encoding
assert_eq!(
    TypedPath::derive("/../foo").normalize(),
    TypedPathBuf::from("/foo"),
);

If any .. is left unresolved as the path is relative and no parent is found, it is discarded:

use typed_path::{TypedPath, TypedPathBuf};

assert_eq!(
    TypedPath::derive("../foo/..").normalize(),
    TypedPathBuf::from(""),
);

// Windows prefixes also count this way, but the prefix remains
assert_eq!(
    TypedPath::derive(r"C:..\foo\..").normalize(),
    TypedPathBuf::from(r"C:"),
);
Source

pub fn absolutize(&self) -> Result<TypedPathBuf>

Converts a path to an absolute form by normalizing the path, returning a TypedPathBuf.

In the case that the path is relative, the current working directory is prepended prior to normalizing.

§Examples
use typed_path::{utils, TypedPath};

// With an absolute path, it is just normalized
let path = TypedPath::derive("/a/b/../c/./d");
assert_eq!(path.absolutize().unwrap(), TypedPath::derive("/a/c/d"));

// With a relative path, it is first joined with the current working directory
// and then normalized
let cwd = utils::current_dir().unwrap().with_unix_encoding().to_typed_path_buf();
let path = cwd.join("a/b/../c/./d");
assert_eq!(path.absolutize().unwrap(), cwd.join("a/c/d"));
Source

pub fn join(&self, path: impl AsRef<[u8]>) -> TypedPathBuf

Creates an owned TypedPathBuf with path adjoined to self.

See TypedPathBuf::push for more details on what it means to adjoin a path.

§Difference from Path

Unlike Path::join, this implementation only supports types that implement AsRef<[u8]> instead of AsRef<Path>.

§Examples
use typed_path::{TypedPath, TypedPathBuf};

assert_eq!(
    TypedPath::derive("/etc").join("passwd"),
    TypedPathBuf::from("/etc/passwd"),
);
Source

pub fn join_checked( &self, path: impl AsRef<[u8]>, ) -> Result<TypedPathBuf, CheckedPathError>

Creates an owned TypedPathBuf with path adjoined to self, checking the path to ensure it is safe to join. When dealing with user-provided paths, this is the preferred method.

See TypedPathBuf::push_checked for more details on what it means to adjoin a path safely.

§Difference from Path

Unlike Path::join_checked, this implementation only supports types that implement AsRef<[u8]> instead of AsRef<Path>.

§Examples
use typed_path::{CheckedPathError, TypedPath, TypedPathBuf};

assert_eq!(
    TypedPath::derive("/etc").join_checked("passwd"),
    Ok(TypedPathBuf::from("/etc/passwd")),
);

assert_eq!(
    TypedPath::derive("/etc").join_checked("/sneaky/path"),
    Err(CheckedPathError::UnexpectedRoot),
);
Source

pub fn with_file_name<S: AsRef<[u8]>>(&self, file_name: S) -> TypedPathBuf

Creates an owned TypedPathBuf like self but with the given file name.

See TypedPathBuf::set_file_name for more details.

§Examples
use typed_path::{TypedPath, TypedPathBuf};

let path = TypedPath::derive("/tmp/foo.txt");
assert_eq!(path.with_file_name("bar.txt"), TypedPathBuf::from("/tmp/bar.txt"));

let path = TypedPath::derive("/tmp");
assert_eq!(path.with_file_name("var"), TypedPathBuf::from("/var"));
Source

pub fn with_extension<S: AsRef<[u8]>>(&self, extension: S) -> TypedPathBuf

Creates an owned TypedPathBuf like self but with the given extension.

See TypedPathBuf::set_extension for more details.

§Examples
use typed_path::{TypedPath, TypedPathBuf};

let path = TypedPath::derive("foo.rs");
assert_eq!(path.with_extension("txt"), TypedPathBuf::from("foo.txt"));

let path = TypedPath::derive("foo.tar.gz");
assert_eq!(path.with_extension(""), TypedPathBuf::from("foo.tar"));
assert_eq!(path.with_extension("xz"), TypedPathBuf::from("foo.tar.xz"));
assert_eq!(path.with_extension("").with_extension("txt"), TypedPathBuf::from("foo.txt"));
Source

pub fn components(&self) -> TypedComponents<'a>

Produces an iterator over the TypedComponents of the path.

When parsing the path, there is a small amount of normalization:

  • Repeated separators are ignored, so a/b and a//b both have a and b as components.

  • Occurrences of . are normalized away, except if they are at the beginning of the path. For example, a/./b, a/b/, a/b/. and a/b all have a and b as components, but ./a/b starts with an additional CurDir component.

  • A trailing slash is normalized away, /a/b and /a/b/ are equivalent.

Note that no other normalization takes place; in particular, a/c and a/b/../c are distinct, to account for the possibility that b is a symbolic link (so its parent isn’t a).

§Examples
use typed_path::{TypedPath, TypedComponent};

let mut components = TypedPath::derive("/tmp/foo.txt").components();

assert!(components.next().unwrap().is_root());
assert_eq!(components.next().unwrap().as_normal_bytes(), Some(b"tmp".as_slice()));
assert_eq!(components.next().unwrap().as_normal_bytes(), Some(b"foo.txt".as_slice()));
assert_eq!(components.next(), None)
Examples found in repository?
examples/typed.rs (line 7)
3fn main() {
4    // Try to be smart to figure out the path (Unix or Windows) automatically
5    let path = TypedPath::derive(r"/path/to/file.txt");
6
7    for component in path.components() {
8        println!("{}", String::from_utf8_lossy(component.as_bytes()));
9    }
10}
Source

pub fn iter(&self) -> TypedIter<'a>

Produces an iterator over the path’s components viewed as [[u8]] slices.

For more information about the particulars of how the path is separated into components, see components.

§Examples
use typed_path::TypedPath;

let mut it = TypedPath::derive("/tmp/foo.txt").iter();

assert_eq!(it.next(), Some(typed_path::constants::unix::SEPARATOR_STR.as_bytes()));
assert_eq!(it.next(), Some(b"tmp".as_slice()));
assert_eq!(it.next(), Some(b"foo.txt".as_slice()));
assert_eq!(it.next(), None)
Source

pub fn display(&self) -> impl Display + '_

Returns an object that implements Display for safely printing paths that may contain non-Unicode data. This may perform lossy conversion, depending on the platform. If you would like an implementation which escapes the path please use Debug instead.

§Examples
use typed_path::TypedPath;

let path = TypedPath::derive("/tmp/foo.rs");

println!("{}", path.display());
Source

pub fn is_unix(&self) -> bool

Returns true if this path represents a Unix path.

Source

pub fn is_windows(&self) -> bool

Returns true if this path represents a Windows path.

Source

pub fn with_unix_encoding(&self) -> TypedPathBuf

Converts this TypedPath into the Unix variant of TypedPathBuf.

Source

pub fn with_unix_encoding_checked( &self, ) -> Result<TypedPathBuf, CheckedPathError>

Converts this TypedPath into the Unix variant of TypedPathBuf, ensuring it is a valid Unix path.

Source

pub fn with_windows_encoding(&self) -> TypedPathBuf

Converts this TypedPath into the Windows variant of TypedPathBuf.

Source

pub fn with_windows_encoding_checked( &self, ) -> Result<TypedPathBuf, CheckedPathError>

Converts this TypedPath into the Windows variant of TypedPathBuf, ensuring it is a valid Windows path.

Trait Implementations§

Source§

impl AsRef<[u8]> for TypedPath<'_>

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Clone for TypedPath<'a>

Source§

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

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

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for TypedPath<'a>

Source§

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

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

impl<'a> From<&'a [u8]> for TypedPath<'a>

Source§

fn from(s: &'a [u8]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a str> for TypedPath<'a>

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl<'a> Hash for TypedPath<'a>

Source§

fn hash<__H: Hasher>(&self, state: &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<'a> Ord for TypedPath<'a>

Source§

fn cmp(&self, other: &TypedPath<'a>) -> 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 PartialEq<TypedPath<'_>> for TypedPathBuf

Source§

fn eq(&self, path: &TypedPath<'_>) -> 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 PartialEq<TypedPathBuf> for TypedPath<'_>

Source§

fn eq(&self, path: &TypedPathBuf) -> 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> PartialEq for TypedPath<'a>

Source§

fn eq(&self, other: &TypedPath<'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 TypedPath<'a>

Source§

fn partial_cmp(&self, other: &TypedPath<'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 TryAsRef<Path<UnixEncoding>> for TypedPath<'_>

Source§

impl TryAsRef<Path<WindowsEncoding>> for TypedPath<'_>

Source§

impl<'a> Copy for TypedPath<'a>

Source§

impl<'a> Eq for TypedPath<'a>

Source§

impl<'a> StructuralPartialEq for TypedPath<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for TypedPath<'a>

§

impl<'a> RefUnwindSafe for TypedPath<'a>

§

impl<'a> Send for TypedPath<'a>

§

impl<'a> Sync for TypedPath<'a>

§

impl<'a> Unpin for TypedPath<'a>

§

impl<'a> UnwindSafe for TypedPath<'a>

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.