/rust/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.216/src/format.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::lib::fmt::{self, Write}; |
2 | | use crate::lib::str; |
3 | | |
4 | | pub(super) struct Buf<'a> { |
5 | | bytes: &'a mut [u8], |
6 | | offset: usize, |
7 | | } |
8 | | |
9 | | impl<'a> Buf<'a> { |
10 | 0 | pub fn new(bytes: &'a mut [u8]) -> Self { |
11 | 0 | Buf { bytes, offset: 0 } |
12 | 0 | } |
13 | | |
14 | 0 | pub fn as_str(&self) -> &str { |
15 | 0 | let slice = &self.bytes[..self.offset]; |
16 | 0 | unsafe { str::from_utf8_unchecked(slice) } |
17 | 0 | } |
18 | | } |
19 | | |
20 | | impl<'a> Write for Buf<'a> { |
21 | 0 | fn write_str(&mut self, s: &str) -> fmt::Result { |
22 | 0 | if self.offset + s.len() > self.bytes.len() { |
23 | 0 | Err(fmt::Error) |
24 | | } else { |
25 | 0 | self.bytes[self.offset..self.offset + s.len()].copy_from_slice(s.as_bytes()); |
26 | 0 | self.offset += s.len(); |
27 | 0 | Ok(()) |
28 | | } |
29 | 0 | } |
30 | | } |