Skip to content

Commit

Permalink
Feat+Refactor(block2/abi): Add new utils on BlockFlags
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Mabileau <[email protected]>
  • Loading branch information
PaulDance committed Jul 17, 2024
1 parent 61037e6 commit 581c372
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
49 changes: 43 additions & 6 deletions crates/block2/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![allow(unused)]

use core::fmt;
use core::ops::{BitAnd, BitOr};
use core::{ffi::c_void, mem::MaybeUninit};
use std::os::raw::{c_char, c_int, c_ulong};

Expand Down Expand Up @@ -81,6 +82,45 @@ impl BlockFlags {

/// Note: Not public ABI.
const BLOCK_HAS_EXTENDED_LAYOUT: Self = Self(1 << 31);

/// `const` version of [`PartialEq`].
pub(crate) const fn equals(self, other: Self) -> bool {
self.0 == other.0
}

/// `const` version of [`BitOr`]: adds the flags together.
pub(crate) const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}

/// `const` version of [`BitAnd`]: only keeps the common flags.
pub(crate) const fn intersect(self, other: Self) -> Self {
Self(self.0 & other.0)
}

/// Returns `true` if and only if all the flags from `other` are enabled,
/// i.e. `self & other == other`.
pub(crate) const fn has(self, other: Self) -> bool {
self.intersect(other).equals(other)
}
}

/// See [`BlockFlags::union`].
impl BitOr for BlockFlags {
type Output = Self;

fn bitor(self, other: Self) -> Self {
self.union(other)
}
}

/// See [`BlockFlags::intersect`].
impl BitAnd for BlockFlags {
type Output = Self;

fn bitand(self, other: Self) -> Self {
self.intersect(other)
}
}

impl fmt::Debug for BlockFlags {
Expand All @@ -94,7 +134,7 @@ impl fmt::Debug for BlockFlags {
$name:ident: $flag:ident
);* $(;)?} => ($(
$(#[$m])?
f.field(stringify!($name), &(self.0 & Self::$flag.0 != 0));
f.field(stringify!($name), &self.has(Self::$flag));
)*)
}
test_flags! {
Expand All @@ -119,13 +159,10 @@ impl fmt::Debug for BlockFlags {
has_extended_layout: BLOCK_HAS_EXTENDED_LAYOUT;
}

f.field(
"over_referenced",
&(self.0 & Self::BLOCK_REFCOUNT_MASK.0 == Self::BLOCK_REFCOUNT_MASK.0),
);
f.field("over_referenced", &self.has(Self::BLOCK_REFCOUNT_MASK));
f.field(
"reference_count",
&((self.0 & Self::BLOCK_REFCOUNT_MASK.0) >> 1),
&((*self & Self::BLOCK_REFCOUNT_MASK).0 >> 1),
);

f.finish_non_exhaustive()
Expand Down
4 changes: 2 additions & 2 deletions crates/block2/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub(crate) fn debug_block_header(header: &BlockHeader, f: &mut DebugStruct<'_, '
f.field(
"descriptor",
&BlockDescriptorHelper {
has_copy_dispose: header.flags.0 & BlockFlags::BLOCK_HAS_COPY_DISPOSE.0 != 0,
has_signature: header.flags.0 & BlockFlags::BLOCK_HAS_SIGNATURE.0 != 0,
has_copy_dispose: header.flags.has(BlockFlags::BLOCK_HAS_COPY_DISPOSE),
has_signature: header.flags.has(BlockFlags::BLOCK_HAS_SIGNATURE),
descriptor: header.descriptor,
},
);
Expand Down
3 changes: 1 addition & 2 deletions crates/block2/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ unsafe impl<F: ?Sized + BlockFn> Send for GlobalBlock<F> {}
// triggers an error.
impl<F: ?Sized> GlobalBlock<F> {
// TODO: Use new ABI with BLOCK_HAS_SIGNATURE
const FLAGS: BlockFlags =
BlockFlags(BlockFlags::BLOCK_IS_GLOBAL.0 | BlockFlags::BLOCK_USE_STRET.0);
const FLAGS: BlockFlags = BlockFlags::BLOCK_IS_GLOBAL.union(BlockFlags::BLOCK_USE_STRET);

#[doc(hidden)]
#[allow(clippy::declare_interior_mutable_const)]
Expand Down

0 comments on commit 581c372

Please sign in to comment.