1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
/*! Memory modeling.
This module provides a `BitStore` trait, which mediates how handles access
memory and perform analysis on the regions they describe.
!*/
use crate::{
access::BitAccess,
mem::{
self,
BitMemory,
},
};
use core::{
cell::Cell,
fmt::Debug,
};
use radium::{
marker::BitOps,
Radium,
};
#[cfg(feature = "atomic")]
use core::sync::atomic;
/** Common interface for memory regions.
This trait is implemented on the fundamental integers no wider than the target
processor word size, their `Cell` wrappers, and (if present) their `Atomic`
variants. Users provide this type as a parameter to their data structures in
order to inform the structure of how it may access the memory it describes.
Currently, `bitvec` is only tested on 32- and 64- bit architectures. This means
that `u8`, `u16`, `u32`, and `usize` unconditionally implement `BitStore`, but
`u64` will only do so on 64-bit targets, and will be unavailable on 32-bit
targets. This is a necessary restriction of `bitvec` internals. Please comment
on [Issue #76](https://github.com/myrrlyn/bitvec/issues/76) if this affects you.
Specifically, this has the davantage that a `BitSlice<_, Cell<_>>` knows that it
has a view of memory that will not undergo concurrent modification. As such, it
can forego atomic accesses, and just use ordinary load/store instructions
without fear of causing observable race conditions.
The associated types `Mem` and `Alias` allow implementors to know the register
width of the memory they describe (`Mem`) and to know the aliasing status of the
region.
# Generic Programming
Generic programming with associated types is *hard*, especially when using them,
as in this trait, to implement a closed graph of relationships between types.
For example, this trait is implemented such that for any given type `T`,
`T::Alias::Mem` == `T::Mem` == `T::NoAlias::Mem`, `T::Alias::Alias == T::Alias`,
and `T::NoAlias::NoAlias == T::NoAlias`. Unfortunately, the Rust type system
does not allow these relationships to be described, so generic programming that
performs type transitions will *rapidly* become uncomfortable to use.
Internally, `bitvec` makes use of type-manipulation functions that are known to
be correct with respect to the implementations of `BitStore` in order to ease
implementation of library methods.
You are not expected to do significant programming that is generic over the
`BitStore` memory parameter. When using a concrete type, the compiler will
gladly reduce the abstract type associations into their instantiated selections,
allowing monomorphized code to be *much* more convenient than generic.
If you have a use case that involves generic programming over this trait, and
you are encountering difficulties dealing with the type associations, please
file an issue asking for support in this area.
# Supertraits
This trait has trait requirements that better express its behavior:
- `Sealed` prevents it from being implemented by downstream libraries (`Sealed`
is a public trait in a private module, that only this crate can name).
- `Sized` instructs the compiler that values of this type can be used as
immediates.
- `Debug` informs the compiler that other structures using this trait bound can
correctly derive `Debug`.
**/
pub trait BitStore: seal::Sealed + Sized + Debug {
/// The register type that the implementor describes.
type Mem: BitMemory + BitOps + BitStore + Into<Self>;
/// The modifier type over `Self::Mem` used to perform memory access.
type Access: BitAccess<Self::Mem>;
/// A sibling `BitStore` implementor that performs alias-aware memory
/// access.
///
/// While the associated type always has the same `Mem` concrete type as
/// `Self`, attempting to encode this requirement as `<Mem = Self::Mem>
/// causes Rust to enter an infinite recursion in the trait solver.
///
/// Instead, the two `Radium` bounds inform the compiler that the `Alias` is
/// irradiant over both the current memory and the destination memory types,
/// allowing generic type algebra to resolve correctly even though the fact
/// that `Radium` is only implemented once is not guaranteed.
type Alias: BitStore
+ Radium<Self::Mem>
+ Radium<<Self::Alias as BitStore>::Mem>;
/// Marker for the thread safety of the implementor.
///
/// This is necessary because `Cell<T: Send>` is `Send`, but `Cell` does not
/// use synchronization instructions and thus cannot be used for aliased
/// parallelized memory manipulation.
#[doc(hidden)]
type Threadsafe;
/// Require that all implementors are aligned to their width.
#[doc(hidden)]
const __ALIGNED_TO_SIZE: [(); 0];
/// Require that the `::Alias` associated type has the same width and
/// alignment as `Self`.
#[doc(hidden)]
const __ALIAS_WIDTH: [(); 0];
}
/// Batch implementation of `BitStore` for appropriate types.
macro_rules! bitstore {
($($t:ty => $a:ty),+ $(,)?) => { $(
impl BitStore for $t {
/// The unsigned integers will only be `BitStore` type parameters
/// for handles to unaliased memory, following the normal Rust
/// reference rules.
type Access = Cell<Self>;
/// In atomic builds, use atomic types for aliased access.
#[cfg(feature = "atomic")]
type Alias = $a;
/// In non-atomic builds, use cell wrappers for aliased access.
#[cfg(not(feature = "atomic"))]
type Alias = Cell<Self>;
type Mem = Self;
#[doc(hidden)]
type Threadsafe = Self;
#[doc(hidden)]
const __ALIGNED_TO_SIZE: [(); 0] = [(); mem::aligned_to_size::<Self>()];
#[doc(hidden)]
const __ALIAS_WIDTH: [(); 0] = [(); mem::cmp_layout::<Self::Mem, Self::Alias>()];
}
#[cfg(feature = "atomic")]
impl BitStore for $a {
type Access = Self;
type Alias = Self;
type Mem = $t;
#[doc(hidden)]
type Threadsafe = Self;
#[doc(hidden)]
const __ALIGNED_TO_SIZE: [(); 0] = [(); mem::aligned_to_size::<Self>()];
#[doc(hidden)]
const __ALIAS_WIDTH: [(); 0] = [(); mem::cmp_layout::<Self::Mem, Self::Alias>()];
}
impl seal::Sealed for $t {}
#[cfg(feature = "atomic")]
impl seal::Sealed for $a {}
)+ };
}
bitstore!(
u8 => atomic::AtomicU8,
u16 => atomic::AtomicU16,
u32 => atomic::AtomicU32,
usize => atomic::AtomicUsize,
);
#[cfg(target_pointer_width = "64")]
bitstore!(u64 => atomic::AtomicU64);
impl<M> BitStore for Cell<M>
where
Self: Radium<M>,
M: BitMemory + BitOps + BitStore,
{
type Access = Self;
type Alias = Self;
type Mem = M;
/// Raw pointers are never threadsafe, so this prevents handles using
/// `Cell<_>` type parameters from crossing thread boundaries.
#[doc(hidden)]
type Threadsafe = *const Self;
// If these are true for `M: BitStore`, then they are true for `Cell<M>`.
#[doc(hidden)]
const __ALIAS_WIDTH: [(); 0] = [];
#[doc(hidden)]
const __ALIGNED_TO_SIZE: [(); 0] = [];
}
impl<M> seal::Sealed for Cell<M> where M: BitMemory + BitStore
{
}
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
compile_fail!(concat!(
"This architecture is currently not supported. File an issue at ",
env!("CARGO_PKG_REPOSITORY")
));
/// Enclose the `Sealed` trait against client use.
mod seal {
/// Marker trait to seal `BitStore` against downstream implementation.
///
/// This trait is public in the module, so that other modules in the crate
/// can use it, but so long as it is not exported by the crate root and this
/// module is private, this trait effectively forbids downstream
/// implementation of the `BitStore` trait.
#[doc(hidden)]
pub trait Sealed {}
}
#[cfg(test)]
#[cfg(not(tarpaulin_include))]
mod tests {
use crate::prelude::*;
use core::cell::Cell;
use static_assertions::*;
#[test]
fn traits() {
// The integers are threadsafe, as they are known to be unaliased.
assert_impl_all!(BitSlice<LocalBits, u8>: Send, Sync);
assert_impl_all!(BitSlice<LocalBits, u16>: Send, Sync);
assert_impl_all!(BitSlice<LocalBits, u32>: Send, Sync);
assert_impl_all!(BitSlice<LocalBits, usize>: Send, Sync);
#[cfg(target_pointer_width = "64")]
assert_impl_all!(BitSlice<LocalBits, u64>: Send, Sync);
// The integer alias is threadsafe when atomics are enabled.
#[cfg(feature = "atomic")]
{
assert_impl_all!(BitSlice<LocalBits, <u8 as BitStore>::Alias>: Send, Sync);
assert_impl_all!(BitSlice<LocalBits, <u16 as BitStore>::Alias>: Send, Sync);
assert_impl_all!(BitSlice<LocalBits, <u32 as BitStore>::Alias>: Send, Sync);
assert_impl_all!(BitSlice<LocalBits, <usize as BitStore>::Alias>: Send, Sync);
#[cfg(target_pointer_width = "64")]
assert_impl_all!(BitSlice<LocalBits, <u64 as BitStore>::Alias>: Send, Sync);
}
// The integer alias is thread unsafe when atomics are disabled.
#[cfg(not(feature = "atomic"))]
{
assert_not_impl_any!(BitSlice<LocalBits, <u8 as BitStore>::Alias>: Send, Sync);
assert_not_impl_any!(BitSlice<LocalBits, <u16 as BitStore>::Alias>: Send, Sync);
assert_not_impl_any!(BitSlice<LocalBits, <u32 as BitStore>::Alias>: Send, Sync);
assert_not_impl_any!(BitSlice<LocalBits, <usize as BitStore>::Alias>: Send, Sync);
#[cfg(target_pointer_width = "64")]
assert_not_impl_any!(BitSlice<LocalBits, <u64 as BitStore>::Alias>: Send, Sync);
}
// `Cell`s are never threadsafe.
assert_not_impl_any!(BitSlice<LocalBits, Cell<u8>>: Send, Sync);
assert_not_impl_any!(BitSlice<LocalBits, Cell<u16>>: Send, Sync);
assert_not_impl_any!(BitSlice<LocalBits, Cell<u32>>: Send, Sync);
assert_not_impl_any!(BitSlice<LocalBits, Cell<usize>>: Send, Sync);
#[cfg(target_pointer_width = "64")]
assert_not_impl_any!(BitSlice<LocalBits, Cell<u64>>: Send, Sync);
}
}