Skip to content

Commit ce0da33

Browse files
committed
quicklog(serialize): add decode to Serialize
1 parent 2e0bbdf commit ce0da33

File tree

7 files changed

+62
-54
lines changed

7 files changed

+62
-54
lines changed

quicklog/benches/logger_benchmark.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::fmt::Display;
2-
use std::str::from_utf8;
32
use std::sync::mpsc::{channel, Receiver, Sender};
43
use std::time::Duration;
54

@@ -27,17 +26,6 @@ struct Nested {
2726

2827
impl Serialize for BigStruct {
2928
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
30-
fn decode(buf: &[u8]) -> String {
31-
let (mut _head, mut tail) = buf.split_at(0);
32-
let mut vec = vec![];
33-
for _ in 0..100 {
34-
(_head, tail) = tail.split_at(4);
35-
vec.push(i32::from_le_bytes(_head.try_into().unwrap()));
36-
}
37-
let s = from_utf8(tail).unwrap();
38-
format!("vec: {:?}, str: {}", vec, s)
39-
}
40-
4129
let (mut _head, mut tail) = write_buf.split_at_mut(0);
4230
for i in 0..100 {
4331
(_head, tail) = tail.split_at_mut(4);
@@ -46,7 +34,18 @@ impl Serialize for BigStruct {
4634

4735
tail.copy_from_slice(self.some.as_bytes());
4836

49-
Store::new(decode, write_buf)
37+
Store::new(Self::decode, write_buf)
38+
}
39+
40+
fn decode(buf: &[u8]) -> (String, &[u8]) {
41+
let (mut _head, mut tail) = buf.split_at(0);
42+
let mut vec = vec![];
43+
for _ in 0..100 {
44+
(_head, tail) = tail.split_at(4);
45+
vec.push(i32::from_le_bytes(_head.try_into().unwrap()));
46+
}
47+
let (s, rest) = <&str as Serialize>::decode(tail);
48+
(format!("vec: {:?}, str: {}", vec, s), rest)
5049
}
5150

5251
fn buffer_size_required(&self) -> usize {

quicklog/benches/quicklog_benchmark.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::str::from_utf8;
2-
31
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
42
use delog::render::DefaultRenderer;
53
use quanta::Instant;

quicklog/examples/macros.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ impl Serialize for S {
2121
self.i.encode(write_buf)
2222
}
2323

24+
fn decode(read_buf: &[u8]) -> (String, &[u8]) {
25+
i32::decode(read_buf)
26+
}
27+
2428
fn buffer_size_required(&self) -> usize {
25-
std::mem::size_of_val(&self.i)
29+
self.i.buffer_size_required()
2630
}
2731
}
2832

quicklog/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
//! }
8989
//!
9090
//! impl Serialize for SomeStruct {
91-
//! fn encode(&self, write_buf: &'static mut [u8]) -> Store { /* some impl */ }
91+
//! fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> { /* some impl */ }
92+
//! fn decode(read_buf: &[u8]) -> (String, &[u8]) { /* some impl */ }
9293
//! fn buffer_size_required(&self) -> usize { /* some impl */ }
9394
//! }
9495
//!

quicklog/src/serialize/mod.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ pub mod buffer;
77
/// not require allocation and could speed things up.
88
pub trait Serialize {
99
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf>;
10+
fn decode(read_buf: &[u8]) -> (String, &[u8]);
1011
fn buffer_size_required(&self) -> usize;
1112
}
1213

1314
/// Function pointer which decodes a byte buffer back into `String` representation
14-
pub type DecodeFn = fn(&[u8]) -> String;
15+
pub type DecodeFn = fn(&[u8]) -> (String, &[u8]);
1516

1617
/// Contains the decode function required to decode `buffer` back into a `String`
1718
/// representation.
@@ -27,7 +28,8 @@ impl Store<'_> {
2728
}
2829

2930
pub fn as_string(&self) -> String {
30-
(self.decode_fn)(self.buffer)
31+
let (s, _) = (self.decode_fn)(self.buffer);
32+
s
3133
}
3234
}
3335

@@ -42,15 +44,18 @@ macro_rules! gen_serialize {
4244
impl Serialize for $primitive {
4345
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
4446
assert!(std::mem::size_of::<$primitive>() == write_buf.len());
45-
fn decode(read_buf: &[u8]) -> String {
46-
let x = <$primitive>::from_le_bytes(read_buf.try_into().unwrap());
47-
format!("{}", x)
48-
}
4947

5048
let size = std::mem::size_of::<$primitive>();
5149
let (x, _) = write_buf.split_at_mut(size);
5250
x.copy_from_slice(&self.to_le_bytes());
53-
Store::new(decode, &*x)
51+
Store::new(Self::decode, x)
52+
}
53+
54+
fn decode(read_buf: &[u8]) -> (String, &[u8]) {
55+
let (chunk, rest) = read_buf.split_at(std::mem::size_of::<$primitive>());
56+
let x = <$primitive>::from_le_bytes(chunk.try_into().unwrap());
57+
58+
(format!("{}", x), rest)
5459
}
5560

5661
fn buffer_size_required(&self) -> usize {
@@ -72,12 +77,13 @@ gen_serialize!(usize);
7277
impl Serialize for &str {
7378
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
7479
assert!(self.len() == write_buf.len());
75-
fn decode(read_buf: &[u8]) -> String {
76-
let x = from_utf8(read_buf).unwrap();
77-
x.to_string()
78-
}
7980
write_buf.copy_from_slice(self.as_bytes());
80-
Store::new(decode, write_buf)
81+
Store::new(Self::decode, write_buf)
82+
}
83+
84+
fn decode(read_buf: &[u8]) -> (String, &[u8]) {
85+
let x = from_utf8(read_buf).unwrap();
86+
(x.to_string(), &[])
8187
}
8288

8389
fn buffer_size_required(&self) -> usize {
@@ -92,9 +98,9 @@ pub fn encode_debug<T: std::fmt::Debug>(val: T, write_buf: &mut [u8]) -> Store {
9298
// `buffer_size_required`
9399
assert!(val_string.len() <= write_buf.len());
94100

95-
fn decode(read_buf: &[u8]) -> String {
101+
fn decode(read_buf: &[u8]) -> (String, &[u8]) {
96102
let x = from_utf8(read_buf).unwrap();
97-
x.to_string()
103+
(x.to_string(), &[])
98104
}
99105

100106
let (chunk, _) = write_buf.split_at_mut(val_string.len());
@@ -115,7 +121,7 @@ mod tests {
115121

116122
let x: $primitive = $val;
117123
let x_store = x.encode(&mut buf);
118-
assert_eq!(format!("{}", x), (x_store.decode_fn)(&buf));
124+
assert_eq!(format!("{}", x), format!("{}", x_store));
119125
}};
120126
}
121127

@@ -146,13 +152,9 @@ mod tests {
146152
let b_store = b.encode(b_chunk);
147153
let c_store = c.encode(c_chunk);
148154

149-
let a_str = (a_store.decode_fn)(a_store.buffer);
150-
let b_str = (b_store.decode_fn)(b_store.buffer);
151-
let c_str = (c_store.decode_fn)(c_store.buffer);
152-
153155
assert_eq!(
154156
format!("{} {} {}", a, b, c),
155-
format!("{} {} {}", a_str, b_str, c_str)
157+
format!("{} {} {}", a_store, b_store, c_store)
156158
)
157159
}
158160

quicklog/tests/common/mod.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@ pub(crate) struct SerializeStruct {
106106

107107
impl Serialize for SerializeStruct {
108108
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
109-
fn decode(read_buf: &[u8]) -> String {
110-
let x = std::str::from_utf8(read_buf).unwrap();
111-
x.to_string()
112-
}
113109
write_buf.copy_from_slice(self.symbol.as_bytes());
114-
Store::new(decode, write_buf)
110+
Store::new(Self::decode, write_buf)
111+
}
112+
113+
fn decode(read_buf: &[u8]) -> (String, &[u8]) {
114+
let x = std::str::from_utf8(read_buf).unwrap();
115+
(x.to_string(), &[])
115116
}
116117

117118
fn buffer_size_required(&self) -> usize {
@@ -127,17 +128,6 @@ pub(crate) struct BigStruct {
127128

128129
impl Serialize for BigStruct {
129130
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
130-
fn decode(buf: &[u8]) -> String {
131-
let (mut _head, mut tail) = buf.split_at(0);
132-
let mut vec = vec![];
133-
for _ in 0..100 {
134-
(_head, tail) = tail.split_at(4);
135-
vec.push(i32::from_le_bytes(_head.try_into().unwrap()));
136-
}
137-
let s = std::str::from_utf8(tail).unwrap();
138-
format!("vec: {:?}, str: {}", vec, s)
139-
}
140-
141131
let (mut _head, mut tail) = write_buf.split_at_mut(0);
142132
for i in 0..100 {
143133
(_head, tail) = tail.split_at_mut(4);
@@ -146,7 +136,18 @@ impl Serialize for BigStruct {
146136

147137
tail.copy_from_slice(self.some.as_bytes());
148138

149-
Store::new(decode, write_buf)
139+
Store::new(Self::decode, write_buf)
140+
}
141+
142+
fn decode(buf: &[u8]) -> (String, &[u8]) {
143+
let (mut _head, mut tail) = buf.split_at(0);
144+
let mut vec = vec![];
145+
for _ in 0..100 {
146+
(_head, tail) = tail.split_at(4);
147+
vec.push(i32::from_le_bytes(_head.try_into().unwrap()));
148+
}
149+
let s = std::str::from_utf8(tail).unwrap();
150+
(format!("vec: {:?}, str: {}", vec, s), &[])
150151
}
151152

152153
fn buffer_size_required(&self) -> usize {

rust-toolchain.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "1.72"
3+
profile = "default"

0 commit comments

Comments
 (0)