Struct quartz_nbt::NbtCompound

source ·
pub struct NbtCompound(/* private fields */);
Expand description

The NBT tag compound type which is essentially just a wrapper for a hash map of string keys to tag values.

This type will implement both Serialize and Deserialize when the serde feature is enabled, however this type should still be read and written with the utilities in the io module when possible if speed is the main priority. See NbtTag for more details.

Implementations§

source§

impl NbtCompound

source

pub fn new() -> Self

Returns a new NBT tag compound with an empty internal hash map.

source

pub fn inner(&self) -> &Map<NbtTag>

Returns a reference to the internal hash map of this compound.

source

pub fn inner_mut(&mut self) -> &mut Map<NbtTag>

Returns a mutable reference to the internal hash map of this compound.

source

pub fn into_inner(self) -> Map<NbtTag>

Returns the internal hash map of this NBT compound.

source

pub fn with_capacity(capacity: usize) -> Self

Returns a new NBT tag compound with the given initial capacity.

source

pub fn clone_from<'a, K, V, M>(map: &'a M) -> Self
where K: Clone + Into<String> + 'a, V: Clone + Into<NbtTag> + 'a, &'a M: IntoIterator<Item = (&'a K, &'a V)>,

Clones the data in the given map and converts it into an NbtCompound.

§Examples
let mut map = HashMap::new();
map.insert("foo", 10i32);
map.insert("bar", -5i32);

let compound = NbtCompound::clone_from(&map);
assert_eq!(
    compound.get::<_, i32>("foo").unwrap() + compound.get::<_, i32>("bar").unwrap(),
    5i32
);
source

pub fn clone_repr_from<'a, K, V, M>(map: &'a M) -> Self
where K: Clone + Into<String> + 'a, V: NbtRepr + 'a, &'a M: IntoIterator<Item = (&'a K, &'a V)>,

👎Deprecated since 0.2.3: This method will eventually be made obsolete with serde compatibility

Creates an NbtCompound of NbtCompounds by mapping each element in the given map to its NBT representation.

source

