Struct minecraft_assets::api::ResourceIdentifier
source · [−]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
Auto Trait Implementations
impl<'a> RefUnwindSafe for ResourceIdentifier<'a>
impl<'a> Send for ResourceIdentifier<'a>
impl<'a> Sync for ResourceIdentifier<'a>
impl<'a> Unpin for ResourceIdentifier<'a>
impl<'a> UnwindSafe for ResourceIdentifier<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more