Skip to content

Commit

Permalink
More improvements (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog authored Jul 10, 2024
1 parent 67a310f commit ab9ace4
Show file tree
Hide file tree
Showing 39 changed files with 693 additions and 433 deletions.
3 changes: 1 addition & 2 deletions crates/musli-core/src/en/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,7 @@ pub trait Encoder: Sized {
vectors: I,
) -> Result<Self::Ok, <Self::Cx as Context>::Error>
where
I: IntoIterator,
I::Item: AsRef<[u8]>,
I: IntoIterator<Item: AsRef<[u8]>>,
{
Err(self.cx().message(expecting::unsupported_type(
&expecting::Bytes,
Expand Down
6 changes: 2 additions & 4 deletions crates/musli-macros/src/internals/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ pub(crate) struct Iter<'a, I, T> {

impl<'a, I, T> Iterator for Iter<'a, I, T>
where
I: Iterator,
I::Item: Apply<T>,
I: Iterator<Item: Apply<T>>,
{
type Item = IterItem<'a, I::Item, T>;

Expand All @@ -53,8 +52,7 @@ where
/// Apply an iterator of functions to a value.
pub(crate) fn iter<I, T>(iter: I, value: &T) -> Iter<'_, I::IntoIter, T>
where
I: IntoIterator,
I::Item: Apply<T>,
I: IntoIterator<Item: Apply<T>>,
{
Iter {
iter: iter.into_iter(),
Expand Down
24 changes: 20 additions & 4 deletions crates/musli-zerocopy/src/buf/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,39 @@ mod sealed {

pub trait Sealed {}

impl<K, V, E: ByteOrder, O: Size> Sealed for crate::phf::map::MapRef<K, V, E, O>
impl<K, V, E, O> Sealed for crate::phf::map::MapRef<K, V, E, O>
where
K: ZeroCopy,
V: ZeroCopy,
E: ByteOrder,
O: Size,
{
}

impl<K, V, E: ByteOrder, O: Size> Sealed for crate::swiss::map::MapRef<K, V, E, O>
impl<K, V, E, O> Sealed for crate::swiss::map::MapRef<K, V, E, O>
where
K: ZeroCopy,
V: ZeroCopy,
E: ByteOrder,
O: Size,
{
}

impl<T, E: ByteOrder, O: Size> Sealed for crate::phf::set::SetRef<T, E, O> where T: ZeroCopy {}
impl<T, E, O> Sealed for crate::phf::set::SetRef<T, E, O>
where
T: ZeroCopy,
E: ByteOrder,
O: Size,
{
}

impl<T, E: ByteOrder, O: Size> Sealed for crate::swiss::set::SetRef<T, E, O> where T: ZeroCopy {}
impl<T, E, O> Sealed for crate::swiss::set::SetRef<T, E, O>
where
T: ZeroCopy,
E: ByteOrder,
O: Size,
{
}
}

/// Trait used for binding a reference to a [`Buf`] through [`Buf::bind()`].
Expand Down
8 changes: 3 additions & 5 deletions crates/musli-zerocopy/src/buf/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,13 +827,11 @@ impl Buf {
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
#[inline]
pub fn swap<T, E: ByteOrder, O: Size>(
&mut self,
a: Ref<T, E, O>,
b: Ref<T, E, O>,
) -> Result<(), Error>
pub fn swap<T, E, O>(&mut self, a: Ref<T, E, O>, b: Ref<T, E, O>) -> Result<(), Error>
where
T: ZeroCopy,
E: ByteOrder,
O: Size,
{
let a = a.offset();
let b = b.offset();
Expand Down
28 changes: 22 additions & 6 deletions crates/musli-zerocopy/src/buf/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ pub trait LoadMut: Load {
fn load_mut<'buf>(&self, buf: &'buf mut Buf) -> Result<&'buf mut Self::Target, Error>;
}

impl<T, E: ByteOrder, O: Size> Load for Ref<T, E, O>
impl<T, E, O> Load for Ref<T, E, O>
where
T: ZeroCopy,
E: ByteOrder,
O: Size,
{
type Target = T;

Expand All @@ -45,9 +47,11 @@ where
}
}

impl<T, E: ByteOrder, O: Size> Load for Ref<[T], E, O>
impl<T, E, O> Load for Ref<[T], E, O>
where
T: ZeroCopy,
E: ByteOrder,
O: Size,
{
type Target = [T];

Expand All @@ -57,7 +61,11 @@ where
}
}

impl<E: ByteOrder, O: Size> Load for Ref<str, E, O> {
impl<E, O> Load for Ref<str, E, O>
where
E: ByteOrder,
O: Size,
{
type Target = str;

#[inline]
Expand All @@ -66,27 +74,35 @@ impl<E: ByteOrder, O: Size> Load for Ref<str, E, O> {
}
}

impl<T, E: ByteOrder, O: Size> LoadMut for Ref<T, E, O>
impl<T, E, O> LoadMut for Ref<T, E, O>
where
T: ZeroCopy,
E: ByteOrder,
O: Size,
{
#[inline]
fn load_mut<'buf>(&self, buf: &'buf mut Buf) -> Result<&'buf mut Self::Target, Error> {
buf.load_sized_mut::<T>(self.offset())
}
}

impl<T, E: ByteOrder, O: Size> LoadMut for Ref<[T], E, O>
impl<T, E, O> LoadMut for Ref<[T], E, O>
where
T: ZeroCopy,
E: ByteOrder,
O: Size,
{
#[inline]
fn load_mut<'buf>(&self, buf: &'buf mut Buf) -> Result<&'buf mut Self::Target, Error> {
buf.load_unsized_mut(*self)
}
}

impl<E: ByteOrder, O: Size> LoadMut for Ref<str, E, O> {
impl<E, O> LoadMut for Ref<str, E, O>
where
E: ByteOrder,
O: Size,
{
#[inline]
fn load_mut<'buf>(&self, buf: &'buf mut Buf) -> Result<&'buf mut Self::Target, Error> {
buf.load_unsized_mut(*self)
Expand Down
69 changes: 57 additions & 12 deletions crates/musli-zerocopy/src/buf/owned_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ use crate::traits::{UnsizedZeroCopy, ZeroCopy};
/// let mut buf = OwnedBuf::new();
/// buf.store(&Custom { field: 10 });
/// ```
pub struct OwnedBuf<E: ByteOrder = Native, O: Size = DefaultSize> {
pub struct OwnedBuf<E = Native, O = DefaultSize>
where
E: ByteOrder,
O: Size,
{
data: NonNull<u8>,
/// The initialized length of the buffer.
len: usize,
Expand Down Expand Up @@ -169,7 +173,11 @@ impl OwnedBuf {
}
}

impl<E: ByteOrder, O: Size> OwnedBuf<E, O> {
impl<E, O> OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
/// Modify the buffer to utilize the specified pointer size when inserting
/// references.
///
Expand Down Expand Up @@ -207,7 +215,10 @@ impl<E: ByteOrder, O: Size> OwnedBuf<E, O> {
/// .with_byte_order::<endian::Little>();
/// ```
#[inline]
pub fn with_byte_order<U: ByteOrder>(self) -> OwnedBuf<U, O> {
pub fn with_byte_order<U>(self) -> OwnedBuf<U, O>
where
U: ByteOrder,
{
let this = ManuallyDrop::new(self);

OwnedBuf {
Expand Down Expand Up @@ -539,12 +550,14 @@ impl<E: ByteOrder, O: Size> OwnedBuf<E, O> {
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
#[inline]
pub fn load_uninit_mut<T, U: ByteOrder, I: Size>(
pub fn load_uninit_mut<T, U, I>(
&mut self,
reference: Ref<MaybeUninit<T>, U, I>,
) -> &mut MaybeUninit<T>
where
T: ZeroCopy,
U: ByteOrder,
I: Size,
{
let at = reference.offset();

Expand Down Expand Up @@ -1125,7 +1138,11 @@ unsafe impl Send for OwnedBuf {}
/// unaliased.
unsafe impl Sync for OwnedBuf {}

impl<E: ByteOrder, O: Size> Deref for OwnedBuf<E, O> {
impl<E, O> Deref for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
type Target = Buf;

#[inline]
Expand All @@ -1134,14 +1151,22 @@ impl<E: ByteOrder, O: Size> Deref for OwnedBuf<E, O> {
}
}

impl<E: ByteOrder, O: Size> DerefMut for OwnedBuf<E, O> {
impl<E, O> DerefMut for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
Buf::new_mut(self.as_mut_slice())
}
}

impl<E: ByteOrder, O: Size> AsRef<Buf> for OwnedBuf<E, O> {
impl<E, O> AsRef<Buf> for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
/// Trivial `AsRef<Buf>` implementation for `OwnedBuf<O>`.
///
/// # Examples
Expand All @@ -1162,7 +1187,11 @@ impl<E: ByteOrder, O: Size> AsRef<Buf> for OwnedBuf<E, O> {
}
}

impl<E: ByteOrder, O: Size> AsMut<Buf> for OwnedBuf<E, O> {
impl<E, O> AsMut<Buf> for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
/// Trivial `AsMut<Buf>` implementation for `OwnedBuf<O>`.
///
/// # Examples
Expand All @@ -1184,7 +1213,11 @@ impl<E: ByteOrder, O: Size> AsMut<Buf> for OwnedBuf<E, O> {
}
}

impl<E: ByteOrder, O: Size> Borrow<Buf> for OwnedBuf<E, O> {
impl<E, O> Borrow<Buf> for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
#[inline]
fn borrow(&self) -> &Buf {
self.as_ref()
Expand Down Expand Up @@ -1219,7 +1252,11 @@ impl<E: ByteOrder, O: Size> Borrow<Buf> for OwnedBuf<E, O> {
/// buf.align_in_place();
/// assert!(buf.alignment() >= align_of::<u32>());
/// ```
impl<E: ByteOrder, O: Size> Clone for OwnedBuf<E, O> {
impl<E, O> Clone for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
fn clone(&self) -> Self {
unsafe {
let mut new = ManuallyDrop::new(Self::with_capacity_and_custom_alignment(
Expand All @@ -1235,7 +1272,11 @@ impl<E: ByteOrder, O: Size> Clone for OwnedBuf<E, O> {
}
}

impl<E: ByteOrder, O: Size> Drop for OwnedBuf<E, O> {
impl<E, O> Drop for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
fn drop(&mut self) {
unsafe {
if self.capacity != 0 {
Expand Down Expand Up @@ -1263,7 +1304,11 @@ const fn invalid_mut<T>(addr: usize) -> *mut T {
unsafe { core::mem::transmute(addr) }
}

impl<E: ByteOrder, O: Size> StoreBuf for OwnedBuf<E, O> {
impl<E, O> StoreBuf for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
type ByteOrder = E;
type Size = O;

Expand Down
9 changes: 7 additions & 2 deletions crates/musli-zerocopy/src/buf/slice_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ where
/// .with_byte_order::<endian::Little>();
/// ```
#[inline]
pub fn with_byte_order<U: ByteOrder>(self) -> SliceMut<'a, U, O> {
pub fn with_byte_order<U>(self) -> SliceMut<'a, U, O>
where
U: ByteOrder,
{
let this = ManuallyDrop::new(self);

SliceMut {
Expand Down Expand Up @@ -436,12 +439,14 @@ where
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
#[inline]
pub fn load_uninit_mut<T, U: ByteOrder, I: Size>(
pub fn load_uninit_mut<T, U, I>(
&mut self,
reference: Ref<MaybeUninit<T>, U, I>,
) -> &mut MaybeUninit<T>
where
T: ZeroCopy,
U: ByteOrder,
I: Size,
{
let at = reference.offset();

Expand Down
14 changes: 12 additions & 2 deletions crates/musli-zerocopy/src/buf/store_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ mod sealed {

pub trait Sealed {}

impl<'a, E: ByteOrder, O: Size> Sealed for SliceMut<'a, E, O> {}
impl<'a, E, O> Sealed for SliceMut<'a, E, O>
where
E: ByteOrder,
O: Size,
{
}

#[cfg(feature = "alloc")]
impl<E: ByteOrder, O: Size> Sealed for OwnedBuf<E, O> {}
impl<E, O> Sealed for OwnedBuf<E, O>
where
E: ByteOrder,
O: Size,
{
}
}

/// A buffer that we can store things into.
Expand Down
6 changes: 4 additions & 2 deletions crates/musli-zerocopy/src/buf/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ impl<T: ?Sized> Visit for &T {
}
}

impl<T: ?Sized, E: ByteOrder, O: Size> Visit for Ref<T, E, O>
impl<T, E, O> Visit for Ref<T, E, O>
where
T: Pointee,
T: ?Sized + Pointee,
Self: Load,
E: ByteOrder,
O: Size,
{
type Target = <Ref<T, E, O> as Load>::Target;

Expand Down
Loading

0 comments on commit ab9ace4

Please sign in to comment.