Skip to content
This repository was archived by the owner on Aug 15, 2021. It is now read-only.

Commit 6fcd94a

Browse files
committed
Improvements
1 parent 9f3f8fd commit 6fcd94a

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/de.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,16 @@ where
281281
}
282282

283283
fn try_parse_u128(&mut self) -> Result<Option<u128>> {
284-
// ^ Only *try* parse because the value could be a non-u128 byte array.
284+
// ^ Only *try* parse because the value could be a non-u128 bytestring.
285285
let desc = match self.peek()? {
286286
Some(desc) => desc,
287287
None => return Ok(None),
288288
};
289289
let ty = desc >> 5;
290290
if ty != 2 {
291-
return Ok(None);
291+
return Ok(None); // not a bytestring
292292
}
293-
let len = desc & 0x1fu8;
293+
let len = desc & 0x1f;
294294
if len > 16 {
295295
return Ok(None);
296296
}
@@ -731,7 +731,7 @@ where
731731
Some(value) => visitor.visit_i128(-1 - value as i128),
732732
None => self.parse_value(visitor),
733733
},
734-
0xc1 | 0xc4..=0xd7 => self.parse_value(visitor),
734+
0xc0 | 0xc1 | 0xc4..=0xd7 => self.parse_value(visitor),
735735
0xd8 => {
736736
self.parse_u8()?;
737737
self.parse_value(visitor)
@@ -771,8 +771,7 @@ where
771771
}
772772
0xfc..=0xfe => Err(self.error(ErrorCode::UnassignedCode)),
773773
0xff => Err(self.error(ErrorCode::UnexpectedCode)),
774-
775-
_ => unreachable!(),
774+
_ => unreachable!(), // Remove this once minimum supported rustc version is 1.33.0.
776775
}
777776
}
778777
}

src/ser.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ where
183183
if len <= 8 {
184184
self.write_u64(tag - 2, value as u64)
185185
} else {
186+
let mut buf = [0u8; 2 + 16];
187+
BigEndian::write_u128(&mut buf[2..], value);
188+
let hdr_offset = 16 - len as usize;
189+
buf[hdr_offset] = 6 << 5 | tag;
190+
buf[hdr_offset + 1] = 2 << 5 | len as u8;
186191
self.writer
187-
.write_all(&[6 << 5 | tag, 2 << 5 | len as u8])
188-
.map_err(|e| e.into())?;
189-
let mut buf = [0u8; 16];
190-
BigEndian::write_u128(&mut buf, value);
191-
self.writer
192-
.write_all(&buf[(16 - len) as usize..])
192+
.write_all(&buf[hdr_offset..])
193193
.map_err(|e| e.into())
194194
}
195195
}

0 commit comments

Comments
 (0)