Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastnbt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde_bytes = "0.11.5"

[features]
arbitrary1 = ["arbitrary"]
btreemap = []

[dev-dependencies]
flate2 = "1"
Expand Down
4 changes: 2 additions & 2 deletions fastnbt/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ macro_rules! nbt_internal {
};

({}) => {
$crate::Value::Compound(std::collections::HashMap::new())
$crate::Value::Compound($crate::value::Map::new())
};

({ $($tt:tt)+ }) => {
$crate::Value::Compound({
let mut object = std::collections::HashMap::new();
let mut object = $crate::value::Map::new();
nbt_internal!(@object object () ($($tt)+) ($($tt)+));
object
})
Expand Down
15 changes: 7 additions & 8 deletions fastnbt/src/value/de.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::HashMap};
use std::borrow::Cow;

use serde::{
de::{
Expand All @@ -12,6 +12,8 @@ use serde_bytes::ByteBuf;

use crate::{error::Error, ByteArray, IntArray, LongArray, Value};

use super::Map;

impl<'de> Deserialize<'de> for Value {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -100,7 +102,7 @@ impl<'de> Deserialize<'de> for Value {
{
match map.next_key_seed(KeyClassifier)? {
Some(KeyClass::Compound(first_key)) => {
let mut compound = HashMap::new();
let mut compound = Map::new();

compound.insert(first_key, map.next_value()?);
while let Some((key, value)) = map.next_entry()? {
Expand Down Expand Up @@ -252,10 +254,7 @@ where
}
}

fn visit_compound<'de, V>(
compound: &'de HashMap<String, Value>,
visitor: V,
) -> Result<V::Value, Error>
fn visit_compound<'de, V>(compound: &'de Map<String, Value>, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
Expand Down Expand Up @@ -735,12 +734,12 @@ impl<'de> SeqAccess<'de> for SeqDeserializer<'de> {
}

struct MapDeserializer<'de> {
iter: <&'de HashMap<String, Value> as IntoIterator>::IntoIter,
iter: <&'de Map<String, Value> as IntoIterator>::IntoIter,
value: Option<&'de Value>,
}

impl<'de> MapDeserializer<'de> {
fn new(map: &'de HashMap<String, Value>) -> Self {
fn new(map: &'de Map<String, Value>) -> Self {
MapDeserializer {
iter: map.iter(),
value: None,
Expand Down
7 changes: 5 additions & 2 deletions fastnbt/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ mod array_serializer;
mod de;
mod ser;

use std::collections::HashMap;
#[cfg(feature = "btreemap")]
pub type Map<K, V> = std::collections::BTreeMap<K, V>;
#[cfg(not(feature = "btreemap"))]
pub type Map<K, V> = std::collections::HashMap<K, V>;

use serde::{serde_if_integer128, Deserialize, Serialize};

Expand Down Expand Up @@ -44,7 +47,7 @@ pub enum Value {
IntArray(IntArray),
LongArray(LongArray),
List(Vec<Value>),
Compound(HashMap<String, Value>),
Compound(Map<String, Value>),
}

#[cfg(feature = "arbitrary1")]
Expand Down
15 changes: 7 additions & 8 deletions fastnbt/src/value/ser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::result;
use std::collections::HashMap;

use serde::{ser::Impossible, serde_if_integer128, Serialize};

Expand All @@ -9,7 +8,7 @@ use crate::{
LONG_ARRAY_TOKEN,
};

use super::array_serializer::ArraySerializer;
use super::{array_serializer::ArraySerializer, Map};

impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
Expand Down Expand Up @@ -275,7 +274,7 @@ impl<'a> serde::Serializer for &'a mut Serializer {

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Ok(SerializeMap {
map: HashMap::new(),
map: Map::new(),
next_key: None,
})
}
Expand All @@ -293,7 +292,7 @@ impl<'a> serde::Serializer for &'a mut Serializer {
) -> Result<Self::SerializeStructVariant> {
Ok(SerializeStructVariant {
name: variant.into(),
map: HashMap::new(),
map: Map::new(),
})
}

Expand Down Expand Up @@ -327,13 +326,13 @@ pub struct SerializeTupleVariant {
}

pub struct SerializeMap {
map: HashMap<String, Value>,
map: Map<String, Value>,
next_key: Option<String>,
}

pub struct SerializeStructVariant {
name: String,
map: HashMap<String, Value>,
map: Map<String, Value>,
}

impl serde::ser::SerializeSeq for SerializeVec {
Expand Down Expand Up @@ -398,7 +397,7 @@ impl serde::ser::SerializeTupleVariant for SerializeTupleVariant {
}

fn end(self) -> Result<Value> {
let mut object = HashMap::new();
let mut object = Map::new();

object.insert(self.name, Value::List(self.vec));

Expand Down Expand Up @@ -661,7 +660,7 @@ impl serde::ser::SerializeStructVariant for SerializeStructVariant {
}

fn end(self) -> Result<Value> {
let mut object = HashMap::new();
let mut object = Map::new();

object.insert(self.name, Value::Compound(self.map));

Expand Down