Skip to content

Commit

Permalink
quicklog(serialize): remove 'static requirement on buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
thog92 committed Feb 29, 2024
1 parent 7f0d3c6 commit 4478245
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion quicklog/benches/logger_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Nested {
}

impl Serialize for BigStruct {
fn encode(&self, write_buf: &'static mut [u8]) -> Store {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
fn decode(buf: &[u8]) -> String {
let (mut _head, mut tail) = buf.split_at(0);
let mut vec = vec![];
Expand Down
2 changes: 1 addition & 1 deletion quicklog/examples/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl std::fmt::Display for S {
}

impl Serialize for S {
fn encode(&self, write_buf: &'static mut [u8]) -> Store {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
encode_i32(self.i, write_buf)
}

Expand Down
20 changes: 10 additions & 10 deletions quicklog/src/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::{fmt::Display, str::from_utf8};
pub mod buffer;

/// Allows specification of a custom way to serialize the Struct.
/// Additionally, this stores the contents serialized onto a static buffer, which does
/// Additionally, this stores the contents serialized into a buffer, which does
/// not require allocation and could speed things up.
pub trait Serialize {
fn encode(&self, write_buf: &'static mut [u8]) -> Store;
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf>;
fn buffer_size_required(&self) -> usize;
}

Expand All @@ -16,13 +16,13 @@ pub type DecodeFn = fn(&[u8]) -> String;
/// Contains the decode function required to decode `buffer` back into a `String`
/// representation.
#[derive(Clone)]
pub struct Store {
pub struct Store<'buf> {
decode_fn: DecodeFn,
buffer: &'static [u8],
buffer: &'buf [u8],
}

impl Store {
pub fn new(decode_fn: DecodeFn, buffer: &'static [u8]) -> Store {
impl Store<'_> {
pub fn new(decode_fn: DecodeFn, buffer: &[u8]) -> Store {
Store { decode_fn, buffer }
}

Expand All @@ -31,15 +31,15 @@ impl Store {
}
}

impl Display for Store {
impl Display for Store<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}

macro_rules! gen_encode_decode {
($name:ident, $primitive:ty) => {
pub fn $name(val: $primitive, write_buf: &'static mut [u8]) -> Store {
pub fn $name(val: $primitive, write_buf: &mut [u8]) -> Store {
assert!(std::mem::size_of::<$primitive>() == write_buf.len());

fn decode(read_buf: &[u8]) -> String {
Expand All @@ -61,7 +61,7 @@ gen_encode_decode!(encode_f32, f32);
gen_encode_decode!(encode_f64, f64);
gen_encode_decode!(encode_usize, usize);

pub fn encode_str(val: &str, write_buf: &'static mut [u8]) -> Store {
pub fn encode_str<'buf>(val: &str, write_buf: &'buf mut [u8]) -> Store<'buf> {
assert!(val.len() == write_buf.len());
fn decode(read_buf: &[u8]) -> String {
let x = from_utf8(read_buf).unwrap();
Expand All @@ -72,7 +72,7 @@ pub fn encode_str(val: &str, write_buf: &'static mut [u8]) -> Store {
}

/// Eager evaluation into a String for debug structs
pub fn encode_debug<T: std::fmt::Debug>(val: T, write_buf: &'static mut [u8]) -> Store {
pub fn encode_debug<T: std::fmt::Debug>(val: T, write_buf: &mut [u8]) -> Store {
let val_string = format!("{:?}", val);
assert!(val_string.len() == write_buf.len());

Expand Down
4 changes: 2 additions & 2 deletions quicklog/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub(crate) struct SerializeStruct {
}

impl Serialize for SerializeStruct {
fn encode(&self, write_buf: &'static mut [u8]) -> Store {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
fn decode(read_buf: &[u8]) -> String {
let x = std::str::from_utf8(read_buf).unwrap();
x.to_string()
Expand All @@ -126,7 +126,7 @@ pub(crate) struct BigStruct {
}

impl Serialize for BigStruct {
fn encode(&self, write_buf: &'static mut [u8]) -> Store {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
fn decode(buf: &[u8]) -> String {
let (mut _head, mut tail) = buf.split_at(0);
let mut vec = vec![];
Expand Down

0 comments on commit 4478245

Please sign in to comment.