Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Format code using 'cargo fmt' #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions typic-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ use syn::{Attribute, Lit, Meta, NestedMeta, Visibility};
pub fn stable_abi(input: TokenStream) -> TokenStream {
use syn::DeriveInput;
let DeriveInput {
ident,
generics,
..
ident, generics, ..
} = parse_macro_input!(input as DeriveInput);

let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
Expand All @@ -34,8 +32,9 @@ pub fn stable_abi(input: TokenStream) -> TokenStream {
{
type Type = Self;
}

}).into()

})
.into()
}

#[proc_macro_attribute]
Expand Down Expand Up @@ -118,24 +117,21 @@ fn impl_struct(definition: syn::ItemStruct) -> TokenStream {
let fields = definition
.fields
.iter()
.rfold(
quote! {typic::internal::PNil},
|rest, field| {
let vis = if let Visibility::Public(_) = field.vis {
.rfold(quote! {typic::internal::PNil}, |rest, field| {
let vis = if let Visibility::Public(_) = field.vis {
format_ident!("Public")
} else {
} else {
format_ident!("Private")
};
let field = field.ty.clone();
quote! {
typic::internal::PCons<
typic::internal::Field<
typic::internal::field::#vis ,
#field>,
#rest>
}
},
);
};
let field = field.ty.clone();
quote! {
typic::internal::PCons<
typic::internal::Field<
typic::internal::field::#vis ,
#field>,
#rest>
}
});

(quote! {
#definition
Expand Down
36 changes: 15 additions & 21 deletions typic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,39 +223,33 @@ pub mod extras {
pub mod zerocopy {
use crate::layout::*;
use crate::transmute::*;
use typenum::U1;
use generic_array::{ArrayLength as Length, GenericArray as Array};
use typenum::U1;

/// Indicates `Self` can be produced from an
/// appropriately-sized array of arbitrarily
/// initialized bytes.
pub unsafe trait FromBytes<O: TransmuteOptions = ()>
{}
pub unsafe trait FromBytes<O: TransmuteOptions = ()> {}

unsafe impl<T, O: TransmuteOptions> FromBytes<O> for T
where
unsafe impl<T, O: TransmuteOptions> FromBytes<O> for T where
T: Layout + TransmuteFrom<Array<u8, SizeOf<T>>, O>
{}

{
}

/// Indicates `Self` can be converted into an
/// appropriately-sized array of arbitrarily
/// initialized bytes.
pub unsafe trait AsBytes<O: TransmuteOptions = ()> {}

unsafe impl<T, O: TransmuteOptions> AsBytes<O> for T
where
unsafe impl<T, O: TransmuteOptions> AsBytes<O> for T where
T: Layout + TransmuteInto<Array<u8, SizeOf<T>>, O>
{}

{
}

/// Indicates `Self` has no alignment requirement.
pub trait Unaligned {}

impl<T> Unaligned for T
where
T: Layout<Align=U1>,
{}
impl<T> Unaligned for T where T: Layout<Align = U1> {}
}

/// [Bytemuck](https://docs.rs/bytemuck)-style casting functions.
Expand All @@ -276,7 +270,9 @@ pub mod extras {
where
&'t T: UnsafeTransmuteInto<&'u U, neglect::Alignment>,
{
if align_of::<U>() > align_of::<T>() && (src as *const T as usize) % align_of::<U>() != 0 {
if align_of::<U>() > align_of::<T>()
&& (src as *const T as usize) % align_of::<U>() != 0
{
None
} else {
// Sound, because we dynamically enforce the alignment
Expand All @@ -285,9 +281,9 @@ pub mod extras {
}
}

use crate::layout::*;
use core::slice;
use generic_array::{ArrayLength as Length, GenericArray as Array};
use crate::layout::*;

/// Try to convert a `&T` into `&U`.
///
Expand Down Expand Up @@ -318,10 +314,8 @@ pub mod extras {
None
} else {
let len = size_of_val(src).checked_div(size_of::<U>()).unwrap_or(0);
Some(unsafe {
slice::from_raw_parts(src.as_ptr() as *const U, len)
})
Some(unsafe { slice::from_raw_parts(src.as_ptr() as *const U, len) })
}
}
}
}
}
4 changes: 1 addition & 3 deletions typic/src/private/bytelevel/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ impl<P: Product> Add<PNil> for P {
type Output = Self;
}

impl<RH, RT> Add<PCons<RH, RT>> for PNil
where
{
impl<RH, RT> Add<PCons<RH, RT>> for PNil {
type Output = PCons<RH, RT>;
}

Expand Down
2 changes: 1 addition & 1 deletion typic/src/private/bytelevel/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use reference::{Reference, Shared, SharedRef, Unique, UniqueRef};
/// The data is from a `pub` field
pub type Pub = crate::internal::Public;

/// The field is from a field that is not `pub`.
/// The field is from a field that is not `pub`.
pub type Priv = crate::internal::Private;

pub type PaddingSlot<Vis, S> = Bytes<Vis, bytes::kind::Uninitialized, S>;
Expand Down
4 changes: 2 additions & 2 deletions typic/src/private/highlevel.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! Types for encoding the high-level representation of a type's structure.

pub mod coproduct;
pub mod product;
pub mod field;
pub mod product;

use crate::private::num::Unsigned;

#[doc(hidden)]
pub use typenum::consts::*;

#[doc(inline)]
pub use field::{Field, Public, Private};
pub use field::{Field, Private, Public};

#[doc(inline)]
pub use coproduct::{Cons as CCons, Nil as CNil};
Expand Down
28 changes: 18 additions & 10 deletions typic/src/private/highlevel/field.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
use core::marker::PhantomData;
use crate::private::num::Min;
use core::marker::PhantomData;

pub struct Public;
pub struct Private;

impl Min<Public> for Public {
type Output = Public;
fn min(self, rhs: Public) -> Self::Output {Public}
type Output = Public;
fn min(self, rhs: Public) -> Self::Output {
Public
}
}

impl Min<Private> for Private {
type Output = Private;
fn min(self, rhs: Private) -> Self::Output {Private}
type Output = Private;
fn min(self, rhs: Private) -> Self::Output {
Private
}
}

impl Min<Public> for Private {
type Output = Private;
fn min(self, rhs: Public) -> Self::Output {Private}
type Output = Private;
fn min(self, rhs: Public) -> Self::Output {
Private
}
}

impl Min<Private> for Public {
type Output = Private;
fn min(self, rhs: Private) -> Self::Output {Private}
type Output = Private;
fn min(self, rhs: Private) -> Self::Output {
Private
}
}

pub struct Field<Vis, Type>(PhantomData<(Vis, Type)>);
pub struct Field<Vis, Type>(PhantomData<(Vis, Type)>);
2 changes: 1 addition & 1 deletion typic/src/private/layout/aligned_to.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Layout;
use crate::private::num::*;
use crate::internal::Public;
use crate::private::num::*;

pub trait AlignedTo<T> {}

Expand Down
31 changes: 21 additions & 10 deletions typic/src/private/layout/into_bytelevel/field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::internal::Field;
use crate::private::bytelevel::{slot::PaddingSlot, PCons};
use crate::private::layout::{Layout, PaddingNeededForField};
use crate::private::num::{self, Minimum, Min, Unsigned};
use crate::internal::Field;
use crate::private::num::{self, Min, Minimum, Unsigned};

pub trait FieldIntoByteLevel<Packed, Visibility, Offset> {
/// The padded, byte-level representation of `Self`.
Expand All @@ -14,31 +14,42 @@ pub trait FieldIntoByteLevel<Packed, Visibility, Offset> {
type Align: Unsigned;
}

impl<Packed, Visibility, Offset, V, F> FieldIntoByteLevel<Packed, Visibility, Offset> for Field<V, F>
impl<Packed, Visibility, Offset, V, F> FieldIntoByteLevel<Packed, Visibility, Offset>
for Field<V, F>
where
F: Layout<Minimum<V, Visibility>> + PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>,
F: Layout<Minimum<V, Visibility>>
+ PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>,
V: Min<Visibility>,
Offset: num::Add<<F as PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>>::Output>,

num::Sum<Offset, <F as PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>>::Output>:
num::Add<<F as Layout<Minimum<V, Visibility>>>::Size>,
num::Add<<F as Layout<Minimum<V, Visibility>>>::Size>,

num::Sum<
num::Sum<Offset, <F as PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>>::Output>,
<F as Layout<Minimum<V, Visibility>>>::Size
num::Sum<
Offset,
<F as PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>>::Output,
>,
<F as Layout<Minimum<V, Visibility>>>::Size,
>: Unsigned,

Packed: num::Min<<F as Layout<Minimum<V, Visibility>>>::Align>,
num::Minimum<Packed, <F as Layout<Minimum<V, Visibility>>>::Align>: Unsigned,
{
type Output = PCons<
PaddingSlot<Visibility, <F as PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>>::Output>,
PaddingSlot<
Visibility,
<F as PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>>::Output,
>,
<F as Layout<Minimum<V, Visibility>>>::ByteLevel,
>;

type Offset = num::Sum<
num::Sum<Offset, <F as PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>>::Output>,
<F as Layout<Minimum<V, Visibility>>>::Size
num::Sum<
Offset,
<F as PaddingNeededForField<Minimum<V, Visibility>, Offset, Packed>>::Output,
>,
<F as Layout<Minimum<V, Visibility>>>::Size,
>;

type Align = num::Minimum<Packed, <F as Layout<Minimum<V, Visibility>>>::Align>;
Expand Down
Loading