Skip to content

Commit

Permalink
switch from end to count, add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
PSeitz authored and Licenser committed Sep 29, 2023
1 parent 70b65e4 commit 4879981
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 35 deletions.
36 changes: 27 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,41 +875,59 @@ mod tests {
let mut d = String::from("[]");
let d = unsafe { d.as_bytes_mut() };
let simd = Deserializer::from_slice(d).expect("");
assert_eq!(simd.tape[1], Node::Array { len: 0, end: 2 });
assert_eq!(simd.tape[1], Node::Array { len: 0, count: 0 });
}

#[test]
fn count2() {
let mut d = String::from("[1]");
let d = unsafe { d.as_bytes_mut() };
let simd = Deserializer::from_slice(d).expect("");
assert_eq!(simd.tape[1], Node::Array { len: 1, end: 3 });
assert_eq!(simd.tape[1], Node::Array { len: 1, count: 1 });
}

#[test]
fn count3() {
let mut d = String::from("[1,2]");
let d = unsafe { d.as_bytes_mut() };
let simd = Deserializer::from_slice(d).expect("");
assert_eq!(simd.tape[1], Node::Array { len: 2, end: 4 });
assert_eq!(simd.tape[1], Node::Array { len: 2, count: 2 });
}

#[test]
fn count4() {
let mut d = String::from(" [ 1 , [ 3 ] , 2 ]");
let d = unsafe { d.as_bytes_mut() };
let simd = Deserializer::from_slice(d).expect("");
assert_eq!(simd.tape[1], Node::Array { len: 3, end: 6 });
assert_eq!(simd.tape[3], Node::Array { len: 1, end: 5 });
assert_eq!(simd.tape[1], Node::Array { len: 3, count: 4 });
assert_eq!(simd.tape[3], Node::Array { len: 1, count: 1 });
}

#[test]
fn count5() {
let mut d = String::from("[[],null,null]");
let d = unsafe { d.as_bytes_mut() };
let simd = Deserializer::from_slice(d).expect("");
assert_eq!(simd.tape[1], Node::Array { len: 3, end: 5 });
assert_eq!(simd.tape[2], Node::Array { len: 0, end: 3 });
assert_eq!(simd.tape[1], Node::Array { len: 3, count: 3 });
assert_eq!(simd.tape[2], Node::Array { len: 0, count: 0 });
}

