Skip to content

Commit

Permalink
Move traits into musli crate
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Mar 13, 2024
1 parent 97d553e commit e0c4dd3
Show file tree
Hide file tree
Showing 27 changed files with 153 additions and 164 deletions.
4 changes: 2 additions & 2 deletions crates/musli-allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ categories = ["no-std", "memory-management", "rust-patterns"]

[features]
default = ["std"]
std = ["alloc", "musli/std"]
alloc = ["musli/alloc"]
std = ["alloc"]
alloc = []

[dependencies]
musli = { path = "../musli", version = "0.0.96", default-features = false }
4 changes: 1 addition & 3 deletions crates/musli-allocator/src/disabled.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use musli::context::Buf;

use crate::Allocator;
use musli::{Allocator, Buf};

/// An empty buffer.
pub struct EmptyBuf;
Expand Down
62 changes: 2 additions & 60 deletions crates/musli-allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub use self::fixed::{FixedString, FixedVec};
mod default_alloc {
#![allow(missing_docs)]

use super::Allocator;
use musli::Allocator;

pub struct DefaultBuffer(super::SystemBuffer);
pub struct Default<'a>(super::System<'a>);
Expand All @@ -60,7 +60,7 @@ mod default_alloc {
}

impl<'a> Allocator for Default<'a> {
type Buf<'this> = <super::System<'a> as super::Allocator>::Buf<'this>
type Buf<'this> = <super::System<'a> as Allocator>::Buf<'this>
where
Self: 'this;

Expand Down Expand Up @@ -128,61 +128,3 @@ pub fn new(buf: &mut DefaultBuffer) -> Default<'_> {
/// * Otherwise this is the [`Stack`] allocator.
#[doc(inline)]
pub use self::default_alloc::{Default, DefaultBuffer};

use musli::context::Buf;

/// An allocator that can be used in combination with a context.
///
/// # Examples
///
/// ```
/// use musli_allocator::Allocator;
/// use musli::context::Buf;
///
/// let mut buf = musli_allocator::buffer();
/// let alloc = musli_allocator::new(&mut buf);
///
/// let mut a = alloc.alloc().expect("allocation a failed");
/// let mut b = alloc.alloc().expect("allocation b failed");
///
/// b.write(b"He11o");
/// a.write(b.as_slice());
///
/// assert_eq!(a.as_slice(), b"He11o");
/// assert_eq!(a.len(), 5);
///
/// a.write(b" W0rld");
///
/// assert_eq!(a.as_slice(), b"He11o W0rld");
/// assert_eq!(a.len(), 11);
///
/// let mut c = alloc.alloc().expect("allocation c failed");
/// c.write(b"!");
/// a.write(c.as_slice());
///
/// assert_eq!(a.as_slice(), b"He11o W0rld!");
/// assert_eq!(a.len(), 12);
/// ```
pub trait Allocator {
/// The type of an allocated buffer.
type Buf<'this>: Buf
where
Self: 'this;

/// Allocate an empty, uninitialized buffer.
///
/// Calling this method returns `None` if the allocation failed.
fn alloc(&self) -> Option<Self::Buf<'_>>;
}

impl<A> Allocator for &A
where
A: ?Sized + Allocator,
{
type Buf<'this> = A::Buf<'this> where Self: 'this;

#[inline(always)]
fn alloc(&self) -> Option<Self::Buf<'_>> {
(*self).alloc()
}
}
4 changes: 2 additions & 2 deletions crates/musli-allocator/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ use core::ops::{Deref, DerefMut};
use core::ptr;
use core::slice;

use musli::context::Buf;
use musli::{Allocator, Buf};

use crate::{Allocator, FixedVec};
use crate::FixedVec;

const HEADER_U32: u32 = size_of::<Header>() as u32;
// We keep max bytes to 2^31, since that ensures that addition between two
Expand Down
4 changes: 2 additions & 2 deletions crates/musli-allocator/src/stack/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::fmt::{self, Write};
use std::string::String;
use std::vec::Vec;

