Skip to content

Commit a17a896

Browse files
committed
Update documentation somewhat
1 parent e98c7f7 commit a17a896

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

library/std/src/io/error.rs

+10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ enum ErrorData<C> {
8787
// higher already. We include it just because repr_bitpacked.rs's encoding
8888
// requires an alignment >= 4 (note that `#[repr(align)]` will not reduce the
8989
// alignment required by the struct, only increase it).
90+
//
91+
// If we add more variants to ErrorData, this can be increased to 8, but it
92+
// should probably be behind `#[cfg_attr(target_pointer_width = "64", ...)]` or
93+
// whatever cfg we're using to enable the `repr_bitpacked` code, since only the
94+
// that version needs the alignment, and 8 is higher than the alignment we'll
95+
// have on 32 bit platforms.
96+
//
97+
// (For the sake of being explicit: the alignment requirement here only matters
98+
// if `error/repr_bitpacked.rs` is in use — for the unpacked repr it doesn't
99+
// matter at all)
90100
#[repr(align(4))]
91101
pub(crate) struct SimpleMessage {
92102
kind: ErrorKind,

library/std/src/io/error/repr_bitpacked.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -321,23 +321,26 @@ fn kind_from_prim(ek: u32) -> Option<ErrorKind> {
321321
// that our encoding relies on for correctness and soundness. (Some of these are
322322
// a bit overly thorough/cautious, admittedly)
323323
//
324-
// If any of these are hit on a platform that libstd supports, we should just
325-
// make sure `repr_unpacked.rs` is used instead.
324+
// If any of these are hit on a platform that libstd supports, we should likely
325+
// just use `repr_unpacked.rs` there instead (unless the fix is easy).
326326
macro_rules! static_assert {
327327
($condition:expr) => {
328-
const _: [(); 0] = [(); (!$condition) as usize];
328+
const _: () = assert!($condition);
329+
};
330+
(@usize_eq: $lhs:expr, $rhs:expr) => {
331+
const _: [(); $lhs] = [(); $rhs];
329332
};
330333
}
331334

332335
// The bitpacking we use requires pointers be exactly 64 bits.
333-
static_assert!(size_of::<NonNull<()>>() == 8);
336+
static_assert!(@usize_eq: size_of::<NonNull<()>>(), 8);
334337

335338
// We also require pointers and usize be the same size.
336-
static_assert!(size_of::<NonNull<()>>() == size_of::<usize>());
339+
static_assert!(@usize_eq: size_of::<NonNull<()>>(), size_of::<usize>());
337340

338341
// `Custom` and `SimpleMessage` need to be thin pointers.
339-
static_assert!(size_of::<&'static SimpleMessage>() == 8);
340-
static_assert!(size_of::<Box<Custom>>() == 8);
342+
static_assert!(@usize_eq: size_of::<&'static SimpleMessage>(), 8);
343+
static_assert!(@usize_eq: size_of::<Box<Custom>>(), 8);
341344

342345
// And they must have >= 4 byte alignment.
343346
static_assert!(align_of::<SimpleMessage>() >= 4);
@@ -346,20 +349,21 @@ static_assert!(align_of::<Custom>() >= 4);
346349
// This is obviously true (`TAG_CUSTOM` is `0b01`), but our implementation of
347350
// `Repr::new_custom` and such would be wrong if it were not, so we check.
348351
static_assert!(size_of::<Custom>() >= TAG_CUSTOM);
352+
349353
// These two store a payload which is allowed to be zero, so they must be
350354
// non-zero to preserve the `NonNull`'s range invariant.
351355
static_assert!(TAG_OS != 0);
352356
static_assert!(TAG_SIMPLE != 0);
353357
// We can't tag `SimpleMessage`s, the tag must be 0.
354-
static_assert!(TAG_SIMPLE_MESSAGE == 0);
358+
static_assert!(@usize_eq: TAG_SIMPLE_MESSAGE, 0);
355359

356360
// Check that the point of all of this still holds.
357361
//
358362
// We'd check against `io::Error`, but *technically* it's allowed to vary,
359363
// as it's not `#[repr(transparent)]`/`#[repr(C)]`. We could add that, but
360364
// the `#[repr()]` would show up in rustdoc, which might be seen as a stable
361365
// commitment.
362-
static_assert!(size_of::<Repr>() == 8);
363-
static_assert!(size_of::<Option<Repr>>() == 8);
364-
static_assert!(size_of::<Result<(), Repr>>() == 8);
365-
static_assert!(size_of::<Result<usize, Repr>>() == 16);
366+
static_assert!(@usize_eq: size_of::<Repr>(), 8);
367+
static_assert!(@usize_eq: size_of::<Option<Repr>>(), 8);
368+
static_assert!(@usize_eq: size_of::<Result<(), Repr>>(), 8);
369+
static_assert!(@usize_eq: size_of::<Result<usize, Repr>>(), 16);

0 commit comments

Comments
 (0)