pub fn iter_map<'a, T: TryFrom<&'a NbtTag>>( &'a self ) -> impl Iterator<Item = (&'a str, Result<T, <T as TryFrom<&'a NbtTag>>::Error>)> + 'a

Iterates over this tag compound, converting each tag reference into the specified type. Each key is paired with the result of the attempted conversion into the specified type. The iterator will not terminate even if some conversions fail.

source

pub fn iter_mut_map<'a, T: TryFrom<&'a mut NbtTag>>( &'a mut self ) -> impl Iterator<Item = (&'a str, Result<T, <T as TryFrom<&'a mut NbtTag>>::Error>)> + 'a

Iterates over this tag compound, converting each mutable tag reference into the specified type. See iter_map for details.

source

pub fn to_snbt(&self) -> String

Converts this tag compound into a valid SNBT string. See NbtTag::to_snbt for details.

source

pub fn to_pretty_snbt(&self) -> String

Converts this tag compound into a valid SNBT string with extra spacing for readability. See NbtTag::to_pretty_snbt for details.

source

pub fn len(&self) -> usize

Returns the number of tags in this compound.

source

pub fn is_empty(&self) -> bool

Returns true if the length of this compound is zero, false otherwise.

source

pub fn get<'a, 'b, K, T>(&'a self, name: &'b K) -> Result<T, NbtReprError>
where String: Borrow<K>, K: Hash + Eq + ?Sized, &'b K: Into<String>, T: TryFrom<&'a NbtTag>, T::Error: Into<Error>,

Returns the value of the tag with the given name, or an error if no tag exists with the given name or specified type. This method should be used to obtain primitives as well as shared references to lists and compounds.

let mut compound = NbtCompound::new();
compound.insert("test", 1.0f64);

assert!((compound.get::<_, f64>("test").unwrap() - 1.0f64).abs() < 1e-5);
assert!(compound.get::<_, i32>("test").is_err()); // Type mismatch
assert!(compound.get::<_, f64>("foo").is_err()); // Missing tag
source

pub fn get_mut<'a, 'b, K, T>( &'a mut self, name: &'b K ) -> Result<T, NbtReprError>
where String: Borrow<K>, K: Hash + Eq + ?Sized, &'b K: Into<String>, T: TryFrom<&'a mut NbtTag>, T::Error: Into<Error>,

Returns the value of the tag with the given name, or an error if no tag exists with the given name or specified type. This method should be used to obtain mutable references to lists and compounds.

let mut compound = NbtCompound::new();
compound.insert("test", 1.0f64);

*compound.get_mut::<_, &mut f64>("test").unwrap() *= 2.0;

assert!((compound.get::<_, f64>("test").unwrap() - 2.0f64).abs() < 1e-5);
assert!(compound.get::<_, i32>("test").is_err()); // Type mismatch
assert!(compound.get::<_, f64>("foo").is_err()); // Missing tag
source

pub fn contains_key<K>(&self, key: &K) -> bool
where String: Borrow<K>, K: Hash + Eq + ?Sized,

Returns whether or not this compound has a tag with the given name.

let mut compound = NbtCompound::new();
compound.insert("test", 1.0f64);

assert!(compound.contains_key("test"));
assert!(!compound.contains_key("foo"));
source

pub fn insert<K: Into<String>, T: Into<NbtTag>>(&mut self, name: K, value: T)

Adds the given value to this compound with the given name after wrapping that value in an NbtTag.

let mut compound = NbtCompound::new();
compound.insert("test", 1.0f64);

assert!((compound.get::<_, f64>("test").unwrap() - 1.0f64).abs() < 1e-5);
source

pub fn from_snbt(input: &str) -> Result<Self, SnbtError>

Parses a nbt compound from snbt

§Example
let tag = NbtCompound::from_snbt(r#"{string:Stuff, list:[I;1,2,3,4,5]}"#).unwrap();
assert!(matches!(tag.get::<_, &str>("string"), Ok("Stuff")));
assert_eq!(tag.get::<_, &[i32]>("list").unwrap(), vec![1,2,3,4,5].as_slice());

Trait Implementations§

source§

impl Clone for NbtCompound

source§

fn clone(&self) -> NbtCompound

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 Debug for NbtCompound

source§

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

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

impl Default for NbtCompound

source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for NbtCompound

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for NbtCompound

source§

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

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

impl Extend<(String, NbtTag)> for NbtCompound

source§

fn extend<T: IntoIterator<Item = (String, NbtTag)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl From<NbtCompound> for NbtTag

source§

fn from(value: NbtCompound) -> NbtTag

Converts to this type from the input type.
source§

impl FromIterator<(String, NbtTag)> for NbtCompound

source§

fn from_iter<T: IntoIterator<Item = (String, NbtTag)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl FromStr for NbtCompound

§

type Err = SnbtError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<Q> Index<&Q> for NbtCompound
where String: Borrow<Q>, Q: Eq + Hash + ?Sized,

§

type Output = NbtTag

The returned type after indexing.
source§

fn index(&self, key: &Q) -> &NbtTag

Performs the indexing (container[index]) operation. Read more
source§

impl<'a> IntoIterator for &'a NbtCompound

§

type IntoIter = <&'a HashMap<String, NbtTag> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
§

type Item = <&'a HashMap<String, NbtTag> as IntoIterator>::Item

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a> IntoIterator for &'a mut NbtCompound

§

type IntoIter = <&'a mut HashMap<String, NbtTag> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
§

type Item = (&'a String, &'a mut NbtTag)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for NbtCompound

§

type IntoIter = <HashMap<String, NbtTag> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
§

type Item = <HashMap<String, NbtTag> as IntoIterator>::Item

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for NbtCompound

source§

fn eq(&self, other: &NbtCompound) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for NbtCompound

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'a> TryFrom<&'a NbtTag> for &'a NbtCompound

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut NbtCompound

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for NbtCompound

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl StructuralPartialEq for NbtCompound

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> ToOwned for T
where T: Clone,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,