aboutsummaryrefslogtreecommitdiff
path: root/azalea-nbt/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-04-22 18:03:57 +0000
committermat <github@matdoes.dev>2022-04-22 18:03:57 +0000
commit99652200aa2840e9a3075f238d9eb001aadb7e80 (patch)
treeb9dab6659f7245c11d7256b539b00c4d5e656e10 /azalea-nbt/src
parent7df6522489b17059a1033ff17245495aa39253d2 (diff)
downloadazalea-drasl-99652200aa2840e9a3075f238d9eb001aadb7e80.tar.xz
simplify nbt list optimization
Diffstat (limited to 'azalea-nbt/src')
-rwxr-xr-xazalea-nbt/src/encode.rs28
-rwxr-xr-xazalea-nbt/src/tag.rs27
2 files changed, 40 insertions, 15 deletions
diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs
index ebd1070f..ef72dce2 100755
--- a/azalea-nbt/src/encode.rs
+++ b/azalea-nbt/src/encode.rs
@@ -54,29 +54,27 @@ impl Tag {
match first_tag {
Self::Int(_) => {
for i in value {
- if let Tag::Int(v) = i {
- writer.write_i32::<BE>(*v)?
- } else {
- panic!("List of Ints should only contain Ints")
- }
+ writer.write_i32::<BE>(
+ *i.as_int().expect("List of Int should only contains Int"),
+ )?;
}
}
Self::String(_) => {
for i in value {
- if let Tag::String(v) = i {
- write_string(writer, v)?;
- } else {
- panic!("List of Strings should only contain Strings")
- }
+ write_string(
+ writer,
+ i.as_string()
+ .expect("List of String should only contain String"),
+ )?;
}
}
&Self::Compound(_) => {
for i in value {
- if let Tag::Compound(v) = i {
- write_compound(writer, v)?;
- } else {
- panic!("List of Compounds should only contain Compounds")
- }
+ write_compound(
+ writer,
+ i.as_compound()
+ .expect("List of Compound should only contain Compound"),
+ )?;
}
}
_ => {
diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs
index f11b8889..705dff6a 100755
--- a/azalea-nbt/src/tag.rs
+++ b/azalea-nbt/src/tag.rs
@@ -36,4 +36,31 @@ impl Tag {
Tag::LongArray(_) => 12,
}
}
+
+ #[inline]
+ pub fn as_int(&self) -> Option<&i32> {
+ if let Tag::Int(v) = self {
+ Some(v)
+ } else {
+ None
+ }
+ }
+
+ #[inline]
+ pub fn as_string(&self) -> Option<&str> {
+ if let Tag::String(v) = self {
+ Some(v)
+ } else {
+ None
+ }
+ }
+
+ #[inline]
+ pub fn as_compound(&self) -> Option<&HashMap<String, Tag>> {
+ if let Tag::Compound(v) = self {
+ Some(v)
+ } else {
+ None
+ }
+ }
}