Skip to content

Commit

Permalink
Renaming fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ejmount committed Jun 9, 2020
1 parent 386039a commit 3b9d13f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/blob.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(clippy::redundant_field_names)]
#![allow(clippy::write_with_newline)]

use std::collections::HashMap;
use std::fmt;
use std::io;
Expand Down Expand Up @@ -153,7 +156,7 @@ impl Blob {
}

/// The number of bytes this blob will serialize to, before compression
pub fn serialized_size(&self) -> usize
pub fn len_bytes(&self) -> usize
{
1 /* compound tag */
+ 2 /* name length*/
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,5 @@ fn nbt_sizes() {
let mut cursor = std::io::Cursor::new(vec![]);
root.to_writer(&mut cursor).unwrap();

assert_eq!(cursor.position() as usize, root.serialized_size());
assert_eq!(cursor.position() as usize, root.len_bytes());
}
12 changes: 7 additions & 5 deletions src/value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::redundant_field_names)]
#![allow(clippy::write_with_newline)]
use std::collections::HashMap;
use std::fmt;
use std::io;
Expand Down Expand Up @@ -192,20 +194,20 @@ impl Value {
}

/// The number of bytes this value serializes to, before compression
pub fn serialized_size(&self) -> usize {
1 /* type ID */ + self.uncompressed_size_of_payload()
pub fn len_bytes(&self) -> usize {
1 /* type ID */ + self.len_payload()
}

/// Serialized size of an entry within a TAG_COMPOUND
/// Also used by Blob, so crate visible
pub(crate) fn size_of_compound_entry((key, value): (&String, &Value)) -> usize {
let key_len = 2 + key.len();
let value_len = value.serialized_size();
let value_len = value.len_bytes();
key_len + value_len
}

// The serialized size of the payload specifically, without tag IDs or names prepended.
fn uncompressed_size_of_payload(&self) -> usize {
fn len_payload(&self) -> usize {
use std::mem::size_of;
match self {
Value::Byte(_) => 1,
Expand All @@ -216,7 +218,7 @@ impl Value {
Value::Double(_) => 8,
Value::String(s) => 2 /* string size */ + s.len(),
Value::List(v) => {
1 /* item tag */ + 4 /* arr size */ + v.iter().map(Self::uncompressed_size_of_payload).sum::<usize>()
1 /* item tag */ + 4 /* arr size */ + v.iter().map(Self::len_payload).sum::<usize>()
}
Value::Compound(hm) => {
hm.iter().map(Self::size_of_compound_entry).sum::<usize>() + 1usize /* TAG_END */
Expand Down

0 comments on commit 3b9d13f

Please sign in to comment.