use core::ffi::c_ulong; use core::fmt; use core::marker::PhantomData; use core::mem; use core::mem::MaybeUninit; use core::ops::Deref; use core::ptr::{self, NonNull};
// TODO: Should this be a static to help the compiler deduplicating them? const GLOBAL_DESCRIPTOR: BlockDescriptor = BlockDescriptor {
reserved: 0,
size: mem::size_of::<BlockHeader>() as c_ulong,
};
/// A global Objective-C block that does not capture an environment. /// /// This can be used as an optimization of [`RcBlock`] if your closure doesn't /// capture any variables. /// /// This is a smart pointer that [`Deref`]s to [`Block`]. /// /// It can created and stored in static memory using the [`global_block!`] /// macro. /// /// [`RcBlock`]: crate::RcBlock /// [`global_block!`]: crate::global_block #[repr(C)] pubstruct GlobalBlock<F: ?Sized> {
header: BlockHeader, // We don't store a function pointer, instead it is placed inside the // invoke function.
f: PhantomData<F>,
}
// TODO: Add `Send + Sync` bounds once the block itself supports that. unsafeimpl<F: ?Sized + BlockFn> Sync for GlobalBlock<F> {} unsafeimpl<F: ?Sized + BlockFn> Send for GlobalBlock<F> {}
// Note: We can't put correct bounds on A and R because we have a const fn, // and that's not allowed yet in our MSRV. // // Fortunately, we don't need them, since they're present on `Sync`, so // constructing the static in `global_block!` with an invalid `GlobalBlock` // triggers an error. impl<F: ?Sized> GlobalBlock<F> { // TODO: Use new ABI with BLOCK_HAS_SIGNATURE const FLAGS: BlockFlags = BlockFlags::BLOCK_IS_GLOBAL.union(BlockFlags::BLOCK_USE_STRET);
// TODO: Add some constructor for when `F: Copy`.
}
impl<F: ?Sized + BlockFn> Deref for GlobalBlock<F> { type Target = Block<F>;
#[inline] fn deref(&self) -> &Self::Target { let ptr: NonNull<Self> = NonNull::from(self); let ptr: NonNull<Block<F>> = ptr.cast(); // SAFETY: This has the same layout as `Block` // // A global block does not hold any data, so it is safe to call // immutably. unsafe { ptr.as_ref() }
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.