Struct quartz_nbt::NbtCompound[][src]

#[repr(transparent)]
pub struct NbtCompound(_);
Expand description

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

Implementations

impl NbtCompound[src]

pub fn new() -> Self[src]

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

pub fn into_inner(self) -> HashMap<String, NbtTag>[src]

Returns the internal hash map of this NBT compound.

pub fn with_capacity(capacity: usize) -> Self[src]

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

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)>, 
[src]

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
);

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)>, 
[src]

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

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
[src]

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.

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
[src]

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

pub fn to_snbt(&self) -> String[src]

Converts this tag compound into a valid SNBT string.

pub fn len(&self) -> usize[src]

Returns the number of tags in this compound.

pub fn is_empty(&self) -> bool[src]

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

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

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_eq!(compound.get::<_, f64>("test"), Ok(1.0f64));
assert_eq!(
    compound.get::<_, i32>("test"),
    Err(NbtReprError::Conversion(NbtStructureError::TypeMismatch))
);
assert_eq!(
    compound.get::<_, f64>("foo"),
    Err(NbtReprError::Structure(NbtStructureError::MissingTag))
);

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

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_eq!(compound.get::<_, f64>("test"), Ok(2.0f64));
assert_eq!(
    compound.get::<_, i32>("test"),
    Err(NbtReprError::Conversion(NbtStructureError::TypeMismatch))
);
assert_eq!(
    compound.get::<_, f64>("foo"),
    Err(NbtReprError::Structure(NbtStructureError::MissingTag))
);

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

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"));

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

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_eq!(compound.get::<_, f64>("test"), Ok(1.0f64));

pub fn from_snbt(input: &str) -> Result<Self, ParserError>[src]

Parses a nbt compound from snbt

Example

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

Trait Implementations

impl AsMut<HashMap<String, NbtTag, RandomState>> for NbtCompound[src]

fn as_mut(&mut self) -> &mut HashMap<String, NbtTag>[src]

Performs the conversion.

impl AsRef<HashMap<String, NbtTag, RandomState>> for NbtCompound[src]

fn as_ref(&self) -> &HashMap<String, NbtTag>[src]

Performs the conversion.

impl Clone for NbtCompound[src]

fn clone(&self) -> NbtCompound[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for NbtCompound[src]

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

Formats the value using the given formatter. Read more

impl Display for NbtCompound[src]

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

Formats the value using the given formatter. Read more

impl From<NbtCompound> for NbtTag[src]

fn from(value: NbtCompound) -> NbtTag[src]

Performs the conversion.

impl PartialEq<NbtCompound> for NbtCompound[src]

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

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

fn ne(&self, other: &NbtCompound) -> bool[src]

This method tests for !=.

impl<'a> TryFrom<&'a NbtTag> for &'a NbtCompound[src]

type Error = NbtStructureError

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut NbtCompound[src]

type Error = NbtStructureError

The type returned in the event of a conversion error.

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

Performs the conversion.

impl TryFrom<NbtTag> for NbtCompound[src]

type Error = NbtStructureError

The type returned in the event of a conversion error.

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

Performs the conversion.

impl StructuralPartialEq for NbtCompound[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

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

recently added

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

impl<T> ToString for T where
    T: Display + ?Sized
[src]

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.