Skip to content

Commit

Permalink
Standardize import style (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlf authored Oct 5, 2022
1 parent 719cfa6 commit 7488026
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 65 deletions.
26 changes: 16 additions & 10 deletions src/byteorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@
//! }
//! ```
use core::convert::{TryFrom, TryInto};
use core::fmt::{self, Binary, Debug, Display, Formatter, LowerHex, Octal, UpperHex};
use core::marker::PhantomData;
use core::num::TryFromIntError;
use core::{
convert::{TryFrom, TryInto},
fmt::{self, Binary, Debug, Display, Formatter, LowerHex, Octal, UpperHex},
marker::PhantomData,
num::TryFromIntError,
};

use zerocopy_derive::*;

use crate::AsBytes;
// This allows the custom derives to work. See the comment on this module for an
// explanation.
use crate::zerocopy;
use crate::{
// This allows the custom derives to work. See the comment on the `zerocopy`
// module for an explanation.
zerocopy,
AsBytes,
};

// We don't reexport `WriteBytesExt` or `ReadBytesExt` because those are only
// available with the `std` feature enabled, and zerocopy is `no_std` by
Expand Down Expand Up @@ -442,8 +446,10 @@ define_type!(An, F64, f64, 64, 8, read_f64, write_f64, "floating point number",
mod tests {
use byteorder::NativeEndian;

use super::*;
use crate::{AsBytes, FromBytes, Unaligned};
use {
super::*,
crate::{AsBytes, FromBytes, Unaligned},
};

// A native integer type (u16, i32, etc).
trait Native: FromBytes + AsBytes + Copy + PartialEq + Debug {
Expand Down
37 changes: 19 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ use core::{
ptr, slice,
};

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use {
alloc::boxed::Box,
core::{alloc::Layout, ptr::NonNull},
};

// This is a hack to allow derives of `FromBytes`, `AsBytes`, and `Unaligned` to
// work in this crate. They assume that zerocopy is linked as an extern crate,
// so they access items from it as `zerocopy::Xxx`. This makes that still work.
Expand Down Expand Up @@ -358,8 +366,8 @@ pub unsafe trait FromBytes {
// multiplied it by `size_of::<T>()`, which is guaranteed to be
// aligned).
let layout = Layout::from_size_align_unchecked(
size_of::<Self>().checked_mul(len).unwrap(),
align_of::<Self>(),
mem::size_of::<Self>().checked_mul(len).unwrap(),
mem::align_of::<Self>(),
);
if layout.size() != 0 {
let ptr = alloc::alloc::alloc_zeroed(layout) as *mut Self;
Expand Down Expand Up @@ -649,10 +657,9 @@ mod simd {
macro_rules! simd_arch_mod {
($arch:ident, $($typ:ident),*) => {
mod $arch {
use {
crate::*,
core::arch::$arch::{$($typ),*},
};
use core::arch::$arch::{$($typ),*};

use crate::*;

impl_for_types!(FromBytes, $($typ),*);
impl_for_types!(AsBytes, $($typ),*);
Expand Down Expand Up @@ -1985,13 +1992,9 @@ unsafe impl<'a> ByteSliceMut for RefMut<'a, [u8]> {}

#[cfg(feature = "alloc")]
mod alloc_support {
pub(crate) extern crate alloc;
pub(crate) use super::*;
pub(crate) use alloc::alloc::Layout;
pub(crate) use alloc::boxed::Box;
pub(crate) use alloc::vec::Vec;
pub(crate) use core::mem::{align_of, size_of};
pub(crate) use core::ptr::NonNull;
use alloc::vec::Vec;

use super::*;

/// Extends a `Vec<T>` by pushing `additional` new items onto the end of the
/// vector. The new items are initialized with zeroes.
Expand Down Expand Up @@ -2217,7 +2220,9 @@ pub use alloc_support::*;
mod tests {
#![allow(clippy::unreadable_literal)]

use {super::*, core::ops::Deref};
use core::ops::Deref;

use super::*;

// `B` should be `[u8; N]`. `T` will require that the entire structure is
// aligned to the alignment of `T`.
Expand Down Expand Up @@ -2897,10 +2902,6 @@ mod tests {

#[test]
fn test_array() {
// This is a hack, as per above in `test_as_bytes_methods`.
mod zerocopy {
pub use crate::*;
}
#[derive(FromBytes, AsBytes)]
#[repr(C)]
struct Foo {
Expand Down
17 changes: 9 additions & 8 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
mod ext;
mod repr;

use proc_macro2::Span;
use quote::quote;
use syn::visit::{self, Visit};
use syn::{
parse_quote, punctuated::Punctuated, token::Comma, Data, DataEnum, DataStruct, DataUnion,
DeriveInput, Error, GenericParam, Ident, Lifetime, Type, TypePath,
use {
proc_macro2::Span,
quote::quote,
syn::visit::{self, Visit},
syn::{
parse_quote, punctuated::Punctuated, token::Comma, Data, DataEnum, DataStruct, DataUnion,
DeriveInput, Error, GenericParam, Ident, Lifetime, Type, TypePath,
},
};

use ext::*;
use repr::*;
use {crate::ext::*, crate::repr::*};

// TODO(https://github.com/rust-lang/rust/issues/54140): Some errors could be
// made better if we could add multiple lines of error output like this:
Expand Down
8 changes: 5 additions & 3 deletions zerocopy-derive/src/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use core::fmt::{self, Display, Formatter};

use proc_macro2::Span;
use syn::spanned::Spanned;
use syn::{Attribute, DeriveInput, Error, Lit, Meta, NestedMeta};
use {
proc_macro2::Span,
syn::spanned::Spanned,
syn::{Attribute, DeriveInput, Error, Lit, Meta, NestedMeta},
};

pub struct Config<Repr: KindRepr> {
// A human-readable message describing what combinations of representations
Expand Down
3 changes: 1 addition & 2 deletions zerocopy-derive/tests/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

extern crate zerocopy as _zerocopy;

use std::marker::PhantomData;
use std::option::IntoIter;
use std::{marker::PhantomData, option::IntoIter};

use _zerocopy::FromBytes;

Expand Down
3 changes: 1 addition & 2 deletions zerocopy-derive/tests/struct_as_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#![allow(warnings)]

use std::marker::PhantomData;
use std::option::IntoIter;
use std::{marker::PhantomData, option::IntoIter};

use zerocopy::AsBytes;

Expand Down
3 changes: 1 addition & 2 deletions zerocopy-derive/tests/struct_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#![allow(warnings)]

use std::marker::PhantomData;
use std::option::IntoIter;
use std::{marker::PhantomData, option::IntoIter};

use zerocopy::FromBytes;

Expand Down
3 changes: 1 addition & 2 deletions zerocopy-derive/tests/struct_unaligned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#![allow(warnings)]

use std::marker::PhantomData;
use std::option::IntoIter;
use std::{marker::PhantomData, option::IntoIter};

use zerocopy::Unaligned;

Expand Down
24 changes: 12 additions & 12 deletions zerocopy-derive/tests/ui/late_compile_pass.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,49 @@ note: required by a bound in `ImplementsFromBytes`
= note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui/late_compile_pass.rs:39:10
--> tests/ui/late_compile_pass.rs:38:10
|
39 | #[derive(Unaligned)]
38 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
|
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned1 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui/late_compile_pass.rs:39:10
--> tests/ui/late_compile_pass.rs:38:10
|
39 | #[derive(Unaligned)]
38 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned1 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui/late_compile_pass.rs:47:10
--> tests/ui/late_compile_pass.rs:46:10
|
47 | #[derive(Unaligned)]
46 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
|
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned2 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui/late_compile_pass.rs:47:10
--> tests/ui/late_compile_pass.rs:46:10
|
47 | #[derive(Unaligned)]
46 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned2 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui/late_compile_pass.rs:54:10
--> tests/ui/late_compile_pass.rs:53:10
|
54 | #[derive(Unaligned)]
53 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
|
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned3 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui/late_compile_pass.rs:54:10
--> tests/ui/late_compile_pass.rs:53:10
|
54 | #[derive(Unaligned)]
53 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned3 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)
3 changes: 1 addition & 2 deletions zerocopy-derive/tests/union_as_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#![allow(warnings)]

use std::marker::PhantomData;
use std::option::IntoIter;
use std::{marker::PhantomData, option::IntoIter};

use zerocopy::AsBytes;

Expand Down
3 changes: 1 addition & 2 deletions zerocopy-derive/tests/union_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#![allow(warnings)]

use std::marker::PhantomData;
use std::option::IntoIter;
use std::{marker::PhantomData, option::IntoIter};

use zerocopy::FromBytes;

Expand Down
3 changes: 1 addition & 2 deletions zerocopy-derive/tests/union_unaligned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#![allow(warnings)]

use std::marker::PhantomData;
use std::option::IntoIter;
use std::{marker::PhantomData, option::IntoIter};

use zerocopy::Unaligned;

Expand Down

0 comments on commit 7488026

Please sign in to comment.