use musli::context::Buf;
use musli::{Allocator, Buf};

use crate::{Allocator, Stack, StackBuffer};
use crate::{Stack, StackBuffer};

use super::{Header, HeaderId, State};

Expand Down
4 changes: 1 addition & 3 deletions crates/musli-allocator/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use core::ptr::NonNull;
use alloc::boxed::Box;
use alloc::vec::Vec;

use musli::context::Buf;

use crate::Allocator;
use musli::{Allocator, Buf};

/// A dynamic buffer allocated on the heap.
pub struct SystemBuffer {
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-allocator/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::Allocator;
use musli::context::Buf;
use musli::{Allocator, Buf};

fn basic_allocations<A: Allocator>(alloc: &A) {
let mut a = alloc.alloc().unwrap();
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-common/src/buffered_writer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! A writer which buffers the writes before it outputs it into the backing
//! storage.

use musli::context::Buf;
use musli::Context;
use musli::{Buf, Context};

use crate::fixed_bytes::{FixedBytes, FixedBytesOverflow};
use crate::writer::Writer;
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-common/src/context/alloc_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use alloc::string::{String, ToString};
use alloc::vec::Vec;

use musli::context::Error;
use musli::Context;
use musli::{Allocator, Context};

use crate::allocator::Allocator;
use crate::context::access;
use crate::context::rich_error::{RichError, Step};

Expand Down
4 changes: 1 addition & 3 deletions crates/musli-common/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use core::fmt;
use core::marker::PhantomData;

use musli::context::Error;
use musli::Context;

use crate::allocator::Allocator;
use musli::{Allocator, Context};

#[cfg(feature = "alloc")]
pub use self::alloc_context::AllocContext;
Expand Down
4 changes: 2 additions & 2 deletions crates/musli-common/src/context/no_std_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use core::fmt;
use core::ops::Range;

use musli::context::Error;
use musli::Context;
use musli::{Allocator, Context};

use crate::allocator::{Allocator, FixedString, FixedVec};
use crate::allocator::{FixedString, FixedVec};
use crate::context::rich_error::{RichError, Step};

use super::access::{Access, Shared};
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-common/src/fixed_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use core::fmt;
use core::mem::MaybeUninit;
use core::ptr;

use musli::context::Buf;
use musli::Context;
use musli::{Buf, Context};

use crate::writer::Writer;

Expand Down
4 changes: 1 addition & 3 deletions crates/musli-common/src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
//! an adapter around an I/O type to work with musli.

#[cfg(feature = "std")]
use musli::context::Buf;
#[cfg(feature = "std")]
use musli::Context;
use musli::{Buf, Context};

/// Wrapper constructed with [wrap].
pub struct Wrap<T> {
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-common/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use core::fmt;
use core::marker;
use core::mem::take;

use musli::Context;
use musli::{Buf, Context};

#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use musli::context::Buf;

/// Maximum size used by a fixed length [`Buf`].
pub const MAX_FIXED_BYTES_LEN: usize = 128;
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-descriptive/src/en.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::fmt;

use musli::context::Buf;
use musli::en::{Encoder, PairEncoder, PairsEncoder, SequenceEncoder, VariantEncoder};
use musli::Context;
use musli::{Buf, Context};
use musli_storage::en::StorageEncoder;

use crate::error::Error;
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-json/src/reader/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use musli::context::Buf;
use musli::de::NumberVisitor;
use musli::Context;
use musli::{Buf, Context};

use crate::error::{Error, ErrorKind};
use crate::reader::integer::decode_signed_full;
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-json/src/reader/slice_parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use musli::context::Buf;
use musli::Context;
use musli::{Buf, Context};

use crate::error::{Error, ErrorKind};
use crate::reader::{Parser, StringReference, Token};
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-json/src/reader/string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(clippy::zero_prefixed_literal)]

use musli::context::Buf;
use musli::Context;
use musli::{Buf, Context};

use crate::error::{Error, ErrorKind};
use crate::reader::{Parser, SliceParser};
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-macros/src/internals/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Tokens {
pub(crate) fn new(span: Span, prefix: &syn::Path) -> Self {
Self {
as_decoder_t: path(span, prefix, ["de", "AsDecoder"]),
buf_t: path(span, prefix, ["context", "Buf"]),
buf_t: path(span, prefix, ["Buf"]),
context_t: path(span, prefix, ["Context"]),
core_result: core(span, ["result", "Result"]),
decode_t: path(span, prefix, ["de", "Decode"]),
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-wire/src/en.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::fmt;

use musli::context::Buf;
use musli::en::{Encoder, PairEncoder, PairsEncoder, SequenceEncoder, VariantEncoder};
use musli::Context;
use musli::{Buf, Context};
use musli_storage::en::StorageEncoder;

use crate::error::Error;
Expand Down
5 changes: 3 additions & 2 deletions crates/musli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ categories = ["no-std", "encoding"]

[features]
default = ["std"]
std = ["alloc", "musli-wire/std", "musli-value/std", "musli-storage/std", "musli-json/std"]
alloc = ["musli-wire/alloc", "musli-value/alloc", "musli-storage/alloc", "musli-json/alloc"]
std = ["alloc", "musli-wire/std", "musli-value/std", "musli-storage/std", "musli-json/std", "musli-allocator/std"]
alloc = ["musli-wire/alloc", "musli-value/alloc", "musli-storage/alloc", "musli-json/alloc", "musli-allocator/alloc"]

[dependencies]
musli-macros = { version = "=0.0.96", path = "../musli-macros" }
Expand All @@ -28,3 +28,4 @@ musli-wire = { path = "../musli-wire", default-features = false }
musli-value = { path = "../musli-value", default-features = false }
musli-storage = { path = "../musli-storage", default-features = false }
musli-json = { path = "../musli-json", default-features = false, features = ["std"] }
musli-allocator = { path = "../musli-allocator", default-features = false }
56 changes: 56 additions & 0 deletions crates/musli/src/allocator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::Buf;

/// An allocator that can be used in combination with a context.
///
/// # Examples
///
/// ```
/// use musli::{Allocator, Buf};
///
/// let mut buf = musli_allocator::buffer();
/// let alloc = musli_allocator::new(&mut buf);
///
/// let mut a = alloc.alloc().expect("allocation a failed");
/// let mut b = alloc.alloc().expect("allocation b failed");
///
/// b.write(b"He11o");
/// a.write(b.as_slice());
///
/// assert_eq!(a.as_slice(), b"He11o");
/// assert_eq!(a.len(), 5);
///
/// a.write(b" W0rld");
///
/// assert_eq!(a.as_slice(), b"He11o W0rld");
/// assert_eq!(a.len(), 11);
///
/// let mut c = alloc.alloc().expect("allocation c failed");
/// c.write(b"!");
/// a.write(c.as_slice());
///
/// assert_eq!(a.as_slice(), b"He11o W0rld!");
/// assert_eq!(a.len(), 12);
/// ```
pub trait Allocator {
/// The type of an allocated buffer.
type Buf<'this>: Buf
where
Self: 'this;

/// Allocate an empty, uninitialized buffer.
///
/// Calling this method returns `None` if the allocation failed.
fn alloc(&self) -> Option<Self::Buf<'_>>;
}

impl<A> Allocator for &A
where
A: ?Sized + Allocator,
{
type Buf<'this> = A::Buf<'this> where Self: 'this;

#[inline(always)]
fn alloc(&self) -> Option<Self::Buf<'_>> {
(*self).alloc()
}
}
Loading

0 comments on commit e0c4dd3

Please sign in to comment.