Skip to content

Commit

Permalink
Add Allocator to Decode trait
Browse files Browse the repository at this point in the history
* Allow specifying a custom lifetime and allocator identifier
  • Loading branch information
udoprog committed Jan 2, 2025
1 parent 9486150 commit b98ca9b
Show file tree
Hide file tree
Showing 76 changed files with 1,141 additions and 510 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ The tradeoffs we will be showcasing to achieve speed here are:
We achieve this through the following methods:

```rust
use musli::alloc::System;
use musli::context::{ErrorMarker as Error, Ignore};
use musli::options::{self, Float, Integer, Width, Options};
use musli::storage::Encoding;
Expand All @@ -372,7 +373,7 @@ where
#[inline]
pub fn decode<'buf, T>(buf: &'buf [u8], alloc: &Slice<'_>) -> Result<T, Error>
where
T: Decode<'buf, Packed>,
T: Decode<'buf, Packed, System>,
{
let cx = Ignore::with_alloc(alloc);
ENCODING.from_slice_with(&cx, buf)
Expand Down
3 changes: 2 additions & 1 deletion asm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub mod generic {

#[cfg(feature = "musli")]
pub mod musli {
use musli::alloc::System;
use musli::context::{ErrorMarker as Error, Ignore};
use musli::options::{self, Options};
use musli::storage::Encoding;
Expand Down Expand Up @@ -220,7 +221,7 @@ pub mod musli {
#[inline(always)]
pub fn decode<'buf, T>(buf: &'buf [u8]) -> Result<T, Error>
where
T: Decode<'buf, Packed>,
T: Decode<'buf, Packed, System>,
{
let cx = Ignore::new();
ENCODING.from_slice_with(&cx, buf)
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-core/src/alloc/raw_vec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// A raw buffer allocated through an [`Allocator`].
///
/// [`Allocator`]: super::Allocator
/// [`Allocator`]: crate::Allocator
///
/// ## Examples
///
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::error::Error;
use core::fmt;
use core::str;

use crate::alloc::Allocator;
use crate::Allocator;

/// Provides ergonomic access to the serialization context.
///
Expand Down
1 change: 1 addition & 0 deletions crates/musli-core/src/de/as_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub trait AsDecoder {
Cx = Self::Cx,
Error = <Self::Cx as Context>::Error,
Mode = <Self::Cx as Context>::Mode,
Allocator = <Self::Cx as Context>::Allocator,
>
where
Self: 'this;
Expand Down
17 changes: 13 additions & 4 deletions crates/musli-core/src/de/decode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Allocator;

use super::Decoder;

/// Trait governing how types are decoded.
Expand All @@ -20,13 +22,16 @@ use super::Decoder;
/// Implementing manually:
///
/// ```
/// use musli::{Decode, Decoder};
/// use musli::{Allocator, Decode, Decoder};
///
/// struct MyType {
/// data: [u8; 128],
/// }
///
/// impl<'de, M> Decode<'de, M> for MyType {
/// impl<'de, M, A> Decode<'de, M, A> for MyType
/// where
/// A: Allocator,
/// {
/// fn decode<D>(decoder: D) -> Result<Self, D::Error>
/// where
/// D: Decoder<'de>,
Expand All @@ -37,7 +42,11 @@ use super::Decoder;
/// }
/// }
/// ```
pub trait Decode<'de, M>: Sized {
pub trait Decode<'de, M, A>
where
Self: Sized,
A: Allocator,
{
/// Whether the type is packed. Packed types can be bitwise copied if the
/// representation of the serialization format is identical to the memory
/// layout of the type.
Expand All @@ -53,5 +62,5 @@ pub trait Decode<'de, M>: Sized {
/// Decode the given input.
fn decode<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M>;
D: Decoder<'de, Mode = M, Allocator = A>;
}
17 changes: 13 additions & 4 deletions crates/musli-core/src/de/decode_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Allocator;

use super::Decoder;

/// Trait governing how types are decoded as bytes.
Expand All @@ -22,14 +24,17 @@ use super::Decoder;
/// Implementing manually:
///
/// ```
/// use musli::{Decode, Decoder};
/// use musli::{Allocator, Decode, Decoder};
/// use musli::de::DecodeBytes;
///
/// struct MyType {
/// data: [u8; 128],
/// }
///
/// impl<'de, M> Decode<'de, M> for MyType {
/// impl<'de, M, A> Decode<'de, M, A> for MyType
/// where
/// A: Allocator,
/// {
/// fn decode<D>(decoder: D) -> Result<Self, D::Error>
/// where
/// D: Decoder<'de>,
Expand All @@ -40,7 +45,11 @@ use super::Decoder;
/// }
/// }
/// ```
pub trait DecodeBytes<'de, M>: Sized {
pub trait DecodeBytes<'de, M, A>
where
Self: Sized,
A: Allocator,
{
/// Whether the type is packed. Packed types can be bitwise copied if the
/// representation of the serialization format is identical to the memory
/// layout of the type.
Expand All @@ -53,5 +62,5 @@ pub trait DecodeBytes<'de, M>: Sized {
/// Decode the given input as bytes.
fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M>;
D: Decoder<'de, Mode = M, Allocator = A>;
}
17 changes: 13 additions & 4 deletions crates/musli-core/src/de/decode_packed.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Allocator;

use super::Decoder;

/// Trait governing how a type is decoded as a packed value.
Expand Down Expand Up @@ -25,14 +27,17 @@ use super::Decoder;
/// Implementing manually:
///
/// ```
/// use musli::{Decode, Decoder};
/// use musli::{Allocator, Decode, Decoder};
/// use musli::de::SequenceDecoder;
///
/// struct Packed {
/// data: (u32, u32),
/// }
///
/// impl<'de, M> Decode<'de, M> for Packed {
/// impl<'de, M, A> Decode<'de, M, A> for Packed
/// where
/// A: Allocator,
/// {
/// fn decode<D>(decoder: D) -> Result<Self, D::Error>
/// where
/// D: Decoder<'de>,
Expand All @@ -45,9 +50,13 @@ use super::Decoder;
/// }
/// }
/// ```
pub trait DecodePacked<'de, M>: Sized {
pub trait DecodePacked<'de, M, A>
where
Self: Sized,
A: Allocator,
{
/// Decode the given input as bytes.
fn decode_packed<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M>;
D: Decoder<'de, Mode = M, Allocator = A>;
}
10 changes: 8 additions & 2 deletions crates/musli-core/src/de/decode_trace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Allocator;

use super::Decoder;

/// Trait governing how types are decoded specifically for tracing.
Expand All @@ -8,9 +10,13 @@ use super::Decoder;
///
/// [`HashMap<K, V>`]: std::collections::HashMap
/// [`fmt::Display`]: std::fmt::Display
pub trait DecodeTrace<'de, M>: Sized {
pub trait DecodeTrace<'de, M, A>
where
Self: Sized,
A: Allocator,
{
/// Decode the given input.
fn trace_decode<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M>;
D: Decoder<'de, Mode = M, Allocator = A>;
}
Loading

0 comments on commit b98ca9b

Please sign in to comment.