pub struct ResourceIdentifier<'a>(_);
Expand description

A namespaced identifier for an undetermined type of resource.

A valid resource location has a format of "namespace:path". If the namespace portion is left out, then "minecraft" is the implied namespace.

Borrowing / Ownership

To avoid cloning / String construction when not necessary, this type can either borrow or take ownership of the underlying string.

By default, no copying or allocating is done. You must call into_owned() to get an owned identifier.

Implementations

Returns this identifier’s underlying string representation.

Example
let ident = ResourceIdentifier::from("stone");
assert_eq!(ident.as_str(), "stone");

let ident = ResourceIdentifier::from("minecraft:dirt");
assert_eq!(ident.as_str(), "minecraft:dirt");

Returns whether or not this resource location includes an explicit namespace.

Example
let id = ResourceIdentifier::from("foo:bar");
assert!(id.has_namespace());

let id = ResourceIdentifier::from("bar");
assert!(!id.has_namespace());

Returns the namespace portion of the resource location.

Example
let id = ResourceIdentifier::from("foo:bar");
assert_eq!(id.namespace(), "foo");

let id = ResourceIdentifier::from("bar");
assert_eq!(id.namespace(), "minecraft");

let id = ResourceIdentifier::from(":bar");
assert_eq!(id.namespace(), "");

Returns the path portion of the resource location.

Example
let id = ResourceIdentifier::from("foo:bar");
assert_eq!(id.path(), "bar");

let id = ResourceIdentifier::from("bar");
assert_eq!(id.path(), "bar");

let id = ResourceIdentifier::from("foo:");
assert_eq!(id.path(), "");

Returns a new identifier with a canonical representation (i.e., containing an explicit namespace).

This will involve allocating a new String if self does not already contain an explicit namespace.

Examples

Prepends the default namespace when one is not present:

let ident = ResourceIdentifier::from("stone");
let canonical = ident.to_canonical();

assert_eq!(canonical.as_str(), "minecraft:stone");

Performs a shallow copy when a namespace is already present:

let ident = ResourceIdentifier::from("foo:bar");
let canonical = ident.to_canonical();

assert_eq!(canonical.as_str(), "foo:bar");

// Prove that it was a cheap copy.
assert_eq!(
    ident.as_str().as_ptr() as usize,
    canonical.as_str().as_ptr() as usize,
);

Returns a new ResourceIdentifier that owns the underlying string.

This is useful for, e.g., storing the identifier in a data structure or passing it to another thread.

By default, all ResourceIdentifiers borrow the string they are constructed with, so no copying will occur unless you call this function.

Examples

Constructing an identifier using From simply borrows the data:

let string = String::from("my:ident");

let ident = ResourceIdentifier::from(&string);

// Identifier borrows data from `string`, cannot be sent across threads.
std::thread::spawn(move || println!("{}", ident));

Calling into_owned() on the identifier allows it to be sent to the thread:

let string = String::from("my:ident");

let ident = ResourceIdentifier::from(&string);
let ident = ident.into_owned();

std::thread::spawn(move || println!("{}", ident));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.