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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#![doc = include_str!("../../doc/ptr/range.md")]
use core::{
fmt::{
self,
Debug,
Formatter,
},
hash::{
Hash,
Hasher,
},
iter::FusedIterator,
ops::{
Bound,
Range,
RangeBounds,
},
};
use wyz::comu::{
Const,
Mutability,
};
use super::{
BitPtr,
BitSpan,
};
use crate::{
devel as dvl,
order::{
BitOrder,
Lsb0,
},
store::BitStore,
};
#[repr(C)]
#[doc = include_str!("../../doc/ptr/BitPtrRange.md")]
pub struct BitPtrRange<M = Const, T = usize, O = Lsb0>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
/// The lower, inclusive, bound of the range. The bit to which this points
/// is considered live.
pub start: BitPtr<M, T, O>,
/// The higher, exclusive, bound of the range. The bit to which this points
/// is considered dead, and the pointer may be one bit beyond the bounds of
/// an allocation region.
///
/// Because Rust and LLVM both define the address of `base + (len * width)`
/// as being within the provenance of `base`, even though that address may
/// itself be the base address of another region in a different provenance,
/// and bit-pointers are always composed of an ordinary memory address and a
/// bit-counter, the ending bit-pointer is always valid.
pub end: BitPtr<M, T, O>,
}
impl<M, T, O> BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
/// The canonical empty range. All ranges with zero length (equal `.start`
/// and `.end`) are equally empty.
pub const EMPTY: Self = Self {
start: BitPtr::DANGLING,
end: BitPtr::DANGLING,
};
/// Explicitly converts a `Range<BitPtr>` into a `BitPtrRange`.
#[inline]
pub fn from_range(Range { start, end }: Range<BitPtr<M, T, O>>) -> Self {
Self { start, end }
}
/// Explicitly converts a `BitPtrRange` into a `Range<BitPtr>`.
#[inline]
pub fn into_range(self) -> Range<BitPtr<M, T, O>> {
let Self { start, end } = self;
start .. end
}
/// Tests if the range is empty (the distance between bit-pointers is `0`).
///
/// ## Original
///
/// [`Range::is_empty`](core::ops::Range::is_empty)
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
/// use bitvec::ptr::BitPtrRange;
///
/// let data = 0u8;
/// let bp = BitPtr::<_, _, Lsb0>::from_ref(&data);
/// let mut range = BitPtrRange::from_range(bp .. bp.wrapping_add(1));
///
/// assert!(!range.is_empty());
/// assert_ne!(range.start, range.end);
///
/// range.next();
///
/// assert!(range.is_empty());
/// assert_eq!(range.start, range.end);
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.start == self.end
}
/// Tests if a given bit-pointer is contained within the range.
///
/// Bit-pointer ordering is defined when the types have the same exact
/// `BitOrder` type parameter and the same `BitStore::Mem` associated type
/// (but are free to differ in alias condition!). Inclusion in a range
/// occurs when the bit-pointer is not strictly less than the range start,
/// and is strictly less than the range end.
///
/// ## Original
///
/// [`Range::contains`](core::ops::Range::contains)
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
/// use bitvec::ptr::BitPtrRange;
/// use core::cell::Cell;
///
/// let data = 0u16;
/// let bp = BitPtr::<_, _, Lsb0>::from_ref(&data);
///
/// let mut range = BitPtrRange::from_range(bp .. bp.wrapping_add(16));
/// range.nth(2);
/// range.nth_back(2);
///
/// assert!(bp < range.start);
/// assert!(!range.contains(&bp));
///
/// let mid = bp.wrapping_add(8);
///
/// let same_mem = mid.cast::<Cell<u16>>();
/// assert!(range.contains(&mid));
/// ```
///
/// Casting to a different `BitStore` type whose `Mem` parameter differs
/// from the range always results in a `false` response, even if the pointer
/// being tested is numerically within the range.
#[inline]
pub fn contains<M2, T2>(&self, pointer: &BitPtr<M2, T2, O>) -> bool
where
M2: Mutability,
T2: BitStore,
{
dvl::match_store::<T::Mem, T2::Mem>()
&& self.start <= *pointer
&& *pointer < self.end
}
/// Converts the range into a span descriptor over all live bits.
///
/// The produced bit-span does *not* include the bit addressed by `.end`.
///
/// ## Safety
///
/// The `.start` and `.end` bit-pointers must both be derived from the same
/// provenance region. `BitSpan` draws its provenance from the `.start`
/// element pointer, and incorrectly extending it beyond the source
/// provenance is undefined behavior.
pub(crate) unsafe fn into_bitspan(self) -> BitSpan<M, T, O> {
self.start.span_unchecked(self.len())
}
/// Snapshots `.start`, then increments it.
///
/// This method is only safe to call when the range is non-empty.
#[inline]
fn take_front(&mut self) -> BitPtr<M, T, O> {
let start = self.start;
self.start = start.wrapping_add(1);
start
}
/// Decrements `.end`, then returns it.
///
/// The bit-pointer returned by this method is always to an alive bit.
///
/// This method is only safe to call when the range is non-empty.
#[inline]
fn take_back(&mut self) -> BitPtr<M, T, O> {
let prev = self.end.wrapping_sub(1);
self.end = prev;
prev
}
}
#[cfg(not(tarpaulin_include))]
impl<M, T, O> Clone for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn clone(&self) -> Self {
Self { ..*self }
}
}
impl<M, T, O> Eq for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
}
impl<M1, M2, O, T1, T2> PartialEq<BitPtrRange<M2, T2, O>>
for BitPtrRange<M1, T1, O>
where
M1: Mutability,
M2: Mutability,
O: BitOrder,
T1: BitStore,
T2: BitStore,
{
#[inline]
fn eq(&self, other: &BitPtrRange<M2, T2, O>) -> bool {
// Pointers over different element types are never equal
dvl::match_store::<T1::Mem, T2::Mem>()
&& self.start == other.start
&& self.end == other.end
}
}
#[cfg(not(tarpaulin_include))]
impl<M, T, O> Default for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn default() -> Self {
Self::EMPTY
}
}
#[cfg(not(tarpaulin_include))]
impl<M, T, O> From<Range<BitPtr<M, T, O>>> for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn from(range: Range<BitPtr<M, T, O>>) -> Self {
Self::from_range(range)
}
}
#[cfg(not(tarpaulin_include))]
impl<M, T, O> From<BitPtrRange<M, T, O>> for Range<BitPtr<M, T, O>>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn from(range: BitPtrRange<M, T, O>) -> Self {
range.into_range()
}
}
#[cfg(not(tarpaulin_include))]
impl<M, T, O> Debug for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
let Range { start, end } = self.clone().into_range();
Debug::fmt(&start, fmt)?;
write!(fmt, "{0}..{0}", if fmt.alternate() { " " } else { "" })?;
Debug::fmt(&end, fmt)
}
}
#[cfg(not(tarpaulin_include))]
impl<M, T, O> Hash for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn hash<H>(&self, state: &mut H)
where H: Hasher {
self.start.hash(state);
self.end.hash(state);
}
}
impl<M, T, O> Iterator for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
type Item = BitPtr<M, T, O>;
easy_iter!();
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if Self::is_empty(&*self) {
return None;
}
Some(self.take_front())
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
if n >= self.len() {
self.start = self.end;
return None;
}
self.start = unsafe { self.start.add(n) };
Some(self.take_front())
}
}
impl<M, T, O> DoubleEndedIterator for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
if Self::is_empty(&*self) {
return None;
}
Some(self.take_back())
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
if n >= self.len() {
self.end = self.start;
return None;
}
let out = unsafe { self.end.sub(n.wrapping_add(1)) };
self.end = out;
Some(out)
}
}
impl<M, T, O> ExactSizeIterator for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn len(&self) -> usize {
(unsafe { self.end.offset_from(self.start) }) as usize
}
}
impl<M, T, O> FusedIterator for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
}
#[cfg(not(tarpaulin_include))]
impl<M, T, O> RangeBounds<BitPtr<M, T, O>> for BitPtrRange<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
#[inline]
fn start_bound(&self) -> Bound<&BitPtr<M, T, O>> {
Bound::Included(&self.start)
}
#[inline]
fn end_bound(&self) -> Bound<&BitPtr<M, T, O>> {
Bound::Excluded(&self.end)
}
}