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
impl NbtCompound
sourcepub fn inner(&self) -> &Map<NbtTag>
pub fn inner(&self) -> &Map<NbtTag>
Returns a reference to the internal hash map of this compound.
sourcepub fn inner_mut(&mut self) -> &mut Map<NbtTag>
pub fn inner_mut(&mut self) -> &mut Map<NbtTag>
Returns a mutable reference to the internal hash map of this compound.
sourcepub fn into_inner(self) -> Map<NbtTag>
pub fn into_inner(self) -> Map<NbtTag>
Returns the internal hash map of this NBT compound.
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Returns a new NBT tag compound with the given initial capacity.
sourcepub fn clone_from<'a, K, V, M>(map: &'a M) -> Self
pub fn clone_from<'a, K, V, M>(map: &'a M) -> Self
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
);
sourcepub fn clone_repr_from<'a, K, V, M>(map: &'a M) -> Self
👎Deprecated since 0.2.3: This method will eventually be made obsolete with serde compatibility
pub fn clone_repr_from<'a, K, V, M>(map: &'a M) -> Self
Creates an NbtCompound
of NbtCompound
s by mapping each element in the given map to its
NBT representation.
sourcepub fn iter_map<'a, T: TryFrom<&'a NbtTag>>(
&'a self
) -> impl Iterator<Item = (&'a str, Result<T, <T as TryFrom<&'a NbtTag>>::Error>)> + 'a
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.
sourcepub 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
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.
sourcepub fn to_snbt(&self) -> String
pub fn to_snbt(&self) -> String
Converts this tag compound into a valid SNBT string. See NbtTag::
to_snbt
for details.
sourcepub fn to_pretty_snbt(&self) -> String
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.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the length of this compound is zero, false otherwise.
sourcepub fn get<'a, 'b, K, T>(&'a self, name: &'b K) -> Result<T, NbtReprError>
pub fn get<'a, 'b, K, T>(&'a self, name: &'b K) -> Result<T, NbtReprError>
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
sourcepub fn get_mut<'a, 'b, K, T>(
&'a mut self,
name: &'b K
) -> Result<T, NbtReprError>
pub fn get_mut<'a, 'b, K, T>( &'a mut self, name: &'b K ) -> Result<T, NbtReprError>
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
sourcepub fn contains_key<K>(&self, key: &K) -> bool
pub fn contains_key<K>(&self, key: &K) -> bool
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"));
sourcepub fn insert<K: Into<String>, T: Into<NbtTag>>(&mut self, name: K, value: T)
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);
sourcepub fn from_snbt(input: &str) -> Result<Self, SnbtError>
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
impl Clone for NbtCompound
source§fn clone(&self) -> NbtCompound
fn clone(&self) -> NbtCompound
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for NbtCompound
impl Debug for NbtCompound
source§impl Default for NbtCompound
impl Default for NbtCompound
source§impl<'de> Deserialize<'de> for NbtCompound
impl<'de> Deserialize<'de> for NbtCompound
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl Display for NbtCompound
impl Display for NbtCompound
source§impl Extend<(String, NbtTag)> for NbtCompound
impl Extend<(String, NbtTag)> for NbtCompound
source§fn extend<T: IntoIterator<Item = (String, NbtTag)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (String, NbtTag)>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl From<NbtCompound> for NbtTag
impl From<NbtCompound> for NbtTag
source§fn from(value: NbtCompound) -> NbtTag
fn from(value: NbtCompound) -> NbtTag
source§impl FromIterator<(String, NbtTag)> for NbtCompound
impl FromIterator<(String, NbtTag)> for NbtCompound
source§impl FromStr for NbtCompound
impl FromStr for NbtCompound
source§impl<Q> Index<&Q> for NbtCompound
impl<Q> Index<&Q> for NbtCompound
source§impl<'a> IntoIterator for &'a NbtCompound
impl<'a> IntoIterator for &'a NbtCompound
source§impl<'a> IntoIterator for &'a mut NbtCompound
impl<'a> IntoIterator for &'a mut NbtCompound
source§impl IntoIterator for NbtCompound
impl IntoIterator for NbtCompound
source§impl PartialEq for NbtCompound
impl PartialEq for NbtCompound
source§fn eq(&self, other: &NbtCompound) -> bool
fn eq(&self, other: &NbtCompound) -> bool
self
and other
values to be equal, and is used
by ==
.