Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/allow quick exit #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl<'de: 'a, 'a, R: io::Read> de::Deserializer<'de> for &'a mut Decoder<R> {

match tag {
0x0a => visitor.visit_map(MapDecoder::new(self)),
0x00 => visitor.visit_map(MapDecoder::new_seeded(self, 0x00)),
_ => Err(Error::NoRootCompound),
}
}
Expand All @@ -137,6 +138,10 @@ where
fn new(outer: &'a mut Decoder<R>) -> Self {
MapDecoder { outer, tag: None }
}

fn new_seeded(outer: &'a mut Decoder<R>, seed: u8) -> Self {
MapDecoder { outer, tag: Some(seed) }
}
}

impl<'de: 'a, 'a, R: io::Read + 'a> de::MapAccess<'de> for MapDecoder<'a, R> {
Expand All @@ -146,6 +151,11 @@ impl<'de: 'a, 'a, R: io::Read + 'a> de::MapAccess<'de> for MapDecoder<'a, R> {
where
K: de::DeserializeSeed<'de>,
{
if matches!(self.tag, Some(0x00)) {
// we exit early here - we know that the map contains nothing from a 0x00 headed object
return Ok(None);
}

let tag = raw::read_bare_byte(&mut self.outer.reader)?;

// NBT indicates the end of a compound type with a 0x00 tag.
Expand Down
14 changes: 12 additions & 2 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,25 @@ where

/// Serialize maps as `Tag_Compound` data.
#[inline]
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
if matches!(len, Some(0)) {
self.write_header(0, None)?;
return Ok(Compound::from_outer(self));
}

let header = self.header; // Circumvent strange borrowing errors.
self.write_header(0x0a, header)?;
Ok(Compound::from_outer(self))
}

/// Serialize structs as `Tag_Compound` data.
#[inline]
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
if len == 0 {
self.write_header(0, None)?;
return Ok(Compound::from_outer(self));
}

let header = self.header; // Circumvent strange borrowing errors.
self.write_header(0x0a, header)?;
Ok(Compound::from_outer(self))
Expand Down