diff --git a/crates/musli/src/allocator/allocator.rs b/crates/musli-core/src/alloc/allocator.rs similarity index 92% rename from crates/musli/src/allocator/allocator.rs rename to crates/musli-core/src/alloc/allocator.rs index b9c14ab7e..42b3dcefa 100644 --- a/crates/musli/src/allocator/allocator.rs +++ b/crates/musli-core/src/alloc/allocator.rs @@ -1,9 +1,9 @@ -use crate::buf::Buf; +use super::Buf; /// An allocator that can be used in combination with a context. pub trait Allocator { /// The type of an allocated buffer. - type Buf<'this, T>: Buf + type Buf<'this, T>: Buf where Self: 'this, T: 'this; diff --git a/crates/musli-core/src/alloc/buf.rs b/crates/musli-core/src/alloc/buf.rs new file mode 100644 index 000000000..c1ee9e2dc --- /dev/null +++ b/crates/musli-core/src/alloc/buf.rs @@ -0,0 +1,53 @@ +/// A raw buffer allocated through an [`Allocator`]. +/// +/// [`Allocator`]: super::Allocator +/// +/// ## Examples +/// +/// ``` +/// use musli::alloc::{Allocator, Buf, Vec}; +/// +/// let values: [u32; 4] = [1, 2, 3, 4]; +/// +/// musli::alloc::default!(|alloc| { +/// let mut buf = alloc.alloc::(); +/// let mut len = 0; +/// +/// for value in values { +/// if !buf.resize(len, 1) { +/// panic!("Allocation failed"); +/// } +/// +/// // SAFETY: We've just resized the above buffer. +/// unsafe { +/// buf.as_mut_ptr().add(len).write(value); +/// } +/// +/// len += 1; +/// } +/// +/// // SAFETY: Slice does not outlive the buffer it references. +/// let bytes = unsafe { core::slice::from_raw_parts(buf.as_ptr(), len) }; +/// assert_eq!(bytes, values); +/// }); +/// ``` +pub trait Buf { + /// Resize the buffer. + fn resize(&mut self, len: usize, additional: usize) -> bool; + + /// Get a pointer into the buffer. + fn as_ptr(&self) -> *const T; + + /// Get a mutable pointer into the buffer. + fn as_mut_ptr(&mut self) -> *mut T; + + /// Try to merge one buffer with another. + /// + /// The two length parameters refers to the initialized length of the two + /// buffers. + /// + /// If this returns `Err(B)` if merging was not possible. + fn try_merge(&mut self, this_len: usize, other: B, other_len: usize) -> Result<(), B> + where + B: Buf; +} diff --git a/crates/musli-core/src/alloc/mod.rs b/crates/musli-core/src/alloc/mod.rs new file mode 100644 index 000000000..0486aa902 --- /dev/null +++ b/crates/musli-core/src/alloc/mod.rs @@ -0,0 +1,9 @@ +//! Traits related to memory allocation. + +mod allocator; +#[doc(inline)] +pub use self::allocator::Allocator; + +mod buf; +#[doc(inline)] +pub use self::buf::Buf; diff --git a/crates/musli-core/src/buf.rs b/crates/musli-core/src/buf.rs deleted file mode 100644 index 0416093a2..000000000 --- a/crates/musli-core/src/buf.rs +++ /dev/null @@ -1,43 +0,0 @@ -//! Types related to buffers. - -use core::fmt; - -/// An error raised when we fail to write. -#[derive(Debug)] -pub struct Error; - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Allocation failed") - } -} - -#[cfg(feature = "std")] -impl std::error::Error for Error {} - -/// A raw buffer allocated from a [`Context`]. -/// -/// [`Context`]: crate::Context -pub trait Buf { - /// An item in the buffer. - type Item; - - /// Resize the buffer. - fn resize(&mut self, len: usize, additional: usize) -> bool; - - /// Get a pointer into the buffer. - fn as_ptr(&self) -> *const Self::Item; - - /// Get a mutable pointer into the buffer. - fn as_mut_ptr(&mut self) -> *mut Self::Item; - - /// Try to merge one buffer with another. - /// - /// The two length parameters refers to the initialized length of the two - /// buffers. - /// - /// If this returns `Err(B)` if merging was not possible. - fn try_merge(&mut self, this_len: usize, other: B, other_len: usize) -> Result<(), B> - where - B: Buf; -} diff --git a/crates/musli-core/src/context.rs b/crates/musli-core/src/context.rs index 536157966..82352c429 100644 --- a/crates/musli-core/src/context.rs +++ b/crates/musli-core/src/context.rs @@ -3,9 +3,10 @@ use core::fmt; use core::str; +use crate::alloc::Allocator; use crate::de::{DecodeBytes, DecodeUnsized, DecodeUnsizedBytes}; use crate::no_std; -use crate::{Buf, Decode, Decoder}; +use crate::{Decode, Decoder}; /// Provides ergonomic access to the serialization context. /// @@ -17,13 +18,10 @@ pub trait Context { type Error: 'static; /// A mark during processing. type Mark: Copy + Default; - /// A growable buffer. - type Buf<'this, T>: Buf - where - Self: 'this, - T: 'this; + /// The allocator associated with the context. + type Allocator: ?Sized + Allocator; /// An allocated buffer containing a valid string. - type BufString<'this>: AsRef + type String<'this>: AsRef where Self: 'this; @@ -71,18 +69,13 @@ pub trait Context { T::decode_unsized_bytes(self, decoder, f) } - /// Allocate a bytes buffer. - /// - /// Returns `None` if the underlying allocator cannot allocate memory for - /// some reason, typically this indicates that we've run out of memory. - /// - /// The buffer will be deallocated once the returned handle is dropped. - fn alloc(&self) -> Self::Buf<'_, T>; + /// Access the underlying allocator. + fn alloc(&self) -> &Self::Allocator; /// Collect and allocate a string from a [`Display`] implementation. /// /// [`Display`]: fmt::Display - fn collect_string(&self, value: &T) -> Result, Self::Error> + fn collect_string(&self, value: &T) -> Result, Self::Error> where T: ?Sized + fmt::Display; @@ -219,7 +212,7 @@ pub trait Context { /// Encountered an unsupported field tag. #[inline(always)] - fn invalid_field_string_tag(&self, _: &'static str, field: Self::BufString<'_>) -> Self::Error { + fn invalid_field_string_tag(&self, _: &'static str, field: Self::String<'_>) -> Self::Error { let field = field.as_ref(); self.message(format_args!("Invalid field tag `{field}`")) } diff --git a/crates/musli-core/src/impls/alloc.rs b/crates/musli-core/src/impls/alloc.rs index b11c69bfc..3839e214f 100644 --- a/crates/musli-core/src/impls/alloc.rs +++ b/crates/musli-core/src/impls/alloc.rs @@ -3,14 +3,14 @@ use core::fmt; #[cfg(feature = "std")] use core::hash::{BuildHasher, Hash}; -use alloc::borrow::{Cow, ToOwned}; -use alloc::boxed::Box; -use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque}; -use alloc::ffi::CString; -use alloc::rc::Rc; -use alloc::string::String; -use alloc::sync::Arc; -use alloc::vec::Vec; +use rust_alloc::borrow::{Cow, ToOwned}; +use rust_alloc::boxed::Box; +use rust_alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque}; +use rust_alloc::ffi::CString; +use rust_alloc::rc::Rc; +use rust_alloc::string::String; +use rust_alloc::sync::Arc; +use rust_alloc::vec::Vec; #[cfg(feature = "std")] use std::collections::{HashMap, HashSet}; @@ -618,11 +618,11 @@ where { use std::os::windows::ffi::OsStrExt; + use crate::alloc::{Allocator, Buf}; use crate::en::VariantEncoder; - use crate::Buf; encoder.encode_variant_fn(|variant| { - let mut buf = cx.alloc::(); + let mut buf = cx.alloc().alloc::(); let mut len = 0; for w in self.encode_wide() { diff --git a/crates/musli-core/src/lib.rs b/crates/musli-core/src/lib.rs index dbaf7f81a..a7ff001d0 100644 --- a/crates/musli-core/src/lib.rs +++ b/crates/musli-core/src/lib.rs @@ -11,19 +11,17 @@ #![cfg_attr(doc_cfg, feature(doc_cfg))] #[cfg(feature = "alloc")] -extern crate alloc; +extern crate alloc as rust_alloc; #[cfg(feature = "std")] extern crate std; +pub mod alloc; + mod context; #[doc(inline)] pub use self::context::Context; -pub mod buf; -#[doc(inline)] -pub use self::buf::Buf; - pub mod de; #[doc(inline)] pub use self::de::{Decode, Decoder}; diff --git a/crates/musli-core/src/never.rs b/crates/musli-core/src/never.rs index 4c32567ad..a322a7f46 100644 --- a/crates/musli-core/src/never.rs +++ b/crates/musli-core/src/never.rs @@ -12,6 +12,7 @@ use core::marker; use crate::no_std::ToOwned; +use crate::alloc::Buf; use crate::de::{ AsDecoder, Decode, DecodeUnsized, DecodeUnsizedBytes, Decoder, EntriesDecoder, EntryDecoder, MapDecoder, SequenceDecoder, SizeHint, UnsizedVisitor, VariantDecoder, @@ -19,7 +20,7 @@ use crate::de::{ use crate::en::{ Encode, Encoder, EntriesEncoder, EntryEncoder, MapEncoder, SequenceEncoder, VariantEncoder, }; -use crate::{Buf, Context}; +use crate::Context; /// Marker type used for the [`Never`] type. #[doc(hidden)] @@ -67,31 +68,26 @@ pub struct Never { _marker: marker::PhantomData<(A, B)>, } -impl Buf for Never -where - T: 'static, -{ - type Item = T; - +impl Buf for Never { #[inline] fn resize(&mut self, _: usize, _: usize) -> bool { match self._never {} } #[inline] - fn as_ptr(&self) -> *const Self::Item { + fn as_ptr(&self) -> *const T { match self._never {} } #[inline] - fn as_mut_ptr(&mut self) -> *mut Self::Item { + fn as_mut_ptr(&mut self) -> *mut T { match self._never {} } #[inline] fn try_merge(&mut self, _: usize, _: B, _: usize) -> Result<(), B> where - B: Buf, + B: Buf, { match self._never {} } diff --git a/crates/musli-core/src/no_std.rs b/crates/musli-core/src/no_std.rs index 3d8e1b3f1..ca0b475d3 100644 --- a/crates/musli-core/src/no_std.rs +++ b/crates/musli-core/src/no_std.rs @@ -16,7 +16,7 @@ use core::fmt; #[cfg(feature = "alloc")] -pub use alloc::borrow::ToOwned; +pub use rust_alloc::borrow::ToOwned; #[cfg(not(feature = "alloc"))] pub use self::to_owned::ToOwned; diff --git a/crates/musli/src/allocator/disabled.rs b/crates/musli/src/alloc/disabled.rs similarity index 84% rename from crates/musli/src/allocator/disabled.rs rename to crates/musli/src/alloc/disabled.rs index 68b5f4c83..f49f5ac27 100644 --- a/crates/musli/src/allocator/disabled.rs +++ b/crates/musli/src/alloc/disabled.rs @@ -1,35 +1,33 @@ use core::marker::PhantomData; use core::ptr; -use crate::{Allocator, Buf}; +use super::{Allocator, Buf}; /// An empty buffer. pub struct EmptyBuf { _marker: PhantomData, } -impl Buf for EmptyBuf { - type Item = T; - +impl Buf for EmptyBuf { #[inline] fn resize(&mut self, _: usize, _: usize) -> bool { false } #[inline] - fn as_ptr(&self) -> *const Self::Item { + fn as_ptr(&self) -> *const T { ptr::NonNull::dangling().as_ptr() } #[inline] - fn as_mut_ptr(&mut self) -> *mut Self::Item { + fn as_mut_ptr(&mut self) -> *mut T { ptr::NonNull::dangling().as_ptr() } #[inline] fn try_merge(&mut self, _: usize, other: B, _: usize) -> Result<(), B> where - B: Buf, + B: Buf, { Err(other) } diff --git a/crates/musli/src/allocator/mod.rs b/crates/musli/src/alloc/mod.rs similarity index 74% rename from crates/musli/src/allocator/mod.rs rename to crates/musli/src/alloc/mod.rs index b8421225f..ac7bb3b8a 100644 --- a/crates/musli/src/allocator/mod.rs +++ b/crates/musli/src/alloc/mod.rs @@ -6,17 +6,20 @@ //! * The [`Stack`] allocator, which can allocate buffers from a fixed-size //! slice. //! +//! The following types are also provided for convenience: +//! * [`Vec`] which can be used as a vector of allocations. +//! * [`String`] which can be used as a safe string container. +//! //!
//! //! ## Examples //! //! ``` -//! use musli::{Allocator, Buf}; -//! use musli::buf::BufVec; +//! use musli::alloc::Vec; //! -//! musli::allocator::default!(|alloc| { -//! let mut a = BufVec::new_in(alloc); -//! let mut b = BufVec::new_in(alloc); +//! musli::alloc::default!(|alloc| { +//! let mut a = Vec::new_in(alloc); +//! let mut b = Vec::new_in(alloc); //! //! b.write(b"He11o"); //! a.write(b.as_slice()); @@ -29,7 +32,7 @@ //! assert_eq!(a.as_slice(), b"He11o W0rld"); //! assert_eq!(a.len(), 11); //! -//! let mut c = BufVec::new_in(alloc); +//! let mut c = Vec::new_in(alloc); //! c.write(b"!"); //! a.write(c.as_slice()); //! @@ -44,6 +47,9 @@ #[cfg(test)] mod tests; +#[doc(inline)] +pub use musli_core::alloc::{Allocator, Buf}; + #[cfg(feature = "alloc")] mod system; @@ -56,15 +62,21 @@ pub use self::system::System; pub static SYSTEM: System = System::new(); mod disabled; +#[doc(inline)] pub use self::disabled::Disabled; mod stack; #[doc(inline)] pub use self::stack::{Stack, StackBuffer}; -mod allocator; +mod string; +pub(crate) use self::string::collect_string; +#[doc(inline)] +pub use self::string::String; + +mod vec; #[doc(inline)] -pub use self::allocator::Allocator; +pub use self::vec::Vec; /// The default stack buffer size for the default allocator provided through /// [`default!`]. @@ -74,7 +86,7 @@ pub const DEFAULT_STACK_BUFFER: usize = 4096; #[doc(hidden)] macro_rules! __default { (|$alloc:ident| $body:block) => { - $crate::allocator::__default_allocator_impl!(|$alloc| $body) + $crate::alloc::__default_allocator_impl!(|$alloc| $body) }; } @@ -90,12 +102,11 @@ macro_rules! __default { /// # Examples /// /// ``` -/// use musli::{Allocator, Buf}; -/// use musli::buf::BufVec; +/// use musli::alloc::Vec; /// -/// musli::allocator::default!(|alloc| { -/// let mut a = BufVec::new_in(alloc); -/// let mut b = BufVec::new_in(alloc); +/// musli::alloc::default!(|alloc| { +/// let mut a = Vec::new_in(alloc); +/// let mut b = Vec::new_in(alloc); /// /// b.write(b"He11o"); /// a.write(b.as_slice()); @@ -108,7 +119,7 @@ macro_rules! __default { /// assert_eq!(a.as_slice(), b"He11o W0rld"); /// assert_eq!(a.len(), 11); /// -/// let mut c = BufVec::new_in(alloc); +/// let mut c = Vec::new_in(alloc); /// c.write(b"!"); /// a.write(c.as_slice()); /// @@ -124,7 +135,7 @@ pub use __default as default; #[doc(hidden)] macro_rules! __default_allocator_impl { (|$alloc:ident| $body:block) => {{ - let $alloc = &$crate::allocator::SYSTEM; + let $alloc = &$crate::alloc::SYSTEM; $body }}; } @@ -134,7 +145,7 @@ macro_rules! __default_allocator_impl { #[doc(hidden)] macro_rules! __default_allocator_impl { (|$alloc:ident| $body:block) => {{ - let $alloc = $crate::allocator::System::new(); + let $alloc = $crate::alloc::System::new(); let $alloc = &$alloc; $body }}; @@ -146,8 +157,8 @@ macro_rules! __default_allocator_impl { macro_rules! __default_allocator_impl { (|$alloc:ident| $body:block) => {{ let mut __buf = - $crate::allocator::StackBuffer::<{ $crate::allocator::DEFAULT_STACK_BUFFER }>::new(); - let $alloc = $crate::allocator::Stack::new(&mut __buf); + $crate::alloc::StackBuffer::<{ $crate::alloc::DEFAULT_STACK_BUFFER }>::new(); + let $alloc = $crate::alloc::Stack::new(&mut __buf); $body }}; } diff --git a/crates/musli/src/allocator/stack.rs b/crates/musli/src/alloc/stack.rs similarity index 99% rename from crates/musli/src/allocator/stack.rs rename to crates/musli/src/alloc/stack.rs index dcbc37b77..fb7ec8aa0 100644 --- a/crates/musli/src/allocator/stack.rs +++ b/crates/musli/src/alloc/stack.rs @@ -8,7 +8,7 @@ use core::num::NonZeroU16; use core::ops::{Deref, DerefMut}; use core::ptr; -use crate::{Allocator, Buf}; +use super::{Allocator, Buf}; #[cfg(test)] macro_rules! let_mut { @@ -251,9 +251,7 @@ pub struct StackBuf<'a, T> { _marker: PhantomData, } -impl<'a, T> Buf for StackBuf<'a, T> { - type Item = T; - +impl<'a, T> Buf for StackBuf<'a, T> { #[inline] fn resize(&mut self, len: usize, additional: usize) -> bool { if additional == 0 || size_of::() == 0 { @@ -302,7 +300,7 @@ impl<'a, T> Buf for StackBuf<'a, T> { } #[inline] - fn as_ptr(&self) -> *const Self::Item { + fn as_ptr(&self) -> *const T { let Some(region) = self.region else { return ptr::NonNull::dangling().as_ptr(); }; @@ -315,7 +313,7 @@ impl<'a, T> Buf for StackBuf<'a, T> { } #[inline] - fn as_mut_ptr(&mut self) -> *mut Self::Item { + fn as_mut_ptr(&mut self) -> *mut T { let Some(region) = self.region else { return ptr::NonNull::dangling().as_ptr(); }; @@ -330,7 +328,7 @@ impl<'a, T> Buf for StackBuf<'a, T> { #[inline] fn try_merge(&mut self, this_len: usize, buf: B, other_len: usize) -> Result<(), B> where - B: Buf, + B: Buf, { let Some(region) = self.region else { return Err(buf); diff --git a/crates/musli/src/allocator/stack/tests.rs b/crates/musli/src/alloc/stack/tests.rs similarity index 90% rename from crates/musli/src/allocator/stack/tests.rs rename to crates/musli/src/alloc/stack/tests.rs index ba0cc49f2..4d40354e6 100644 --- a/crates/musli/src/allocator/stack/tests.rs +++ b/crates/musli/src/alloc/stack/tests.rs @@ -1,10 +1,9 @@ use std::collections::{BTreeSet, HashMap}; use std::fmt::{self, Write}; use std::string::String; -use std::vec::Vec; +use std::vec::Vec as StdVec; -use crate::buf::BufVec; -use crate::Allocator; +use crate::alloc::{Allocator, Vec}; use super::{Header, HeaderId, Stack, StackBuffer, State}; @@ -46,17 +45,17 @@ fn collect( mut current: Option, expected: E, mut next: N, -) -> Vec +) -> StdVec where E: IntoIterator, N: FnMut(HeaderId) -> Option, { - let mut actual = Vec::new(); - let mut actual_idents = Vec::new(); - let mut expected_idents = Vec::new(); + let mut actual = StdVec::new(); + let mut actual_idents = StdVec::new(); + let mut expected_idents = StdVec::new(); let mut it = expected.into_iter(); - let mut errors = Vec::new(); + let mut errors = StdVec::new(); loop { let expected = it.next(); @@ -193,9 +192,9 @@ fn grow_last() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let a = BufVec::new_in::(&alloc); + let a = Vec::::new_in(&alloc); - let mut b = BufVec::new_in::(&alloc); + let mut b = Vec::::new_in(&alloc); b.write(&[1, 2, 3, 4, 5, 6]); b.write(&[7, 8]); @@ -226,15 +225,15 @@ fn realloc() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); a.write(&[1, 2, 3, 4]); assert_eq!(a.region, Some(A)); - let mut b = BufVec::new_in::(&alloc); + let mut b = Vec::::new_in(&alloc); b.write(&[1, 2, 3, 4]); assert_eq!(b.region, Some(B)); - let mut c = BufVec::new_in::(&alloc); + let mut c = Vec::::new_in(&alloc); c.write(&[1, 2, 3, 4]); assert_eq!(c.region, Some(C)); @@ -266,7 +265,7 @@ fn realloc() { C => { 8, 4, 4, Used }, }; - let mut d = BufVec::new_in::(&alloc); + let mut d = Vec::::new_in(&alloc); assert_eq!(d.region, Some(A)); assert_structure! { @@ -302,9 +301,9 @@ fn grow_empty_moved() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); - let b = BufVec::new_in::(&alloc); - let mut c = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); + let b = Vec::::new_in(&alloc); + let mut c = Vec::::new_in(&alloc); c.write(&[0]); a.write(&[1, 2, 3, 4]); @@ -351,8 +350,8 @@ fn extend() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); - let mut b = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); + let mut b = Vec::::new_in(&alloc); a.write(&[1, 2]); b.write(&[1, 2, 3, 4]); @@ -379,9 +378,9 @@ fn extend_middle() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); - let mut b = BufVec::new_in::(&alloc); - let mut c = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); + let mut b = Vec::::new_in(&alloc); + let mut c = Vec::::new_in(&alloc); a.write(&[1, 2]); b.write(&[1, 2, 3, 4]); @@ -411,9 +410,9 @@ fn extend_gap() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); - let mut b = BufVec::new_in::(&alloc); - let mut c = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); + let mut b = Vec::::new_in(&alloc); + let mut c = Vec::::new_in(&alloc); a.write(&[1, 2]); b.write(&[7, 8, 9, 10]); @@ -446,11 +445,11 @@ fn test_overlapping_slice_miri() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); a.write(&[1, 2, 3, 4]); let a_slice = a.as_slice(); - let mut b = BufVec::new_in::(&alloc); + let mut b = Vec::::new_in(&alloc); b.write(&[5, 6, 7, 8]); let b_slice = b.as_slice(); @@ -464,16 +463,16 @@ fn grow_into_preceeding() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); a.write(&[0]); - let mut b = BufVec::new_in::(&alloc); + let mut b = Vec::::new_in(&alloc); b.write(&[1]); - let mut c = BufVec::new_in::(&alloc); + let mut c = Vec::::new_in(&alloc); c.write(&[2]); - let mut d = BufVec::new_in::(&alloc); + let mut d = Vec::::new_in(&alloc); d.write(&[3]); drop(a); @@ -502,8 +501,8 @@ fn flip_flop() { let mut buf = StackBuffer::<4096>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); - let mut b = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); + let mut b = Vec::::new_in(&alloc); a.write(&[0]); b.write(&[0]); @@ -584,7 +583,7 @@ fn limits() { let mut buf = StackBuffer::<32>::new(); let alloc = Stack::new(&mut buf); - let mut a = BufVec::new_in::(&alloc); + let mut a = Vec::::new_in(&alloc); assert!(a.write(&[0, 1, 2, 3, 4, 5, 6, 7])); assert_structure! { diff --git a/crates/musli/src/buf/buf_string.rs b/crates/musli/src/alloc/string.rs similarity index 64% rename from crates/musli/src/buf/buf_string.rs rename to crates/musli/src/alloc/string.rs index a93d1f0b2..06706eb4f 100644 --- a/crates/musli/src/buf/buf_string.rs +++ b/crates/musli/src/alloc/string.rs @@ -1,27 +1,29 @@ -use core::fmt::{self, Write}; +use core::fmt; use core::ops::Deref; use core::str; -use crate::buf::BufVec; use crate::fixed::CapacityError; -use crate::{Allocator, Buf, Context}; +use crate::Context; -/// Wrapper around a [`Buf`], guaranteed to be a valid utf-8 string. -pub struct BufString +use super::{Allocator, Vec}; + +/// Wrapper around a buffer that is guaranteed to be a valid utf-8 string. +pub struct String<'a, A> where - B: Buf, + A: 'a + ?Sized + Allocator, { - buf: BufVec, + buf: Vec<'a, u8, A>, } /// Collect a string into a string buffer. -pub(crate) fn collect_string(cx: &C, value: T) -> Result>, C::Error> +pub(crate) fn collect_string(cx: &C, value: T) -> Result, C::Error> where C: ?Sized + Context, T: fmt::Display, { - let buf = cx.alloc(); - let mut string = BufString::new(buf); + use core::fmt::Write; + + let mut string = String::new_in(cx.alloc()); if write!(string, "{value}").is_err() { return Err(cx.message("Failed to write to string")); @@ -30,20 +32,18 @@ where Ok(string) } -impl BufString +impl<'a, A> String<'a, A> where - B: Buf, + A: 'a + ?Sized + Allocator, { /// Construct a new string buffer in the provided allocator. - pub fn new_in<'a>(alloc: &'a (impl ?Sized + Allocator = B>)) -> Self { + pub fn new_in(alloc: &'a A) -> Self { Self::new(alloc.alloc::()) } /// Construct a new fixed string. - pub(crate) const fn new(buf: B) -> BufString { - BufString { - buf: BufVec::new(buf), - } + pub(crate) const fn new(buf: A::Buf<'a, u8>) -> Self { + Self { buf: Vec::new(buf) } } fn as_str(&self) -> &str { @@ -68,22 +68,24 @@ where } } -impl fmt::Write for BufString +impl<'a, A> fmt::Write for String<'a, A> where - B: Buf, + A: 'a + ?Sized + Allocator, { + #[inline] fn write_char(&mut self, c: char) -> fmt::Result { self.try_push(c).map_err(|_| fmt::Error) } + #[inline] fn write_str(&mut self, s: &str) -> fmt::Result { self.try_push_str(s).map_err(|_| fmt::Error) } } -impl Deref for BufString +impl<'a, A> Deref for String<'a, A> where - B: Buf, + A: 'a + ?Sized + Allocator, { type Target = str; @@ -93,9 +95,9 @@ where } } -impl fmt::Display for BufString +impl<'a, A> fmt::Display for String<'a, A> where - B: Buf, + A: 'a + ?Sized + Allocator, { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -103,9 +105,9 @@ where } } -impl AsRef for BufString +impl<'a, A> AsRef for String<'a, A> where - B: Buf, + A: ?Sized + Allocator, { #[inline] fn as_ref(&self) -> &str { diff --git a/crates/musli/src/allocator/system.rs b/crates/musli/src/alloc/system.rs similarity index 96% rename from crates/musli/src/allocator/system.rs rename to crates/musli/src/alloc/system.rs index 0dbb9680b..8e9370316 100644 --- a/crates/musli/src/allocator/system.rs +++ b/crates/musli/src/alloc/system.rs @@ -5,13 +5,14 @@ use core::mem::{align_of, size_of}; use core::ptr; use core::ptr::NonNull; -use ::alloc::alloc; -use ::alloc::boxed::Box; +use rust_alloc::alloc; +use rust_alloc::boxed::Box; use crate::loom::cell::UnsafeCell; use crate::loom::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use crate::loom::sync::with_mut_usize; -use crate::{Allocator, Buf}; + +use super::{Allocator, Buf}; /// The max capacity of an allocated region as it's being handed back. #[cfg(not(loom))] @@ -33,14 +34,12 @@ const MAX_REGIONS: usize = 2; /// # Examples /// /// ``` -/// use musli::{Allocator, Buf}; -/// use musli::allocator::System; -/// use musli::buf::BufVec; +/// use musli::alloc::{System, Vec}; /// /// let alloc = System::new(); /// -/// let mut buf1 = BufVec::new_in(&alloc); -/// let mut buf2 = BufVec::new_in(&alloc); +/// let mut buf1 = Vec::new_in(&alloc); +/// let mut buf2 = Vec::new_in(&alloc); // /// assert!(buf1.write(b"Hello, ")); /// assert!(buf2.write(b"world!")); @@ -183,9 +182,7 @@ pub struct SystemBuf<'a, T> { _marker: PhantomData, } -impl<'a, T> Buf for SystemBuf<'a, T> { - type Item = T; - +impl<'a, T> Buf for SystemBuf<'a, T> { #[inline] fn resize(&mut self, len: usize, additional: usize) -> bool { if additional == 0 || size_of::() == 0 { @@ -201,7 +198,7 @@ impl<'a, T> Buf for SystemBuf<'a, T> { } #[inline] - fn as_ptr(&self) -> *const Self::Item { + fn as_ptr(&self) -> *const T { let Some(region) = &self.region else { return ptr::NonNull::dangling().as_ptr(); }; @@ -210,7 +207,7 @@ impl<'a, T> Buf for SystemBuf<'a, T> { } #[inline] - fn as_mut_ptr(&mut self) -> *mut Self::Item { + fn as_mut_ptr(&mut self) -> *mut T { let Some(region) = &mut self.region else { return ptr::NonNull::dangling().as_ptr(); }; @@ -221,7 +218,7 @@ impl<'a, T> Buf for SystemBuf<'a, T> { #[inline] fn try_merge(&mut self, _: usize, other: B, _: usize) -> Result<(), B> where - B: Buf, + B: Buf, { Err(other) } diff --git a/crates/musli/src/allocator/tests.rs b/crates/musli/src/alloc/tests.rs similarity index 81% rename from crates/musli/src/allocator/tests.rs rename to crates/musli/src/alloc/tests.rs index f6cb3334e..6dd49ac91 100644 --- a/crates/musli/src/allocator/tests.rs +++ b/crates/musli/src/alloc/tests.rs @@ -1,9 +1,11 @@ -use crate::buf::BufVec; -use crate::Allocator; +use super::{Allocator, Vec}; -fn basic_allocations(alloc: &A) { - let mut a = BufVec::new_in(alloc); - let mut b = BufVec::new_in(alloc); +fn basic_allocations
(alloc: &A) +where + A: Allocator, +{ + let mut a = Vec::new_in(alloc); + let mut b = Vec::new_in(alloc); b.write(b"He11o"); @@ -20,7 +22,7 @@ fn basic_allocations(alloc: &A) { assert_eq!(a.as_slice(), b"He11o W0rld"); assert_eq!(a.len(), 11); - let mut c = BufVec::new_in(alloc); + let mut c = Vec::new_in(alloc); c.write(b"!"); assert_eq!(c.len(), 1); @@ -32,8 +34,8 @@ fn basic_allocations(alloc: &A) { fn grow_allocations(alloc: &A) { const BYTES: &[u8] = b"abcd"; - let mut a = BufVec::new_in(alloc); - let mut b = BufVec::new_in(alloc); + let mut a = Vec::new_in(alloc); + let mut b = Vec::new_in(alloc); for _ in 0..1024 { assert!(a.write(BYTES)); @@ -51,7 +53,7 @@ fn grow_allocations(alloc: &A) { } drop(a); - let mut c = BufVec::new_in(alloc); + let mut c = Vec::new_in(alloc); for _ in 0..1024 { assert!(c.write(BYTES)); @@ -84,9 +86,12 @@ fn system_grow() { grow_allocations(&alloc); } -fn zst_allocations(alloc: &A) { - let mut a = BufVec::new_in(alloc); - let mut b = BufVec::new_in(alloc); +fn zst_allocations(alloc: &A) +where + A: Allocator, +{ + let mut a = Vec::new_in(alloc); + let mut b = Vec::new_in(alloc); assert!(a.write(&[(); 100])); assert!(b.write(&[(); 100])); diff --git a/crates/musli/src/buf/buf_vec.rs b/crates/musli/src/alloc/vec.rs similarity index 61% rename from crates/musli/src/buf/buf_vec.rs rename to crates/musli/src/alloc/vec.rs index b6613361d..3669edf2b 100644 --- a/crates/musli/src/buf/buf_vec.rs +++ b/crates/musli/src/alloc/vec.rs @@ -1,36 +1,41 @@ -use core::fmt::{self, Arguments}; +use core::fmt; use core::mem::ManuallyDrop; #[cfg(test)] use core::ops::{Deref, DerefMut}; use core::ptr; use core::slice; -use crate::buf::{Buf, Error}; -use crate::Allocator; +use super::{Allocator, Buf}; /// A vector backed by an [`Allocator`] [`Buf`]. -pub struct BufVec +pub struct Vec<'a, T, A> where - B: Buf, + A: 'a + ?Sized + Allocator, + T: 'a, { - buf: B, + buf: A::Buf<'a, T>, len: usize, } -impl BufVec +impl<'a, T, A> Vec<'a, T, A> where - B: Buf, + A: 'a + ?Sized + Allocator, + T: 'a, { + /// Construct a buffer vector from raw parts. + const unsafe fn from_raw_parts(buf: A::Buf<'a, T>, len: usize) -> Self { + Self { buf, len } + } + /// Construct a new buffer vector. /// /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); /// /// a.push(String::from("Hello")); /// a.push(String::from("World")); @@ -38,10 +43,7 @@ where /// assert_eq!(a.as_slice(), ["Hello", "World"]); /// }); /// ``` - pub fn new_in<'a, T>(alloc: &'a (impl ?Sized + Allocator = B>)) -> Self - where - T: 'a, - { + pub fn new_in(alloc: &'a A) -> Self { Self { buf: alloc.alloc::(), len: 0, @@ -50,7 +52,7 @@ where /// Construct a new buffer vector. #[inline] - pub const fn new(buf: B) -> Self { + pub const fn new(buf: A::Buf<'a, T>) -> Self { Self { buf, len: 0 } } @@ -59,11 +61,10 @@ where /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); /// /// assert_eq!(a.len(), 0); /// a.write(b"Hello"); @@ -79,11 +80,10 @@ where /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); /// /// assert!(a.is_empty()); /// a.write(b"Hello"); @@ -103,11 +103,10 @@ where /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); /// /// a.push(b'H'); /// a.push(b'e'); @@ -119,7 +118,7 @@ where /// }); /// ``` #[inline] - pub fn push(&mut self, item: B::Item) -> bool { + pub fn push(&mut self, item: T) -> bool { if !self.buf.resize(self.len, 1) { return false; } @@ -140,11 +139,10 @@ where /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); /// /// a.push(String::from("foo")); /// a.push(String::from("bar")); @@ -156,7 +154,7 @@ where /// assert_eq!(a.pop(), None); /// }); /// ``` - pub fn pop(&mut self) -> Option { + pub fn pop(&mut self) -> Option { if self.len == 0 { return None; } @@ -172,11 +170,10 @@ where /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); /// /// a.push(b'H'); /// a.push(b'e'); @@ -200,23 +197,22 @@ where /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); /// assert_eq!(a.as_slice(), b""); /// a.write(b"Hello"); /// assert_eq!(a.as_slice(), b"Hello"); /// }); /// ``` - pub fn as_slice(&self) -> &[B::Item] { + pub fn as_slice(&self) -> &[T] { // SAFETY: We know that the buffer is initialized up to `self.len`. unsafe { slice::from_raw_parts(self.buf.as_ptr(), self.len) } } #[inline] - fn into_parts(self) -> (B, usize) { + fn into_raw_parts(self) -> (A::Buf<'a, T>, usize) { let this = ManuallyDrop::new(self); // SAFETY: The interior buffer is valid and will not be dropped thanks to `ManuallyDrop`. @@ -227,10 +223,10 @@ where } } -impl BufVec +impl<'a, T, A> Vec<'a, T, A> where - B: Buf, - B::Item: Copy, + A: 'a + ?Sized + Allocator, + T: 'a + Copy, { /// Write the given number of bytes. /// @@ -240,17 +236,16 @@ where /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); /// assert_eq!(a.len(), 0); /// a.write(b"Hello"); /// assert_eq!(a.len(), 5); /// }); /// ``` - pub fn write(&mut self, items: &[B::Item]) -> bool { + pub fn write(&mut self, items: &[T]) -> bool { if !self.buf.resize(self.len, items.len()) { return false; } @@ -266,12 +261,7 @@ where true } -} -impl BufVec -where - B: Buf, -{ /// Write a buffer of the same type onto the current buffer. /// /// This allows allocators to provide more efficient means of extending the @@ -280,12 +270,11 @@ where /// ## Examples /// /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; + /// use musli::alloc::Vec; /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); - /// let mut b = BufVec::new_in(alloc); + /// musli::alloc::default!(|alloc| { + /// let mut a = Vec::new_in(alloc); + /// let mut b = Vec::new_in(alloc); /// /// a.write(b"Hello"); /// b.write(b" World"); @@ -295,74 +284,60 @@ where /// }); /// ``` #[inline] - pub fn extend(&mut self, other: BufVec) -> bool - where - U: Buf, - { - let (other, other_len) = other.into_parts(); + pub fn extend(&mut self, other: Vec<'_, T, A>) -> bool { + let (other, other_len) = other.into_raw_parts(); // Try to merge one buffer with another. if let Err(buf) = self.buf.try_merge(self.len, other, other_len) { - let other = BufVec { - buf, - len: other_len, - }; - + let other = unsafe { Vec::::from_raw_parts(buf, other_len) }; return self.write(other.as_slice()); } self.len += other_len; true } +} - /// Try to write a format string into the buffer. - /// - /// ## Examples - /// - /// ``` - /// use musli::{Allocator, Buf}; - /// use musli::buf::BufVec; - /// - /// musli::allocator::default!(|alloc| { - /// let mut a = BufVec::new_in(alloc); - /// let world = "World"; - /// - /// write!(a, "Hello {world}")?; - /// - /// assert_eq!(a.as_slice(), b"Hello World"); - /// }); - /// # Ok::<(), musli::buf::Error>(()) - /// ``` +/// Try to write a format string into the buffer. +/// +/// ## Examples +/// +/// ``` +/// use core::fmt::Write; +/// +/// use musli::alloc::Vec; +/// +/// musli::alloc::default!(|alloc| { +/// let mut a = Vec::new_in(alloc); +/// let world = "World"; +/// +/// write!(a, "Hello {world}")?; +/// +/// assert_eq!(a.as_slice(), b"Hello World"); +/// }); +/// # Ok::<(), core::fmt::Error>(()) +/// ``` +impl<'a, A> fmt::Write for Vec<'a, u8, A> +where + A: 'a + ?Sized + Allocator, +{ #[inline] - pub fn write_fmt(&mut self, arguments: Arguments<'_>) -> Result<(), Error> { - struct Write<'a, B>(&'a mut BufVec) - where - B: Buf; - - impl fmt::Write for Write<'_, B> - where - B: Buf, - { - fn write_str(&mut self, s: &str) -> fmt::Result { - if !self.0.write(s.as_bytes()) { - return Err(fmt::Error); - } - - Ok(()) - } + fn write_str(&mut self, s: &str) -> fmt::Result { + if !self.write(s.as_bytes()) { + return Err(fmt::Error); } - let mut write = Write(self); - fmt::write(&mut write, arguments).map_err(|_| Error) + Ok(()) } } #[cfg(test)] -impl Deref for BufVec +impl<'a, T, A> Deref for Vec<'a, T, A> where - B: Buf, + A: 'a + ?Sized + Allocator, + T: 'a, { - type Target = B; + type Target = A::Buf<'a, T>; #[inline] fn deref(&self) -> &Self::Target { @@ -371,9 +346,10 @@ where } #[cfg(test)] -impl DerefMut for BufVec +impl<'a, T, A> DerefMut for Vec<'a, T, A> where - B: Buf, + A: 'a + ?Sized + Allocator, + T: 'a, { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { @@ -381,9 +357,10 @@ where } } -impl Drop for BufVec +impl<'a, T, A> Drop for Vec<'a, T, A> where - B: Buf, + A: 'a + ?Sized + Allocator, + T: 'a, { fn drop(&mut self) { self.clear(); diff --git a/crates/musli/src/buf/mod.rs b/crates/musli/src/buf/mod.rs deleted file mode 100644 index 494a27045..000000000 --- a/crates/musli/src/buf/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -//! [`Buf`] utilities. -//! -//! [`Buf`]: crate::Buf - -mod buf_string; -pub(crate) use self::buf_string::collect_string; -pub use self::buf_string::BufString; - -mod buf_vec; -pub use self::buf_vec::BufVec; - -#[doc(inline)] -pub use musli_core::buf::{Buf, Error}; diff --git a/crates/musli/src/context/error.rs b/crates/musli/src/context/error.rs index 84867c410..5de4722eb 100644 --- a/crates/musli/src/context/error.rs +++ b/crates/musli/src/context/error.rs @@ -10,7 +10,7 @@ use core::fmt; use crate::no_std; #[cfg(feature = "alloc")] -use alloc::string::{String, ToString}; +use rust_alloc::string::{String, ToString}; /// Trait governing errors raised during encodeing or decoding. pub trait Error: Sized + 'static + Send + Sync + fmt::Display + fmt::Debug { diff --git a/crates/musli/src/context/mod.rs b/crates/musli/src/context/mod.rs index ac2434d50..0f3b4b69d 100644 --- a/crates/musli/src/context/mod.rs +++ b/crates/musli/src/context/mod.rs @@ -20,10 +20,10 @@ use core::cell::{Cell, UnsafeCell}; use core::fmt; use core::marker::PhantomData; -use crate::buf::{self, BufString}; +use crate::alloc::{self, Allocator, String}; use crate::mode::Binary; use crate::no_std; -use crate::{Allocator, Context}; +use crate::Context; /// A simple non-diagnostical capturing context which simply emits the original /// error. @@ -76,23 +76,23 @@ where type Mode = M; type Error = E; type Mark = (); - type Buf<'this, T> = A::Buf<'this, T> where Self: 'this, T: 'this; - type BufString<'this> = BufString> where Self: 'this; + type Allocator = A; + type String<'this> = String<'this, A> where Self: 'this; #[inline] fn clear(&self) {} #[inline] - fn alloc(&self) -> Self::Buf<'_, T> { - self.alloc.alloc() + fn alloc(&self) -> &Self::Allocator { + &self.alloc } #[inline] - fn collect_string(&self, value: &T) -> Result, Self::Error> + fn collect_string(&self, value: &T) -> Result, Self::Error> where T: ?Sized + fmt::Display, { - buf::collect_string(self, value) + alloc::collect_string(self, value) } #[inline] @@ -176,23 +176,23 @@ where type Mode = M; type Error = ErrorMarker; type Mark = (); - type Buf<'this, T> = A::Buf<'this, T> where Self: 'this, T: 'this; - type BufString<'this> = BufString> where Self: 'this; + type Allocator = A; + type String<'this> = String<'this, A> where Self: 'this; #[inline] fn clear(&self) {} #[inline] - fn alloc(&self) -> Self::Buf<'_, T> { - self.alloc.alloc() + fn alloc(&self) -> &Self::Allocator { + &self.alloc } #[inline] - fn collect_string(&self, value: &T) -> Result, Self::Error> + fn collect_string(&self, value: &T) -> Result, Self::Error> where T: ?Sized + fmt::Display, { - buf::collect_string(self, value) + alloc::collect_string(self, value) } #[inline] @@ -250,8 +250,8 @@ where type Mode = M; type Error = ErrorMarker; type Mark = (); - type Buf<'this, T> = A::Buf<'this, T> where Self: 'this, T: 'this; - type BufString<'this> = BufString> where Self: 'this; + type Allocator = A; + type String<'this> = String<'this, A> where Self: 'this; #[inline] fn clear(&self) { @@ -263,16 +263,16 @@ where } #[inline] - fn alloc(&self) -> Self::Buf<'_, T> { - self.alloc.alloc() + fn alloc(&self) -> &Self::Allocator { + &self.alloc } #[inline] - fn collect_string(&self, value: &T) -> Result, Self::Error> + fn collect_string(&self, value: &T) -> Result, Self::Error> where T: ?Sized + fmt::Display, { - buf::collect_string(self, value) + alloc::collect_string(self, value) } #[inline] diff --git a/crates/musli/src/context/rich_context.rs b/crates/musli/src/context/rich_context.rs index a38b3c2b0..a851b2734 100644 --- a/crates/musli/src/context/rich_context.rs +++ b/crates/musli/src/context/rich_context.rs @@ -5,17 +5,17 @@ use core::fmt::{self, Write}; use core::marker::PhantomData; use core::ops::Range; -use crate::buf::{self, BufString, BufVec}; -use crate::{Allocator, Context}; +use crate::alloc::{self, Allocator, String, Vec}; +use crate::Context; use super::access::{Access, Shared}; use super::rich_error::{RichError, Step}; use super::ErrorMarker; #[cfg(all(not(loom), feature = "alloc"))] -use crate::allocator::System; +use crate::alloc::System; -type BufPair<'a, A> = (Range, BufString<::Buf<'a, u8>>); +type BufPair<'a, A> = (Range, String<'a, A>); /// A rich context which uses allocations and tracks the exact location of /// errors. @@ -28,12 +28,12 @@ type BufPair<'a, A> = (Range, BufString<::Buf<'a, u8>>); /// enabled, and will use the [`System`] allocator. pub struct RichContext<'a, A, M> where - A: ?Sized + Allocator, + A: 'a + ?Sized + Allocator, { alloc: &'a A, mark: Cell, - errors: UnsafeCell>>>, - path: UnsafeCell>>>>>, + errors: UnsafeCell, A>>, + path: UnsafeCell>, A>>, // How many elements of `path` we've gone over capacity. path_cap: Cell, include_type: bool, @@ -48,7 +48,7 @@ impl RichContext<'static, System, M> { /// Construct a new context which uses the system allocator for memory. #[inline] pub fn new() -> Self { - Self::with_alloc(&crate::allocator::SYSTEM) + Self::with_alloc(&crate::alloc::SYSTEM) } } @@ -62,13 +62,13 @@ impl Default for RichContext<'static, System, M> { impl<'a, A, M> RichContext<'a, A, M> where - A: ?Sized + Allocator, + A: 'a + ?Sized + Allocator, { /// Construct a new context which uses allocations to a fixed but /// configurable number of diagnostics. pub fn with_alloc(alloc: &'a A) -> Self { - let errors = BufVec::new_in(alloc); - let path = BufVec::new_in(alloc); + let errors = Vec::new_in(alloc); + let path = Vec::new_in(alloc); Self { alloc, @@ -110,7 +110,7 @@ where } /// Push an error into the collection. - fn push_error(&self, range: Range, error: BufString>) { + fn push_error(&self, range: Range, error: String<'a, A>) { let _access = self.access.exclusive(); // SAFETY: We've checked that we have exclusive access just above. @@ -120,7 +120,7 @@ where } /// Push a path. - fn push_path(&self, step: Step>>) { + fn push_path(&self, step: Step>) { let _access = self.access.exclusive(); // SAFETY: We've checked that we have exclusive access just above. @@ -148,11 +148,11 @@ where } } - fn format_string(&self, value: T) -> Option>> + fn format_string(&self, value: T) -> Option> where T: fmt::Display, { - let mut string = BufString::new_in(self.alloc); + let mut string = String::new_in(self.alloc); write!(string, "{value}").ok()?; Some(string) } @@ -160,14 +160,14 @@ where impl<'a, A, M> Context for RichContext<'a, A, M> where - A: ?Sized + Allocator, + A: 'a + ?Sized + Allocator, M: 'static, { type Mode = M; type Error = ErrorMarker; type Mark = usize; - type Buf<'this, T> = A::Buf<'this, T> where Self: 'this, T: 'this; - type BufString<'this> = BufString> where Self: 'this; + type Allocator = A; + type String<'this> = String<'this, A> where Self: 'this; #[inline] fn clear(&self) { @@ -182,16 +182,16 @@ where } #[inline] - fn alloc(&self) -> Self::Buf<'_, T> { - self.alloc.alloc() + fn alloc(&self) -> &Self::Allocator { + self.alloc } #[inline] - fn collect_string(&self, value: &T) -> Result, Self::Error> + fn collect_string(&self, value: &T) -> Result, Self::Error> where T: ?Sized + fmt::Display, { - buf::collect_string(self, value) + alloc::collect_string(self, value) } #[inline] @@ -363,8 +363,8 @@ pub struct Errors<'a, 'buf, A> where A: 'buf + ?Sized + Allocator, { - path: &'a [Step>>], - errors: &'a [(Range, BufString>)], + path: &'a [Step>], + errors: &'a [(Range, String<'buf, A>)], index: usize, path_cap: usize, _access: Shared<'a>, @@ -372,9 +372,9 @@ where impl<'a, 'buf, A> Iterator for Errors<'a, 'buf, A> where - A: ?Sized + Allocator, + A: 'buf + ?Sized + Allocator, { - type Item = RichError<'a, BufString>, BufString>>; + type Item = RichError<'a, String<'buf, A>, String<'buf, A>>; #[inline] fn next(&mut self) -> Option { diff --git a/crates/musli/src/descriptive/de.rs b/crates/musli/src/descriptive/de.rs index 65501c17b..bd15ede85 100644 --- a/crates/musli/src/descriptive/de.rs +++ b/crates/musli/src/descriptive/de.rs @@ -2,7 +2,7 @@ use core::fmt; use core::mem::take; #[cfg(feature = "alloc")] -use alloc::vec::Vec; +use rust_alloc::vec::Vec; use crate::de::{ Decode, DecodeUnsized, Decoder, EntriesDecoder, EntryDecoder, MapDecoder, SequenceDecoder, diff --git a/crates/musli/src/descriptive/en.rs b/crates/musli/src/descriptive/en.rs index 754b7d884..bb853f821 100644 --- a/crates/musli/src/descriptive/en.rs +++ b/crates/musli/src/descriptive/en.rs @@ -36,7 +36,7 @@ where { cx: &'a C, writer: W, - buffer: BufWriter>, + buffer: BufWriter<'a, C::Allocator>, } impl<'a, W, const OPT: Options, C> SelfPackEncoder<'a, W, OPT, C> @@ -332,7 +332,7 @@ where { type Cx = C; type Ok = (); - type EncodeNext<'this> = StorageEncoder<'a, &'this mut BufWriter>, OPT, C> where Self: 'this; + type EncodeNext<'this> = StorageEncoder<'a, &'this mut BufWriter<'a, C::Allocator>, OPT, C> where Self: 'this; #[inline] fn encode_next(&mut self) -> Result, C::Error> { diff --git a/crates/musli/src/descriptive/error.rs b/crates/musli/src/descriptive/error.rs index 1c12512be..0d446f840 100644 --- a/crates/musli/src/descriptive/error.rs +++ b/crates/musli/src/descriptive/error.rs @@ -1,9 +1,9 @@ use core::fmt; #[cfg(feature = "alloc")] -use alloc::boxed::Box; +use rust_alloc::boxed::Box; #[cfg(feature = "alloc")] -use alloc::string::ToString; +use rust_alloc::string::ToString; use crate::no_std; diff --git a/crates/musli/src/fixed.rs b/crates/musli/src/fixed.rs index bdb095b71..4f9180db4 100644 --- a/crates/musli/src/fixed.rs +++ b/crates/musli/src/fixed.rs @@ -8,9 +8,9 @@ use core::mem::MaybeUninit; use core::ops::{Deref, DerefMut}; use core::ptr; -use crate::buf::BufVec; +use crate::alloc::Vec; use crate::writer::Writer; -use crate::{Buf, Context}; +use crate::Context; /// An error raised when we are at capacity. #[non_exhaustive] @@ -193,10 +193,9 @@ impl Writer for FixedBytes { } #[inline] - fn extend(&mut self, cx: &C, buffer: BufVec) -> Result<(), C::Error> + fn extend(&mut self, cx: &C, buffer: Vec<'_, u8, C::Allocator>) -> Result<(), C::Error> where C: ?Sized + Context, - B: Buf, { // SAFETY: the buffer never outlives this function call. self.write_bytes(cx, buffer.as_slice()) diff --git a/crates/musli/src/int/tests.rs b/crates/musli/src/int/tests.rs index b2b01af71..add54d63c 100644 --- a/crates/musli/src/int/tests.rs +++ b/crates/musli/src/int/tests.rs @@ -1,7 +1,7 @@ use std::fmt; use std::vec::Vec; -use alloc::vec; +use rust_alloc::vec; use crate::context; use crate::fixed::FixedBytes; @@ -16,7 +16,7 @@ const ITER: usize = 100; #[test] fn basic_continuation() { - crate::allocator::default!(|alloc| { + crate::alloc::default!(|alloc| { let cx = context::Ignore::marker(&alloc); let mut bytes = FixedBytes::<8>::new(); c::encode(&cx, &mut bytes, 5000u32).unwrap(); @@ -36,7 +36,7 @@ fn test_continuation_encoding() { where T: PartialEq + fmt::Debug + Unsigned, { - crate::allocator::default!(|alloc| { + crate::alloc::default!(|alloc| { let mut out = Vec::new(); let cx = crate::context::Ignore::marker(&alloc); c::encode(&cx, &mut out, expected).unwrap(); @@ -55,7 +55,7 @@ fn test_continuation_encoding() { where T: Unsigned, { - crate::allocator::default!(|alloc| { + crate::alloc::default!(|alloc| { let mut out = Vec::new(); let cx = crate::context::Same::marker(&alloc); c::encode(&cx, crate::wrap::wrap(&mut out), value).unwrap(); diff --git a/crates/musli/src/json/de/key_decoder.rs b/crates/musli/src/json/de/key_decoder.rs index e5f195852..f0f508b50 100644 --- a/crates/musli/src/json/de/key_decoder.rs +++ b/crates/musli/src/json/de/key_decoder.rs @@ -1,6 +1,6 @@ use core::fmt; -use crate::buf::BufVec; +use crate::alloc::Vec; use crate::de::{Decode, DecodeUnsized, Decoder, SizeHint, Skip, UnsizedVisitor, Visitor}; use crate::Context; @@ -29,7 +29,7 @@ where where V: UnsizedVisitor<'de, C, [u8]>, { - let mut scratch = BufVec::new(self.cx.alloc()); + let mut scratch = Vec::new_in(self.cx.alloc()); match self.parser.parse_string(self.cx, true, &mut scratch)? { StringReference::Borrowed(string) => visitor.visit_borrowed(self.cx, string.as_bytes()), diff --git a/crates/musli/src/json/de/mod.rs b/crates/musli/src/json/de/mod.rs index b955295b4..0700d77e2 100644 --- a/crates/musli/src/json/de/mod.rs +++ b/crates/musli/src/json/de/mod.rs @@ -22,10 +22,7 @@ use self::variant_decoder::JsonVariantDecoder; use core::fmt; use core::str; -#[cfg(feature = "alloc")] -use alloc::vec::Vec; - -use crate::buf::BufVec; +use crate::alloc::Vec; use crate::de::{ Decode, DecodeUnsized, Decoder, SequenceDecoder, SizeHint, Skip, UnsizedVisitor, Visitor, }; @@ -205,7 +202,7 @@ where #[inline] fn decode_char(mut self) -> Result { let start = self.cx.mark(); - let mut scratch = BufVec::new(self.cx.alloc()); + let mut scratch = Vec::new_in(self.cx.alloc()); let string = match self.parser.parse_string(self.cx, true, &mut scratch)? { StringReference::Borrowed(string) => string, @@ -332,7 +329,7 @@ where let cx = self.cx; self.decode_sequence(|seq| { - let mut bytes = Vec::with_capacity(seq.size_hint().or_default()); + let mut bytes = rust_alloc::vec::Vec::with_capacity(seq.size_hint().or_default()); while let Some(item) = seq.try_decode_next()? { bytes.push(item.decode_u8()?); @@ -347,7 +344,7 @@ where where V: UnsizedVisitor<'de, C, str>, { - let mut scratch = BufVec::new(self.cx.alloc()); + let mut scratch = Vec::new_in(self.cx.alloc()); match self.parser.parse_string(self.cx, true, &mut scratch)? { StringReference::Borrowed(borrowed) => visitor.visit_borrowed(self.cx, borrowed), diff --git a/crates/musli/src/json/encoding.rs b/crates/musli/src/json/encoding.rs index 414d5bfb3..c01d73b04 100644 --- a/crates/musli/src/json/encoding.rs +++ b/crates/musli/src/json/encoding.rs @@ -4,9 +4,9 @@ use core::marker; #[cfg(feature = "alloc")] -use alloc::string::String; +use rust_alloc::string::String; #[cfg(feature = "alloc")] -use alloc::vec::Vec; +use rust_alloc::vec::Vec; use crate::mode::Text; use crate::Decode; @@ -211,7 +211,7 @@ where where T: ?Sized + Encode, { - crate::allocator::default!(|alloc| { + crate::alloc::default!(|alloc| { let cx = crate::context::Same::new(alloc); self.to_string_with(&cx, value) }) @@ -228,7 +228,7 @@ where /// ``` /// use musli::{Decode, Encode}; /// use musli::json; - /// use musli::allocator::System; + /// use musli::alloc::System; /// use musli::context::Same; /// # use musli::json::Error; /// diff --git a/crates/musli/src/json/error.rs b/crates/musli/src/json/error.rs index ca2318819..6c34441d4 100644 --- a/crates/musli/src/json/error.rs +++ b/crates/musli/src/json/error.rs @@ -1,9 +1,9 @@ use core::fmt; #[cfg(feature = "alloc")] -use alloc::boxed::Box; +use rust_alloc::boxed::Box; #[cfg(feature = "alloc")] -use alloc::string::ToString; +use rust_alloc::string::ToString; /// Error raised during json encoding. #[derive(Debug)] diff --git a/crates/musli/src/json/parser/mut_slice_parser.rs b/crates/musli/src/json/parser/mut_slice_parser.rs index 39b9c6381..b50d6d62e 100644 --- a/crates/musli/src/json/parser/mut_slice_parser.rs +++ b/crates/musli/src/json/parser/mut_slice_parser.rs @@ -1,8 +1,8 @@ -use crate::buf::BufVec; +use crate::alloc::{Allocator, Vec}; use crate::json::error::ErrorMessage; use crate::json::parser::{Parser, StringReference, Token}; use crate::reader::SliceUnderflow; -use crate::{Buf, Context}; +use crate::Context; use super::string::SliceAccess; @@ -41,7 +41,7 @@ impl<'de> Parser<'de> for MutSliceParser<'_, 'de> { &mut self, cx: &C, validate: bool, - scratch: &'scratch mut BufVec>, + scratch: &'scratch mut Vec<'_, u8, (impl Allocator + ?Sized)>, ) -> Result, C::Error> where C: ?Sized + Context, diff --git a/crates/musli/src/json/parser/parser.rs b/crates/musli/src/json/parser/parser.rs index c528764e2..527337e24 100644 --- a/crates/musli/src/json/parser/parser.rs +++ b/crates/musli/src/json/parser/parser.rs @@ -1,8 +1,8 @@ -use crate::buf::BufVec; +use crate::alloc::{Allocator, Vec}; use crate::de::Visitor; use crate::json::parser::integer::decode_signed_full; use crate::json::parser::{StringReference, Token}; -use crate::{Buf, Context}; +use crate::Context; mod private { pub trait Sealed {} @@ -36,7 +36,7 @@ pub trait Parser<'de>: private::Sealed { &mut self, cx: &C, validate: bool, - scratch: &'scratch mut BufVec>, + scratch: &'scratch mut Vec<'_, u8, (impl Allocator + ?Sized)>, ) -> Result, C::Error> where C: ?Sized + Context; @@ -231,7 +231,7 @@ where &mut self, cx: &C, validate: bool, - scratch: &'scratch mut BufVec>, + scratch: &'scratch mut Vec<'_, u8, (impl Allocator + ?Sized)>, ) -> Result, C::Error> where C: ?Sized + Context, diff --git a/crates/musli/src/json/parser/slice_parser.rs b/crates/musli/src/json/parser/slice_parser.rs index bc055815c..0acc5ec95 100644 --- a/crates/musli/src/json/parser/slice_parser.rs +++ b/crates/musli/src/json/parser/slice_parser.rs @@ -1,8 +1,8 @@ -use crate::buf::BufVec; +use crate::alloc::{Allocator, Vec}; use crate::json::error::ErrorMessage; use crate::json::parser::{Parser, StringReference, Token}; use crate::reader::SliceUnderflow; -use crate::{Buf, Context}; +use crate::Context; use super::string::SliceAccess; @@ -33,7 +33,7 @@ impl<'de> Parser<'de> for SliceParser<'de> { &mut self, cx: &C, validate: bool, - scratch: &'scratch mut BufVec>, + scratch: &'scratch mut Vec<'_, u8, (impl Allocator + ?Sized)>, ) -> Result, C::Error> where C: ?Sized + Context, diff --git a/crates/musli/src/json/parser/string.rs b/crates/musli/src/json/parser/string.rs index 59a5730a8..a48ed7e0e 100644 --- a/crates/musli/src/json/parser/string.rs +++ b/crates/musli/src/json/parser/string.rs @@ -1,7 +1,7 @@ #![allow(clippy::zero_prefixed_literal)] -use crate::buf::BufVec; -use crate::{Buf, Context}; +use crate::alloc::{Allocator, Vec}; +use crate::Context; // Copied and adapter form the serde-json project under the MIT and Apache 2.0 // license. @@ -102,7 +102,7 @@ where pub(crate) fn parse_escape( &mut self, validate: bool, - scratch: &mut BufVec>, + scratch: &mut Vec<'_, u8, (impl Allocator + ?Sized)>, ) -> Result { let start = self.cx.mark(); let b = self.next()?; @@ -117,7 +117,10 @@ where b'r' => scratch.push(b'\r'), b't' => scratch.push(b'\t'), b'u' => { - fn encode_surrogate(scratch: &mut BufVec>, n: u16) -> bool { + fn encode_surrogate( + scratch: &mut Vec<'_, u8, (impl Allocator + ?Sized)>, + n: u16, + ) -> bool { scratch.write(&[ (n >> 12 & 0b0000_1111) as u8 | 0b1110_0000, (n >> 6 & 0b0011_1111) as u8 | 0b1000_0000, @@ -282,7 +285,7 @@ where &mut self, validate: bool, start: C::Mark, - scratch: &'scratch mut BufVec>, + scratch: &'scratch mut Vec<'_, u8, (impl Allocator + ?Sized)>, ) -> Result, C::Error> { // Index of the first byte not yet copied into the scratch space. let mut open_mark = self.cx.mark(); diff --git a/crates/musli/src/json/parser/tests.rs b/crates/musli/src/json/parser/tests.rs index 3559a6f9d..602865b38 100644 --- a/crates/musli/src/json/parser/tests.rs +++ b/crates/musli/src/json/parser/tests.rs @@ -1,6 +1,6 @@ #![cfg(feature = "std")] -use alloc::format; +use rust_alloc::format; use crate::context; use crate::json::error::Error; @@ -10,7 +10,7 @@ use crate::mode::Binary; #[test] fn test_decode_exponent() { - crate::allocator::default!(|alloc| { + crate::alloc::default!(|alloc| { let cx = context::Same::<_, Binary, Error>::new(alloc); macro_rules! test_number { @@ -47,7 +47,7 @@ fn test_decode_exponent() { #[test] fn test_decode_unsigned() { - crate::allocator::default!(|alloc| { + crate::alloc::default!(|alloc| { let cx = context::Same::<_, Binary, Error>::new(alloc); macro_rules! test_number { @@ -115,7 +115,7 @@ fn test_decode_unsigned() { #[test] fn test_decode_signed() { - crate::allocator::default!(|alloc| { + crate::alloc::default!(|alloc| { let cx = context::Same::<_, Binary, Error>::new(alloc); macro_rules! test_number { diff --git a/crates/musli/src/lib.rs b/crates/musli/src/lib.rs index c9f2a865e..514c6687f 100644 --- a/crates/musli/src/lib.rs +++ b/crates/musli/src/lib.rs @@ -398,7 +398,7 @@ #![cfg_attr(doc_cfg, feature(doc_cfg))] #[cfg(feature = "alloc")] -extern crate alloc; +extern crate alloc as rust_alloc; #[cfg(feature = "std")] extern crate std; @@ -556,14 +556,12 @@ pub use musli_core::decoder; pub use musli_core::visitor; #[doc(inline)] -pub use musli_core::{Buf, Context, Decode, Decoder, Encode, Encoder}; +pub use musli_core::{Context, Decode, Decoder, Encode, Encoder}; #[doc(hidden)] pub use musli_core::__priv; -pub mod allocator; -#[doc(inline)] -pub use self::allocator::Allocator; +pub mod alloc; pub mod descriptive; pub mod json; @@ -584,8 +582,6 @@ pub mod options; #[doc(inline)] pub use self::options::Options; -pub mod buf; - pub mod reader; #[doc(inline)] pub use self::reader::{IntoReader, Reader}; diff --git a/crates/musli/src/macros/internal.rs b/crates/musli/src/macros/internal.rs index 9150bf4ae..fcfa9debc 100644 --- a/crates/musli/src/macros/internal.rs +++ b/crates/musli/src/macros/internal.rs @@ -44,7 +44,7 @@ macro_rules! bare_encoding { /// Encode the given value to a [`Vec`] using the [`DEFAULT`] /// [`Encoding`]. /// - /// [`Vec`]: alloc::vec::Vec + /// [`Vec`]: rust_alloc::vec::Vec /// /// # Examples /// @@ -72,7 +72,7 @@ macro_rules! bare_encoding { #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] #[inline] - pub fn to_vec(value: &T) -> Result, Error> + pub fn to_vec(value: &T) -> Result, Error> where T: ?Sized + $crate::Encode, { @@ -275,7 +275,7 @@ macro_rules! encoding_impls { W: $crate::Writer, T: ?Sized + $crate::Encode<$mode>, { - $crate::allocator::default!(|alloc| { + $crate::alloc::default!(|alloc| { let cx = $crate::context::Same::new(alloc); self.encode_with(&cx, writer, value) }) @@ -283,7 +283,7 @@ macro_rules! encoding_impls { /// Encode the given value to a [`Vec`] using the current [`Encoding`]. /// - /// [`Vec`]: alloc::vec::Vec + /// [`Vec`]: rust_alloc::vec::Vec /// /// # Examples /// @@ -313,11 +313,11 @@ macro_rules! encoding_impls { #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] #[inline] - pub fn to_vec(self, value: &T) -> Result, Error> + pub fn to_vec(self, value: &T) -> Result, Error> where T: ?Sized + $crate::Encode<$mode>, { - let mut vec = alloc::vec::Vec::new(); + let mut vec = rust_alloc::vec::Vec::new(); self.encode(&mut vec, value)?; Ok(vec) } @@ -358,7 +358,7 @@ macro_rules! encoding_impls { where T: ?Sized + $crate::Encode<$mode>, { - $crate::allocator::default!(|alloc| { + $crate::alloc::default!(|alloc| { let cx = $crate::context::Same::new(alloc); self.to_fixed_bytes_with(&cx, value) }) @@ -450,7 +450,7 @@ macro_rules! encoding_impls { R: $reader_trait<'de>, T: $crate::Decode<'de, $mode>, { - $crate::allocator::default!(|alloc| { + $crate::alloc::default!(|alloc| { let cx = $crate::context::Same::new(alloc); self.decode_with(&cx, reader) }) @@ -489,7 +489,7 @@ macro_rules! encoding_impls { where T: $crate::Decode<'de, $mode>, { - $crate::allocator::default!(|alloc| { + $crate::alloc::default!(|alloc| { let cx = $crate::context::Same::new(alloc); self.from_slice_with(&cx, bytes) }) @@ -521,7 +521,7 @@ macro_rules! encoding_impls { /// /// ``` /// use musli::{Decode, Encode}; - /// use musli::allocator::System; + /// use musli::alloc::System; /// use musli::context::Same; #[doc = concat!("use musli::", stringify!($what), "::Encoding;")] #[doc = concat!("# use musli::", stringify!($what), "::Error;")] @@ -566,13 +566,13 @@ macro_rules! encoding_impls { /// configurable [`Context`]. /// /// [`Context`]: crate::Context - /// [`Vec`]: alloc::vec::Vec + /// [`Vec`]: rust_alloc::vec::Vec /// /// # Examples /// /// ``` /// use musli::{Decode, Encode}; - /// use musli::allocator::System; + /// use musli::alloc::System; /// use musli::context::Same; #[doc = concat!("use musli::", stringify!($what), "::Encoding;")] #[doc = concat!("# use musli::", stringify!($what), "::Error;")] @@ -601,12 +601,16 @@ macro_rules! encoding_impls { #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] #[inline] - pub fn to_vec_with(self, cx: &C, value: &T) -> Result, C::Error> + pub fn to_vec_with( + self, + cx: &C, + value: &T, + ) -> Result, C::Error> where C: ?Sized + $crate::Context, T: ?Sized + $crate::Encode, { - let mut vec = alloc::vec::Vec::new(); + let mut vec = rust_alloc::vec::Vec::new(); self.encode_with(cx, &mut vec, value)?; Ok(vec) } @@ -618,7 +622,7 @@ macro_rules! encoding_impls { /// /// ``` /// use musli::{Decode, Encode, FixedBytes}; - /// use musli::allocator::System; + /// use musli::alloc::System; /// use musli::context::Same; #[doc = concat!("use musli::", stringify!($what), "::Encoding;")] #[doc = concat!("# use musli::", stringify!($what), "::Error;")] @@ -668,7 +672,7 @@ macro_rules! encoding_impls { /// /// ``` /// use musli::{Decode, Encode}; - /// use musli::allocator::System; + /// use musli::alloc::System; /// use musli::context::Same; #[doc = concat!("use musli::", stringify!($what), "::Encoding;")] #[doc = concat!("# use musli::", stringify!($what), "::Error;")] @@ -721,7 +725,7 @@ macro_rules! encoding_impls { /// /// ``` /// use musli::{Decode, Encode}; - /// use musli::allocator::System; + /// use musli::alloc::System; /// use musli::context::Same; #[doc = concat!("use musli::", stringify!($what), "::Encoding;")] #[doc = concat!("# use musli::", stringify!($what), "::Error;")] @@ -772,7 +776,7 @@ macro_rules! encoding_impls { /// /// ``` /// use musli::{Decode, Encode}; - /// use musli::allocator::System; + /// use musli::alloc::System; /// use musli::context::Same; #[doc = concat!("use musli::", stringify!($what), "::Encoding;")] #[doc = concat!("# use musli::", stringify!($what), "::Error;")] diff --git a/crates/musli/src/macros/test.rs b/crates/musli/src/macros/test.rs index 4379951eb..d6f1f3303 100644 --- a/crates/musli/src/macros/test.rs +++ b/crates/musli/src/macros/test.rs @@ -44,7 +44,7 @@ macro_rules! test_fns { } } - $crate::allocator::default!(|alloc| { + $crate::alloc::default!(|alloc| { let mut cx = $crate::context::RichContext::with_alloc(alloc); cx.include_type(); @@ -97,7 +97,7 @@ macro_rules! test_fns { /// Encode and then decode the given value once. #[doc(hidden)] #[track_caller] - pub fn decode<'de, T, U, M>(value: T, out: &'de mut ::alloc::vec::Vec, expected: &U) -> U + pub fn decode<'de, T, U, M>(value: T, out: &'de mut rust_alloc::vec::Vec, expected: &U) -> U where T: $crate::en::Encode, T: ::core::fmt::Debug + ::core::cmp::PartialEq, @@ -130,7 +130,7 @@ macro_rules! test_fns { } } - $crate::allocator::default!(|alloc| { + $crate::alloc::default!(|alloc| { let mut cx = $crate::context::RichContext::with_alloc(alloc); cx.include_type(); @@ -167,7 +167,7 @@ macro_rules! test_fns { /// Encode a value to bytes. #[doc(hidden)] #[track_caller] - pub fn to_vec(value: T) -> ::alloc::vec::Vec + pub fn to_vec(value: T) -> rust_alloc::vec::Vec where T: $crate::en::Encode, M: 'static, @@ -178,7 +178,7 @@ macro_rules! test_fns { use ::core::any::type_name; - $crate::allocator::default!(|alloc| { + $crate::alloc::default!(|alloc| { let mut cx = $crate::context::RichContext::with_alloc(alloc); cx.include_type(); @@ -429,7 +429,7 @@ pub use __test_matrix; #[doc(hidden)] pub mod support { - pub use alloc::vec::Vec; + pub use rust_alloc::vec::Vec; use crate::mode::Binary; use crate::value::{self, Value}; diff --git a/crates/musli/src/serde/deserializer.rs b/crates/musli/src/serde/deserializer.rs index 5ada1684b..7fd1e0f2c 100644 --- a/crates/musli/src/serde/deserializer.rs +++ b/crates/musli/src/serde/deserializer.rs @@ -11,9 +11,9 @@ use crate::mode::Text; use crate::Context; #[cfg(feature = "alloc")] -use alloc::string::String; +use rust_alloc::string::String; #[cfg(feature = "alloc")] -use alloc::vec::Vec; +use rust_alloc::vec::Vec; pub struct Deserializer<'de, 'a, D> where diff --git a/crates/musli/src/serde/error.rs b/crates/musli/src/serde/error.rs index edd077c66..e0c4ebf07 100644 --- a/crates/musli/src/serde/error.rs +++ b/crates/musli/src/serde/error.rs @@ -3,9 +3,9 @@ use core::fmt; use crate::Context; #[cfg(feature = "alloc")] -use alloc::boxed::Box; +use rust_alloc::boxed::Box; #[cfg(feature = "alloc")] -use alloc::format; +use rust_alloc::format; #[derive(Debug)] pub enum SerdeError { diff --git a/crates/musli/src/serde/mod.rs b/crates/musli/src/serde/mod.rs index 53cdc5a08..4e63756ca 100644 --- a/crates/musli/src/serde/mod.rs +++ b/crates/musli/src/serde/mod.rs @@ -102,7 +102,7 @@ use serde::{Deserialize, Serialize}; use self::deserializer::Deserializer; use self::serializer::Serializer; -use crate::buf::{self, BufString}; +use crate::alloc::{self, String}; use crate::no_std; use crate::{Context, Decoder, Encoder}; @@ -121,11 +121,8 @@ where type Mode = C::Mode; type Error = error::SerdeError; type Mark = C::Mark; - type Buf<'this, T> = C::Buf<'this, T> - where - Self: 'this, - T: 'this; - type BufString<'this> = BufString> + type Allocator = C::Allocator; + type String<'this> = String<'this, C::Allocator> where Self: 'this; @@ -141,16 +138,16 @@ where } #[inline] - fn alloc(&self) -> Self::Buf<'_, T> { + fn alloc(&self) -> &Self::Allocator { self.inner.alloc() } #[inline] - fn collect_string(&self, value: &T) -> Result, Self::Error> + fn collect_string(&self, value: &T) -> Result, Self::Error> where T: ?Sized + fmt::Display, { - buf::collect_string(self, value) + alloc::collect_string(self, value) } #[inline] diff --git a/crates/musli/src/storage/de.rs b/crates/musli/src/storage/de.rs index d7dac1b59..2cb30284b 100644 --- a/crates/musli/src/storage/de.rs +++ b/crates/musli/src/storage/de.rs @@ -1,7 +1,7 @@ use core::fmt; #[cfg(feature = "alloc")] -use alloc::vec::Vec; +use rust_alloc::vec::Vec; use crate::de::{ DecodeUnsized, Decoder, EntriesDecoder, EntryDecoder, MapDecoder, SequenceDecoder, SizeHint, diff --git a/crates/musli/src/storage/error.rs b/crates/musli/src/storage/error.rs index 4171f3480..ae2587ab6 100644 --- a/crates/musli/src/storage/error.rs +++ b/crates/musli/src/storage/error.rs @@ -1,9 +1,9 @@ use core::fmt; #[cfg(feature = "alloc")] -use alloc::boxed::Box; +use rust_alloc::boxed::Box; #[cfg(feature = "alloc")] -use alloc::string::ToString; +use rust_alloc::string::ToString; /// Error raised during storage encoding. #[derive(Debug)] diff --git a/crates/musli/src/str.rs b/crates/musli/src/str.rs index de913f541..26d7929ec 100644 --- a/crates/musli/src/str.rs +++ b/crates/musli/src/str.rs @@ -10,9 +10,9 @@ ))] #[cfg(feature = "alloc")] -use alloc::string::String; +use rust_alloc::string::String; #[cfg(feature = "alloc")] -use alloc::vec::Vec; +use rust_alloc::vec::Vec; use core::fmt; @@ -41,7 +41,7 @@ impl fmt::Display for Utf8Error { /// The same as [`String::from_utf8`], but the implementation can different /// depending on if the `simdutf8` feature is enabled. /// -/// [`String::from_utf8`]: alloc::string::String::from_utf8 +/// [`String::from_utf8`]: rust_alloc::string::String::from_utf8 #[inline(always)] #[cfg(all(feature = "alloc", not(feature = "simdutf8")))] pub fn from_utf8_owned(bytes: Vec) -> Result { @@ -54,7 +54,7 @@ pub fn from_utf8_owned(bytes: Vec) -> Result { /// The same as [`String::from_utf8`], but the implementation can different /// depending on if the `simdutf8` feature is enabled. /// -/// [`String::from_utf8`]: alloc::string::String::from_utf8 +/// [`String::from_utf8`]: rust_alloc::string::String::from_utf8 #[inline(always)] #[cfg(all(feature = "alloc", feature = "simdutf8"))] pub fn from_utf8_owned(bytes: Vec) -> Result { diff --git a/crates/musli/src/tests/loom.rs b/crates/musli/src/tests/loom.rs index 1f07b22a0..88a0632f1 100644 --- a/crates/musli/src/tests/loom.rs +++ b/crates/musli/src/tests/loom.rs @@ -1,7 +1,6 @@ use std::sync::Arc; -use crate::allocator::System; -use crate::buf::BufVec; +use crate::alloc::{System, Vec}; const BIG1: &[u8] = &[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, @@ -11,8 +10,8 @@ const BIG2: &[u8] = &[ ]; fn work(alloc: &System) { - let mut buf1 = BufVec::new_in(alloc); - let mut buf2 = BufVec::new_in(alloc); + let mut buf1 = Vec::new_in(alloc); + let mut buf2 = Vec::new_in(alloc); assert!(buf1.write(BIG1)); assert!(buf2.write(BIG2)); diff --git a/crates/musli/src/value/en.rs b/crates/musli/src/value/en.rs index 198291214..9c15bc8e5 100644 --- a/crates/musli/src/value/en.rs +++ b/crates/musli/src/value/en.rs @@ -1,9 +1,9 @@ use core::fmt; #[cfg(feature = "alloc")] -use alloc::boxed::Box; +use rust_alloc::boxed::Box; #[cfg(feature = "alloc")] -use alloc::vec::Vec; +use rust_alloc::vec::Vec; use crate::en::{Encode, Encoder}; #[cfg(feature = "alloc")] @@ -426,7 +426,7 @@ where { cx: &'a C, output: O, - writer: BufWriter>, + writer: BufWriter<'a, C::Allocator>, } #[cfg(feature = "alloc")] @@ -452,7 +452,7 @@ where { type Cx = C; type Ok = (); - type EncodeNext<'this> = StorageEncoder<'a, &'this mut BufWriter>, OPT, C> + type EncodeNext<'this> = StorageEncoder<'a, &'this mut BufWriter<'a, C::Allocator>, OPT, C> where Self: 'this; diff --git a/crates/musli/src/value/error.rs b/crates/musli/src/value/error.rs index a2801f52b..c6ddde0d8 100644 --- a/crates/musli/src/value/error.rs +++ b/crates/musli/src/value/error.rs @@ -1,9 +1,9 @@ use core::fmt; #[cfg(feature = "alloc")] -use alloc::boxed::Box; +use rust_alloc::boxed::Box; #[cfg(feature = "alloc")] -use alloc::string::ToString; +use rust_alloc::string::ToString; use super::type_hint::{NumberHint, TypeHint}; diff --git a/crates/musli/src/value/mod.rs b/crates/musli/src/value/mod.rs index 81a5f6f7a..9324e8e46 100644 --- a/crates/musli/src/value/mod.rs +++ b/crates/musli/src/value/mod.rs @@ -22,7 +22,7 @@ pub use self::value::{AsValueDecoder, Value}; #[doc(inline)] pub use error::Error; -use crate::allocator; +use crate::alloc; use crate::mode::Binary; use crate::value::en::ValueEncoder; use crate::{Decode, Encode, Options}; @@ -38,7 +38,7 @@ where let mut output = Value::Unit; - allocator::default!(|alloc| { + alloc::default!(|alloc| { let cx = crate::context::Same::<_, Binary, Error>::new(&alloc); ValueEncoder::::new(&cx, &mut output).encode(value)?; Ok(output) @@ -52,7 +52,7 @@ where { use crate::de::Decoder; - allocator::default!(|alloc| { + alloc::default!(|alloc| { let cx = crate::context::Same::<_, Binary, Error>::new(&alloc); value.decoder::(&cx).decode() }) diff --git a/crates/musli/src/value/value.rs b/crates/musli/src/value/value.rs index b995fa30d..71f4da873 100644 --- a/crates/musli/src/value/value.rs +++ b/crates/musli/src/value/value.rs @@ -1,11 +1,11 @@ #[cfg(feature = "alloc")] -use alloc::borrow::ToOwned; +use rust_alloc::borrow::ToOwned; #[cfg(feature = "alloc")] -use alloc::boxed::Box; +use rust_alloc::boxed::Box; #[cfg(feature = "alloc")] -use alloc::string::String; +use rust_alloc::string::String; #[cfg(feature = "alloc")] -use alloc::vec::Vec; +use rust_alloc::vec::Vec; use crate::de::{AsDecoder, Decode, Decoder, Visitor}; #[cfg(feature = "alloc")] diff --git a/crates/musli/src/wire/de.rs b/crates/musli/src/wire/de.rs index cb9d32f7a..09e7e8194 100644 --- a/crates/musli/src/wire/de.rs +++ b/crates/musli/src/wire/de.rs @@ -2,7 +2,7 @@ use core::fmt; use core::mem::take; #[cfg(feature = "alloc")] -use alloc::vec::Vec; +use rust_alloc::vec::Vec; use crate::de::{ Decode, DecodeUnsized, Decoder, EntriesDecoder, EntryDecoder, MapDecoder, SequenceDecoder, diff --git a/crates/musli/src/wire/en.rs b/crates/musli/src/wire/en.rs index 063ceb993..d8cd9683c 100644 --- a/crates/musli/src/wire/en.rs +++ b/crates/musli/src/wire/en.rs @@ -62,7 +62,7 @@ where { cx: &'a C, writer: W, - buffer: BufWriter>, + buffer: BufWriter<'a, C::Allocator>, } impl<'a, W, const OPT: Options, C> WireSequenceEncoder<'a, W, OPT, C> @@ -342,7 +342,7 @@ where { type Cx = C; type Ok = (); - type EncodeNext<'this> = StorageEncoder<'a, &'this mut BufWriter>, OPT, C> where Self: 'this; + type EncodeNext<'this> = StorageEncoder<'a, &'this mut BufWriter<'a, C::Allocator>, OPT, C> where Self: 'this; #[inline] fn encode_next(&mut self) -> Result, C::Error> { diff --git a/crates/musli/src/wire/error.rs b/crates/musli/src/wire/error.rs index cbf6ff66f..642b98d03 100644 --- a/crates/musli/src/wire/error.rs +++ b/crates/musli/src/wire/error.rs @@ -1,9 +1,9 @@ use core::fmt; #[cfg(feature = "alloc")] -use alloc::boxed::Box; +use rust_alloc::boxed::Box; #[cfg(feature = "alloc")] -use alloc::string::ToString; +use rust_alloc::string::ToString; /// Error raised during descriptive encoding. #[derive(Debug)] diff --git a/crates/musli/src/wire/tests/struct_unpack.rs b/crates/musli/src/wire/tests/struct_unpack.rs index 42596cd1b..8288bb498 100644 --- a/crates/musli/src/wire/tests/struct_unpack.rs +++ b/crates/musli/src/wire/tests/struct_unpack.rs @@ -1,4 +1,4 @@ -use alloc::string::String; +use rust_alloc::string::String; use crate::wire::tag::{Kind, Tag}; use crate::wire::test::Typed; diff --git a/crates/musli/src/wrap.rs b/crates/musli/src/wrap.rs index cf53b4a08..5e708a936 100644 --- a/crates/musli/src/wrap.rs +++ b/crates/musli/src/wrap.rs @@ -4,9 +4,9 @@ //! adapter around an I/O type to work with musli. #[cfg(feature = "std")] -use crate::buf::BufVec; +use crate::alloc::Vec; #[cfg(feature = "std")] -use crate::{Buf, Context}; +use crate::Context; /// Wrap a type so that it implements [`Reader`] and [`Writer`]. /// @@ -40,10 +40,9 @@ where } #[inline] - fn extend(&mut self, cx: &C, buffer: BufVec) -> Result<(), C::Error> + fn extend(&mut self, cx: &C, buffer: Vec<'_, u8, C::Allocator>) -> Result<(), C::Error> where C: ?Sized + Context, - B: Buf, { // SAFETY: the buffer never outlives this function call. self.write_bytes(cx, buffer.as_slice()) diff --git a/crates/musli/src/writer.rs b/crates/musli/src/writer.rs index aca112392..c4b005571 100644 --- a/crates/musli/src/writer.rs +++ b/crates/musli/src/writer.rs @@ -7,11 +7,8 @@ use core::fmt; use core::mem::take; -use crate::buf::BufVec; -use crate::{Buf, Context}; - -#[cfg(feature = "alloc")] -use alloc::vec::Vec; +use crate::alloc::{Allocator, Vec}; +use crate::Context; /// The trait governing how a writer works. pub trait Writer { @@ -33,10 +30,9 @@ pub trait Writer { fn borrow_mut(&mut self) -> Self::Mut<'_>; /// Write a buffer to the current writer. - fn extend(&mut self, cx: &C, buffer: BufVec) -> Result<(), C::Error> + fn extend(&mut self, cx: &C, buffer: Vec<'_, u8, C::Allocator>) -> Result<(), C::Error> where - C: ?Sized + Context, - B: Buf; + C: ?Sized + Context; /// Write bytes to the current writer. fn write_bytes(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error> @@ -65,10 +61,9 @@ where } #[inline] - fn extend(&mut self, cx: &C, buffer: BufVec) -> Result<(), C::Error> + fn extend(&mut self, cx: &C, buffer: Vec<'_, u8, C::Allocator>) -> Result<(), C::Error> where C: ?Sized + Context, - B: Buf, { (*self).extend(cx, buffer) } @@ -91,7 +86,7 @@ where } #[cfg(feature = "alloc")] -impl Writer for Vec { +impl Writer for rust_alloc::vec::Vec { type Mut<'this> = &'this mut Self where Self: 'this; #[inline] @@ -100,10 +95,9 @@ impl Writer for Vec { } #[inline] - fn extend(&mut self, cx: &C, buffer: BufVec) -> Result<(), C::Error> + fn extend(&mut self, cx: &C, buffer: Vec<'_, u8, C::Allocator>) -> Result<(), C::Error> where C: ?Sized + Context, - B: Buf, { // SAFETY: the buffer never outlives this function call. self.write_bytes(cx, buffer.as_slice()) @@ -139,10 +133,9 @@ impl Writer for &mut [u8] { } #[inline] - fn extend(&mut self, cx: &C, buffer: BufVec) -> Result<(), C::Error> + fn extend(&mut self, cx: &C, buffer: Vec<'_, u8, C::Allocator>) -> Result<(), C::Error> where C: ?Sized + Context, - B: Buf, { // SAFETY: the buffer never outlives this function call. self.write_bytes(cx, buffer.as_slice()) @@ -185,34 +178,34 @@ impl Writer for &mut [u8] { } } -/// A writer that writes against an underlying [`Buf`]. -pub struct BufWriter +/// A writer that writes against an underlying [`Vec`]. +pub struct BufWriter<'a, A> where - B: Buf, + A: 'a + ?Sized + Allocator, { - buf: BufVec, + buf: Vec<'a, u8, A>, } -impl BufWriter +impl<'a, A> BufWriter<'a, A> where - B: Buf, + A: 'a + ?Sized + Allocator, { /// Construct a new buffer writer. - pub fn new(buf: B) -> Self { + pub fn new(alloc: &'a A) -> Self { Self { - buf: BufVec::new(buf), + buf: Vec::new_in(alloc), } } /// Coerce into inner buffer. - pub fn into_inner(self) -> BufVec { + pub fn into_inner(self) -> Vec<'a, u8, A> { self.buf } } -impl Writer for BufWriter +impl<'a, A> Writer for BufWriter<'a, A> where - T: Buf, + A: 'a + ?Sized + Allocator, { type Mut<'this> = &'this mut Self where @@ -224,10 +217,9 @@ where } #[inline(always)] - fn extend(&mut self, cx: &C, buffer: BufVec) -> Result<(), C::Error> + fn extend(&mut self, cx: &C, buffer: Vec<'_, u8, C::Allocator>) -> Result<(), C::Error> where C: ?Sized + Context, - B: Buf, { if !self.buf.write(buffer.as_slice()) { return Err(cx.message("Buffer overflow")); diff --git a/crates/musli/tests/storage_trace.rs b/crates/musli/tests/storage_trace.rs index db102f740..6d5ac011c 100644 --- a/crates/musli/tests/storage_trace.rs +++ b/crates/musli/tests/storage_trace.rs @@ -1,6 +1,6 @@ #![allow(unused)] -use musli::allocator::System; +use musli::alloc::System; use musli::context::RichContext; use musli::{Decode, Encode}; @@ -30,7 +30,7 @@ struct To { #[test] fn storage_trace() { - musli::allocator::default!(|alloc| { + musli::alloc::default!(|alloc| { let cx = RichContext::with_alloc(alloc); let from = From { diff --git a/crates/musli/tests/trace_collection.rs b/crates/musli/tests/trace_collection.rs index fc7600f2e..a293b5b61 100644 --- a/crates/musli/tests/trace_collection.rs +++ b/crates/musli/tests/trace_collection.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -use musli::allocator::System; +use musli::alloc::System; use musli::context::RichContext; use musli::{Decode, Encode}; @@ -19,7 +19,7 @@ struct Collection { #[test] fn trace_collection() { - musli::allocator::default!(|alloc| { + musli::alloc::default!(|alloc| { let cx = RichContext::with_alloc(alloc); let mut values = HashMap::new(); diff --git a/crates/musli/tests/trace_complex.rs b/crates/musli/tests/trace_complex.rs index d0da70534..20095d57f 100644 --- a/crates/musli/tests/trace_complex.rs +++ b/crates/musli/tests/trace_complex.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -use musli::allocator::System; +use musli::alloc::System; use musli::context::RichContext; use musli::{Decode, Encode}; @@ -33,7 +33,7 @@ struct To { #[test] fn trace_complex() { - musli::allocator::default!(|alloc| { + musli::alloc::default!(|alloc| { let cx = RichContext::with_alloc(alloc); let mut field = HashMap::new(); diff --git a/crates/musli/tests/trace_no_std.rs b/crates/musli/tests/trace_no_std.rs index 0ab04c394..a920f718e 100644 --- a/crates/musli/tests/trace_no_std.rs +++ b/crates/musli/tests/trace_no_std.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -use musli::allocator::{Stack, StackBuffer}; +use musli::alloc::{Stack, StackBuffer}; use musli::context::RichContext; use musli::{Decode, Encode}; diff --git a/no-std/examples/json.rs b/no-std/examples/json.rs index 05a3ced58..5567718be 100644 --- a/no-std/examples/json.rs +++ b/no-std/examples/json.rs @@ -4,7 +4,7 @@ mod prelude; -use musli::allocator::{Stack, StackBuffer}; +use musli::alloc::{Stack, StackBuffer}; use musli::context::RichContext; use musli::{Decode, Encode}; diff --git a/no-std/examples/serde.rs b/no-std/examples/serde.rs index 127667c91..7161a1174 100644 --- a/no-std/examples/serde.rs +++ b/no-std/examples/serde.rs @@ -4,7 +4,7 @@ mod prelude; -use musli::allocator::{Stack, StackBuffer}; +use musli::alloc::{Stack, StackBuffer}; use musli::context::RichContext; use musli::{Decode, Encode}; use serde::{Deserialize, Serialize};