Skip to content

Commit 4ff9917

Browse files
authored
Update formatting (#2585)
gherrit-pr-id: I3cca523354ce3ce6d6dcf6d5c902106f86e445a8
1 parent b134526 commit 4ff9917

27 files changed

+277
-257
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,13 @@ jobs:
676676
steps:
677677
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
678678
- name: Check Rust formatting
679-
run: ./ci/check_fmt.sh
679+
run: |
680+
set -eo pipefail
681+
682+
# Required by `ci/check_fmt.sh` as of this writing.
683+
rustup install $(./cargo.sh --version nightly) -c rust-src,rustfmt
684+
685+
./ci/check_fmt.sh
680686
681687
check_readme:
682688
needs: generate_cache
@@ -804,7 +810,13 @@ jobs:
804810
# is `pre-push`. Running all hooks ensures that, if a new hook is added
805811
# which can't run in CI (at least not using this naive setup), we'll
806812
# notice and it will remind us to update this test.
807-
run: set -e; for hook in ./githooks/*; do $hook; done
813+
run: |
814+
set -eo pipefail
815+
816+
# Required by `ci/check_fmt.sh` as of this writing.
817+
rustup install $(./cargo.sh --version nightly) -c rust-src,rustfmt
818+
819+
for hook in ./githooks/*; do $hook; done
808820
809821
# Used to signal to branch protections that all other jobs have succeeded.
810822
all-jobs-succeed:

ci/check_fmt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ if [[ -z $files ]]
1515
then
1616
exit 1
1717
fi
18-
rustfmt --check $files >&2
18+
./cargo.sh +nightly fmt --check -- $files >&2

rustfmt.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# This file may not be copied, modified, or distributed except according to
77
# those terms.
88

9-
edition = "2021"
9+
edition = "2024"
1010

1111
# The "Default" setting has a heuristic which splits lines too aggressively.
1212
# We are willing to revisit this setting in future versions of rustfmt.
@@ -17,3 +17,6 @@ use_small_heuristics = "Max"
1717

1818
# Prevent carriage returns
1919
newline_style = "Unix"
20+
21+
imports_granularity = "Crate"
22+
group_imports = "StdExternalCrate"

src/byte_slice.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ use core::{
1515
ops::{Deref, DerefMut},
1616
};
1717

18-
#[cfg(doc)]
19-
use crate::Ref;
20-
2118
// For each trait polyfill, as soon as the corresponding feature is stable, the
2219
// polyfill import will be unused because method/function resolution will prefer
2320
// the inherent method/function over a trait method/function. Thus, we suppress
@@ -26,6 +23,8 @@ use crate::Ref;
2623
// See the documentation on `util::polyfills` for more information.
2724
#[allow(unused_imports)]
2825
use crate::util::polyfills::{self, NonNullExt as _, NumExt as _};
26+
#[cfg(doc)]
27+
use crate::Ref;
2928

3029
/// A mutable or immutable reference to a byte slice.
3130
///

src/error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,13 @@
115115
//! .map_err(|err| err.to_string())
116116
//! }).join().unwrap();
117117
//! ```
118+
#[cfg(zerocopy_core_error_1_81_0)]
119+
use core::error::Error;
118120
use core::{
119121
convert::Infallible,
120122
fmt::{self, Debug, Write},
121123
ops::Deref,
122124
};
123-
124-
#[cfg(zerocopy_core_error_1_81_0)]
125-
use core::error::Error;
126125
#[cfg(all(not(zerocopy_core_error_1_81_0), any(feature = "std", test)))]
127126
use std::error::Error;
128127

src/layout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,12 +1289,13 @@ mod tests {
12891289
#[test]
12901290
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
12911291
fn test_validate_rust_layout() {
1292-
use crate::util::testutil::*;
12931292
use core::{
12941293
convert::TryInto as _,
12951294
ptr::{self, NonNull},
12961295
};
12971296

1297+
use crate::util::testutil::*;
1298+
12981299
// This test synthesizes pointers with various metadata and uses Rust's
12991300
// built-in APIs to confirm that Rust makes decisions about type layout
13001301
// which are consistent with what we believe is guaranteed by the

src/lib.rs

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,6 @@ mod split_at;
368368
// FIXME(#252): If we make this pub, come up with a better name.
369369
mod wrappers;
370370

371-
pub use crate::byte_slice::*;
372-
pub use crate::byteorder::*;
373-
pub use crate::error::*;
374-
pub use crate::r#ref::*;
375-
pub use crate::split_at::{Split, SplitAt};
376-
pub use crate::wrappers::*;
377-
378371
use core::{
379372
cell::{Cell, UnsafeCell},
380373
cmp::Ordering,
@@ -390,28 +383,34 @@ use core::{
390383
ptr::{self, NonNull},
391384
slice,
392385
};
393-
394386
#[cfg(feature = "std")]
395387
use std::io;
396388

397389
use crate::pointer::invariant::{self, BecauseExclusive};
390+
pub use crate::{
391+
byte_slice::*,
392+
byteorder::*,
393+
error::*,
394+
r#ref::*,
395+
split_at::{Split, SplitAt},
396+
wrappers::*,
397+
};
398398

399399
#[cfg(any(feature = "alloc", test, kani))]
400400
extern crate alloc;
401401
#[cfg(any(feature = "alloc", test))]
402402
use alloc::{boxed::Box, vec::Vec};
403-
use util::MetadataOf;
404-
405403
#[cfg(any(feature = "alloc", test))]
406404
use core::alloc::Layout;
407405

408-
// Used by `TryFromBytes::is_bit_valid`.
409-
#[doc(hidden)]
410-
pub use crate::pointer::{invariant::BecauseImmutable, Maybe, Ptr};
406+
use util::MetadataOf;
407+
411408
// Used by `KnownLayout`.
412409
#[doc(hidden)]
413410
pub use crate::layout::*;
414-
411+
// Used by `TryFromBytes::is_bit_valid`.
412+
#[doc(hidden)]
413+
pub use crate::pointer::{invariant::BecauseImmutable, Maybe, Ptr};
415414
// For each trait polyfill, as soon as the corresponding feature is stable, the
416415
// polyfill import will be unused because method/function resolution will prefer
417416
// the inherent method/function over a trait method/function. Thus, we suppress
@@ -448,9 +447,6 @@ const _: () = {
448447
//
449448
// The "note" provides enough context to make it easy to figure out how to fix
450449
// the error.
451-
#[allow(unused)]
452-
use {FromZeros as FromZeroes, IntoBytes as AsBytes, Ref as LayoutVerified};
453-
454450
/// Implements [`KnownLayout`].
455451
///
456452
/// This derive analyzes various aspects of a type's layout that are needed for
@@ -553,6 +549,8 @@ use {FromZeros as FromZeroes, IntoBytes as AsBytes, Ref as LayoutVerified};
553549
#[cfg(any(feature = "derive", test))]
554550
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
555551
pub use zerocopy_derive::KnownLayout;
552+
#[allow(unused)]
553+
use {FromZeros as FromZeroes, IntoBytes as AsBytes, Ref as LayoutVerified};
556554

557555
/// Indicates that zerocopy can reason about certain aspects of a type's layout.
558556
///
@@ -1165,7 +1163,6 @@ const _: () = unsafe {
11651163
#[cfg(any(feature = "derive", test))]
11661164
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
11671165
pub use zerocopy_derive::FromZeros;
1168-
11691166
/// Analyzes whether a type is [`Immutable`].
11701167
///
11711168
/// This derive analyzes, at compile time, whether the annotated type satisfies
@@ -5550,22 +5547,22 @@ pub unsafe trait Unaligned {
55505547
Self: Sized;
55515548
}
55525549

5553-
/// Derives an optimized [`Hash`] implementation.
5550+
/// Derives optimized [`PartialEq`] and [`Eq`] implementations.
55545551
///
55555552
/// This derive can be applied to structs and enums implementing both
55565553
/// [`Immutable`] and [`IntoBytes`]; e.g.:
55575554
///
55585555
/// ```
5559-
/// # use zerocopy_derive::{ByteHash, Immutable, IntoBytes};
5560-
/// #[derive(ByteHash, Immutable, IntoBytes)]
5556+
/// # use zerocopy_derive::{ByteEq, Immutable, IntoBytes};
5557+
/// #[derive(ByteEq, Immutable, IntoBytes)]
55615558
/// #[repr(C)]
55625559
/// struct MyStruct {
55635560
/// # /*
55645561
/// ...
55655562
/// # */
55665563
/// }
55675564
///
5568-
/// #[derive(ByteHash, Immutable, IntoBytes)]
5565+
/// #[derive(ByteEq, Immutable, IntoBytes)]
55695566
/// #[repr(u8)]
55705567
/// enum MyEnum {
55715568
/// # Variant,
@@ -5575,36 +5572,30 @@ pub unsafe trait Unaligned {
55755572
/// }
55765573
/// ```
55775574
///
5578-
/// The standard library's [`derive(Hash)`][derive@Hash] produces hashes by
5579-
/// individually hashing each field and combining the results. Instead, the
5580-
/// implementations of [`Hash::hash()`] and [`Hash::hash_slice()`] generated by
5581-
/// `derive(ByteHash)` convert the entirety of `self` to a byte slice and hashes
5582-
/// it in a single call to [`Hasher::write()`]. This may have performance
5583-
/// advantages.
5584-
///
5585-
/// [`Hash`]: core::hash::Hash
5586-
/// [`Hash::hash()`]: core::hash::Hash::hash()
5587-
/// [`Hash::hash_slice()`]: core::hash::Hash::hash_slice()
5575+
/// The standard library's [`derive(Eq, PartialEq)`][derive@PartialEq] computes
5576+
/// equality by individually comparing each field. Instead, the implementation
5577+
/// of [`PartialEq::eq`] emitted by `derive(ByteHash)` converts the entirety of
5578+
/// `self` and `other` to byte slices and compares those slices for equality.
5579+
/// This may have performance advantages.
55885580
#[cfg(any(feature = "derive", test))]
55895581
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
5590-
pub use zerocopy_derive::ByteHash;
5591-
5592-
/// Derives optimized [`PartialEq`] and [`Eq`] implementations.
5582+
pub use zerocopy_derive::ByteEq;
5583+
/// Derives an optimized [`Hash`] implementation.
55935584
///
55945585
/// This derive can be applied to structs and enums implementing both
55955586
/// [`Immutable`] and [`IntoBytes`]; e.g.:
55965587
///
55975588
/// ```
5598-
/// # use zerocopy_derive::{ByteEq, Immutable, IntoBytes};
5599-
/// #[derive(ByteEq, Immutable, IntoBytes)]
5589+
/// # use zerocopy_derive::{ByteHash, Immutable, IntoBytes};
5590+
/// #[derive(ByteHash, Immutable, IntoBytes)]
56005591
/// #[repr(C)]
56015592
/// struct MyStruct {
56025593
/// # /*
56035594
/// ...
56045595
/// # */
56055596
/// }
56065597
///
5607-
/// #[derive(ByteEq, Immutable, IntoBytes)]
5598+
/// #[derive(ByteHash, Immutable, IntoBytes)]
56085599
/// #[repr(u8)]
56095600
/// enum MyEnum {
56105601
/// # Variant,
@@ -5614,15 +5605,19 @@ pub use zerocopy_derive::ByteHash;
56145605
/// }
56155606
/// ```
56165607
///
5617-
/// The standard library's [`derive(Eq, PartialEq)`][derive@PartialEq] computes
5618-
/// equality by individually comparing each field. Instead, the implementation
5619-
/// of [`PartialEq::eq`] emitted by `derive(ByteHash)` converts the entirety of
5620-
/// `self` and `other` to byte slices and compares those slices for equality.
5621-
/// This may have performance advantages.
5608+
/// The standard library's [`derive(Hash)`][derive@Hash] produces hashes by
5609+
/// individually hashing each field and combining the results. Instead, the
5610+
/// implementations of [`Hash::hash()`] and [`Hash::hash_slice()`] generated by
5611+
/// `derive(ByteHash)` convert the entirety of `self` to a byte slice and hashes
5612+
/// it in a single call to [`Hasher::write()`]. This may have performance
5613+
/// advantages.
5614+
///
5615+
/// [`Hash`]: core::hash::Hash
5616+
/// [`Hash::hash()`]: core::hash::Hash::hash()
5617+
/// [`Hash::hash_slice()`]: core::hash::Hash::hash_slice()
56225618
#[cfg(any(feature = "derive", test))]
56235619
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
5624-
pub use zerocopy_derive::ByteEq;
5625-
5620+
pub use zerocopy_derive::ByteHash;
56265621
/// Implements [`SplitAt`].
56275622
///
56285623
/// This derive can be applied to structs; e.g.:

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,10 +1112,10 @@ macro_rules! cryptocorrosion_derive_traits {
11121112

11131113
#[cfg(test)]
11141114
mod tests {
1115-
use crate::*;
11161115
use crate::{
11171116
byteorder::native_endian::{U16, U32},
11181117
util::testutil::*,
1118+
*,
11191119
};
11201120

11211121
#[derive(KnownLayout, Immutable, FromBytes, IntoBytes, PartialEq, Debug)]

src/pointer/inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
use core::{marker::PhantomData, mem, ops::Range, ptr::NonNull};
1010

11+
pub use _def::PtrInner;
12+
1113
#[allow(unused_imports)]
1214
use crate::util::polyfills::NumExt as _;
1315
use crate::{
@@ -16,8 +18,6 @@ use crate::{
1618
AlignmentError, CastError, KnownLayout, MetadataOf, SizeError, SplitAt,
1719
};
1820

19-
pub use _def::PtrInner;
20-
2121
mod _def {
2222
use super::*;
2323
/// The inner pointer stored inside a [`Ptr`][crate::Ptr].

src/pointer/ptr.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ use crate::{
2222

2323
/// Module used to gate access to [`Ptr`]'s fields.
2424
mod def {
25-
use super::*;
26-
2725
#[cfg(doc)]
2826
use super::super::invariant;
27+
use super::*;
2928

3029
/// A raw pointer with more restrictions.
3130
///
@@ -532,9 +531,8 @@ mod _conversions {
532531

533532
/// State transitions between invariants.
534533
mod _transitions {
535-
use crate::pointer::transmute::TryTransmuteFromPtr;
536-
537534
use super::*;
535+
use crate::pointer::transmute::TryTransmuteFromPtr;
538536

539537
impl<'a, T, I> Ptr<'a, T, I>
540538
where

src/util/macro_util.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
1818
#![allow(missing_debug_implementations)]
1919

20-
use core::{
21-
marker::PhantomData,
22-
mem::{self, ManuallyDrop},
23-
};
24-
2520
// FIXME(#29), FIXME(https://github.com/rust-lang/rust/issues/69835): Remove
2621
// this `cfg` when `size_of_val_raw` is stabilized.
2722
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
2823
#[cfg(not(target_pointer_width = "16"))]
2924
use core::ptr::{self, NonNull};
25+
use core::{
26+
marker::PhantomData,
27+
mem::{self, ManuallyDrop},
28+
};
3029

3130
use crate::{
3231
pointer::{
@@ -334,11 +333,12 @@ mod __size_of {
334333
}
335334
}
336335

337-
#[cfg(zerocopy_diagnostic_on_unimplemented_1_78_0)]
338-
pub use __size_of::size_of;
339336
#[cfg(not(zerocopy_diagnostic_on_unimplemented_1_78_0))]
340337
pub use core::mem::size_of;
341338

339+
#[cfg(zerocopy_diagnostic_on_unimplemented_1_78_0)]
340+
pub use __size_of::size_of;
341+
342342
/// Does the struct type `$t` have padding?
343343
///
344344
/// `$ts` is the list of the type of every field in `$t`. `$t` must be a

testutil/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
// Inlining format args isn't supported on our MSRV.
1010
#![allow(clippy::uninlined_format_args)]
1111

12-
use rustc_version::{Channel, Version};
1312
use std::{env, error::Error, fs};
1413

14+
use rustc_version::{Channel, Version};
15+
1516
struct PinnedVersions {
1617
msrv: String,
1718
stable: String,

0 commit comments

Comments
 (0)