#[test]
fn test_tape_object_simple() {
let mut d = String::from(r#" { "hello": 1 , "b": 1 }"#);
let d = unsafe { d.as_bytes_mut() };
let simd = Deserializer::from_slice(d).expect("");
assert_eq!(
simd.tape,
[
Node::Static(StaticNode::Null),
Node::Object { len: 2, count: 4 },
Node::String(r#"hello"#), // <-- This is already escaped
Node::Static(StaticNode::I64(1)),
Node::String("b"),
Node::Static(StaticNode::I64(1)),
]
);
}

#[test]
Expand All @@ -921,11 +939,11 @@ mod tests {
simd.tape,
[
Node::Static(StaticNode::Null),
Node::Object { len: 2, end: 9 },
Node::Object { len: 2, count: 7 },
Node::String(r#"hell"o"#), // <-- This is already escaped
Node::Static(StaticNode::I64(1)),
Node::String("b"),
Node::Array { len: 3, end: 9 },
Node::Array { len: 3, count: 3 },
Node::Static(StaticNode::I64(1)),
Node::Static(StaticNode::I64(2)),
Node::Static(StaticNode::I64(3))
Expand Down
14 changes: 7 additions & 7 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ where
Node::Static(StaticNode::U64(n)) => visitor.visit_u64(n),
#[cfg(feature = "128bit")]
Node::Static(StaticNode::U128(n)) => visitor.visit_u128(n),
Node::Array { len, end: _ } => visitor.visit_seq(CommaSeparated::new(self, len)),
Node::Object { len, end: _ } => visitor.visit_map(CommaSeparated::new(self, len)),
Node::Array { len, count: _ } => visitor.visit_seq(CommaSeparated::new(self, len)),
Node::Object { len, count: _ } => visitor.visit_map(CommaSeparated::new(self, len)),
}
}

Expand Down Expand Up @@ -234,7 +234,7 @@ where
V: Visitor<'de>,
{
// Parse the opening bracket of the sequence.
if let Ok(Node::Array { len, end: _ }) = self.next() {
if let Ok(Node::Array { len, count: _ }) = self.next() {
// Give the visitor access to each element of the sequence.
visitor.visit_seq(CommaSeparated::new(self, len))
} else {
Expand Down Expand Up @@ -297,7 +297,7 @@ where
V: Visitor<'de>,
{
// Parse the opening bracket of the sequence.
if let Ok(Node::Object { len, end: _ }) = self.next() {
if let Ok(Node::Object { len, count: _ }) = self.next() {
// Give the visitor access to each element of the sequence.
visitor.visit_map(CommaSeparated::new(self, len))
} else {
Expand All @@ -317,8 +317,8 @@ where
{
match self.next() {
// Give the visitor access to each element of the sequence.
Ok(Node::Object { len, end: _ }) => visitor.visit_map(CommaSeparated::new(self, len)),
Ok(Node::Array { len, end: _ }) => visitor.visit_seq(CommaSeparated::new(self, len)),
Ok(Node::Object { len, count: _ }) => visitor.visit_map(CommaSeparated::new(self, len)),
Ok(Node::Array { len, count: _ }) => visitor.visit_seq(CommaSeparated::new(self, len)),
_ => Err(Deserializer::error(ErrorType::ExpectedMap)),
}
}
Expand All @@ -335,7 +335,7 @@ where
{
// Parse the opening bracket of the sequence.
match self.next() {
Ok(Node::Object { len, end: _ }) if len == 1 => {
Ok(Node::Object { len, count: _ }) if len == 1 => {
// Give the visitor access to each element of the sequence.
// let value = ri!(visitor.visit_enum(VariantAccess::new(self)));
visitor.visit_enum(VariantAccess::new(self))
Expand Down
18 changes: 9 additions & 9 deletions src/stage2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl<'de> Deserializer<'de> {
}

last_start = r_i;
insert_res!(Node::Object { len: 0, end: 0 });
insert_res!(Node::Object { len: 0, count: 0 });

depth += 1;
cnt = 1;
Expand All @@ -326,7 +326,7 @@ impl<'de> Deserializer<'de> {
}

last_start = r_i;
insert_res!(Node::Array { len: 0, end: 0 });
insert_res!(Node::Array { len: 0, count: 0 });

depth += 1;
cnt = 1;
Expand Down Expand Up @@ -459,7 +459,7 @@ impl<'de> Deserializer<'de> {
s.add(depth).write((StackState::Object, last_start, cnt));
}
last_start = r_i;
insert_res!(Node::Object { len: 0, end: 0 });
insert_res!(Node::Object { len: 0, count: 0 });
depth += 1;
cnt = 1;
object_begin!();
Expand All @@ -470,7 +470,7 @@ impl<'de> Deserializer<'de> {
s.add(depth).write((StackState::Object, last_start, cnt));
}
last_start = r_i;
insert_res!(Node::Array { len: 0, end: 0 });
insert_res!(Node::Array { len: 0, count: 0 });
depth += 1;
cnt = 1;
array_begin!();
Expand All @@ -490,14 +490,14 @@ impl<'de> Deserializer<'de> {
match *res.as_mut_ptr().add(last_start) {
Node::Array {
ref mut len,
ref mut end,
count: ref mut end,
}
| Node::Object {
ref mut len,
ref mut end,
count: ref mut end,
} => {
*len = cnt;
*end = r_i;
*end = r_i - last_start - 1;
}
_ => unreachable!(),
};
Expand Down Expand Up @@ -571,7 +571,7 @@ impl<'de> Deserializer<'de> {
s.add(depth).write((StackState::Array, last_start, cnt));
}
last_start = r_i;
insert_res!(Node::Object { len: 0, end: 0 });
insert_res!(Node::Object { len: 0, count: 0 });
depth += 1;
cnt = 1;
object_begin!();
Expand All @@ -582,7 +582,7 @@ impl<'de> Deserializer<'de> {
s.add(depth).write((StackState::Array, last_start, cnt));
}
last_start = r_i;
insert_res!(Node::Array { len: 0, end: 0 });
insert_res!(Node::Array { len: 0, count: 0 });
depth += 1;
cnt = 1;
array_begin!();
Expand Down
4 changes: 2 additions & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ where
match unsafe { self.de.next_() } {
Node::Static(s) => Value::from(s),
Node::String(s) => Value::from(s),
Node::Array { len, end: _ } => self.parse_array(len),
Node::Object { len, end: _ } => self.parse_map(len),
Node::Array { len, count: _ } => self.parse_array(len),
Node::Object { len, count: _ } => self.parse_map(len),
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/value/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ impl<'value> Mutable for Value<'value> {
_ => None,
}
}
/// Get mutable access to a map.
///
/// ```rust
/// use simd_json::*;
///
/// let mut object: BorrowedValue = json!({
/// "answer": 23,
/// "key": 7
/// }).into();
/// assert_eq!(object["answer"], 23);
///
/// if let Some(inner) = object.as_object_mut() {
/// inner.insert("value".into(), BorrowedValue::from(json!({"nested": 42})));
/// }
/// assert_eq!(object["value"], json!({"nested": 42}));
///
/// ```
#[inline]
#[must_use]
fn as_object_mut(&mut self) -> Option<&mut Object<'value>> {
Expand Down Expand Up @@ -400,8 +417,8 @@ impl<'de> BorrowDeserializer<'de> {
match unsafe { self.0.next_() } {
Node::Static(s) => Value::Static(s),
Node::String(s) => Value::from(s),
Node::Array { len, end: _ } => self.parse_array(len),
Node::Object { len, end: _ } => self.parse_map(len),
Node::Array { len, count: _ } => self.parse_array(len),
Node::Object { len, count: _ } => self.parse_map(len),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/value/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ impl<'de> OwnedDeserializer<'de> {
match unsafe { self.de.next_() } {
Node::Static(s) => Value::Static(s),
Node::String(s) => Value::from(s),
Node::Array { len, end: _ } => self.parse_array(len),
Node::Object { len, end: _ } => self.parse_map(len),
Node::Array { len, count: _ } => self.parse_array(len),
Node::Object { len, count: _ } => self.parse_map(len),
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/value/tape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ pub enum Node<'input> {
Object {
/// The number of keys in the object
len: usize,
/// The end index of the object in the `Vec<Node>` array
end: usize,
/// The total number of nodes in the object, including subelements.
count: usize,
},
/// An array with a given size starts here. The next `size`
/// elements belong to it - values can be nested and have a
/// `size` of their own.
Array {
/// The number of elements in the array
len: usize,
/// The end index of the array in the `Vec<Node>` array
end: usize,
/// The total number of nodes in the array, including subelements.
count: usize,
},
/// A static value that is interned into the tape, it can
/// be directly taken and isn't nested.
Expand Down

0 comments on commit 4879981

Please sign in to comment.