aboutsummaryrefslogtreecommitdiff
path: root/azalea-nbt/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2023-03-22 13:52:38 +0000
committermat <github@matdoes.dev>2023-03-22 13:52:38 +0000
commit6738be8090cfe4f9c5af14a45ba8112baf3a04c1 (patch)
treec01c3112c2aa258d77e523de7b07aabdb2af89ec /azalea-nbt/src
parentb27697819550e643057bd4190984c2a26f4465d8 (diff)
downloadazalea-drasl-6738be8090cfe4f9c5af14a45ba8112baf3a04c1.tar.xz
use compact_str in nbt
Diffstat (limited to 'azalea-nbt/src')
-rwxr-xr-xazalea-nbt/src/decode.rs7
-rwxr-xr-xazalea-nbt/src/encode.rs3
-rwxr-xr-xazalea-nbt/src/lib.rs12
-rwxr-xr-xazalea-nbt/src/tag.rs7
4 files changed, 16 insertions, 13 deletions
diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs
index 1ec7a912..6ec4bf13 100755
--- a/azalea-nbt/src/decode.rs
+++ b/azalea-nbt/src/decode.rs
@@ -3,6 +3,7 @@ use crate::Tag;
use ahash::AHashMap;
use azalea_buf::{BufReadError, McBufReadable};
use byteorder::{ReadBytesExt, BE};
+use compact_str::CompactString;
use flate2::read::{GzDecoder, ZlibDecoder};
use log::warn;
use std::io::Cursor;
@@ -20,17 +21,17 @@ fn read_bytes<'a>(buf: &'a mut Cursor<&[u8]>, length: usize) -> Result<&'a [u8],
}
#[inline]
-fn read_string(stream: &mut Cursor<&[u8]>) -> Result<String, Error> {
+fn read_string(stream: &mut Cursor<&[u8]>) -> Result<CompactString, Error> {
let length = stream.read_u16::<BE>()? as usize;
let buf = read_bytes(stream, length)?;
Ok(if let Ok(string) = std::str::from_utf8(buf) {
- string.to_string()
+ string.into()
} else {
let lossy_string = String::from_utf8_lossy(buf).into_owned();
warn!("Error decoding utf8 (bytes: {buf:?}, lossy: \"{lossy_string})\"");
- lossy_string
+ lossy_string.into()
})
}
diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs
index 36109f35..09cfffac 100755
--- a/azalea-nbt/src/encode.rs
+++ b/azalea-nbt/src/encode.rs
@@ -3,6 +3,7 @@ use crate::Tag;
use ahash::AHashMap;
use azalea_buf::McBufWritable;
use byteorder::{WriteBytesExt, BE};
+use compact_str::CompactString;
use flate2::write::{GzEncoder, ZlibEncoder};
use std::io::Write;
@@ -17,7 +18,7 @@ fn write_string(writer: &mut dyn Write, string: &str) -> Result<(), Error> {
#[inline]
fn write_compound(
writer: &mut dyn Write,
- value: &AHashMap<String, Tag>,
+ value: &AHashMap<CompactString, Tag>,
end_tag: bool,
) -> Result<(), Error> {
for (key, tag) in value {
diff --git a/azalea-nbt/src/lib.rs b/azalea-nbt/src/lib.rs
index 4d096776..0ceca39f 100755
--- a/azalea-nbt/src/lib.rs
+++ b/azalea-nbt/src/lib.rs
@@ -20,10 +20,10 @@ mod tests {
fn mcbuf_nbt() {
let mut buf = Vec::new();
let tag = Tag::Compound(AHashMap::from_iter(vec![(
- "hello world".to_string(),
+ "hello world".into(),
Tag::Compound(AHashMap::from_iter(vec![(
- "name".to_string(),
- Tag::String("Bananrama".to_string()),
+ "name".into(),
+ Tag::String("Bananrama".into()),
)])),
)]));
tag.write_into(&mut buf).unwrap();
@@ -34,10 +34,10 @@ mod tests {
assert_eq!(
result,
Tag::Compound(AHashMap::from_iter(vec![(
- "hello world".to_string(),
+ "hello world".into(),
Tag::Compound(AHashMap::from_iter(vec![(
- "name".to_string(),
- Tag::String("Bananrama".to_string()),
+ "name".into(),
+ Tag::String("Bananrama".into()),
)])),
)]))
);
diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs
index 87a7374d..4d1e08b8 100755
--- a/azalea-nbt/src/tag.rs
+++ b/azalea-nbt/src/tag.rs
@@ -1,5 +1,6 @@
use ahash::AHashMap;
+use compact_str::CompactString;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@@ -17,9 +18,9 @@ pub enum Tag {
Float(f32) = 5,
Double(f64) = 6,
ByteArray(Vec<u8>) = 7,
- String(String) = 8,
+ String(CompactString) = 8,
List(Vec<Tag>) = 9,
- Compound(AHashMap<String, Tag>) = 10,
+ Compound(AHashMap<CompactString, Tag>) = 10,
IntArray(Vec<i32>) = 11,
LongArray(Vec<i64>) = 12,
}
@@ -107,7 +108,7 @@ impl Tag {
/// If the type is a compound, return the `AHashMap<String, Tag>`.
#[inline]
- pub fn as_compound(&self) -> Option<&AHashMap<String, Tag>> {
+ pub fn as_compound(&self) -> Option<&AHashMap<CompactString, Tag>> {
if let Tag::Compound(v) = self {
Some(v)
} else {