Skip to content

How can I use BTreeMap instead of HashMap? #124

Description

@HairlessVillager

I need to ensure that the results of serialization and deserialization are consistent each time. However, HashMap salts the hash process, resulting in inconsistent serialization and deserialization outcomes. I think replacing HashMap with BTreeMap should work, as the latter maintains the lexicographical order of keys. I'd like to ask how this can be implemented.

I've tried to use a customed enum:

enum BTreeMapValue {
    Byte(i8),
    Short(i16),
    Int(i32),
    Long(i64),
    Float(f32),
    Double(f64),
    String(String),
    ByteArray(ByteArray),
    IntArray(IntArray),
    LongArray(LongArray),
    List(Vec<BTreeMapValue>),
    Compound(BTreeMap<String, BTreeMapValue>),
}
let x = BTreeMapValue::Compound(BTreeMap::from([(
    "key1".to_string(),
    BTreeMapValue::String("value1".to_string()),
)]));
let y = fastnbt::to_bytes(&x)?;
println!("{:?}", y);

But when I call .to_bytes, it always raise Error: Error("invalid nbt: no root compound").

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions