-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathsingle.rs
More file actions
1446 lines (1375 loc) · 40.2 KB
/
single.rs
File metadata and controls
1446 lines (1375 loc) · 40.2 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![doc = include_str!("../../doc/ptr/single.md")]
use core::{
any,
cmp,
convert::TryFrom,
fmt::{
self,
Debug,
Display,
Formatter,
Pointer,
},
hash::{
Hash,
Hasher,
},
marker::PhantomData,
ptr,
};
use tap::{
Pipe,
TryConv,
};
use wyz::{
comu::{
Address,
Const,
Frozen,
Mut,
Mutability,
NullPtrError,
},
fmt::FmtForward,
};
use super::{
check_alignment,
AddressExt,
BitPtrRange,
BitRef,
BitSpan,
BitSpanError,
MisalignError,
};
use crate::{
access::BitAccess,
devel as dvl,
index::BitIdx,
mem,
order::{
BitOrder,
Lsb0,
},
store::BitStore,
};
#[repr(C, packed)]
#[doc = include_str!("../../doc/ptr/BitPtr.md")]
pub struct BitPtr<M = Const, T = usize, O = Lsb0>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
/// Memory addresses must be well-aligned and non-null.
///
/// This is not actually a requirement of `BitPtr`, but it is a requirement
/// of `BitSpan`, and it is extended across the entire crate for
/// consistency.
ptr: Address<M, T>,
/// The index of the referent bit within `*addr`.
bit: BitIdx<T::Mem>,
/// The ordering used to select the bit at `head` in `*addr`.
_or: PhantomData<O>,
}
impl<M, T, O> BitPtr<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
/// The canonical dangling pointer. This selects the starting bit of the
/// canonical dangling pointer for `T`.
pub const DANGLING: Self = Self {
ptr: Address::DANGLING,
bit: BitIdx::MIN,
_or: PhantomData,
};
/// Loads the address field, sidestepping any alignment problems.
///
/// This is the only safe way to access `(&self).ptr`. Do not perform field
/// access on `.ptr` through a reference except through this method.
#[inline]
fn get_addr(&self) -> Address<M, T> {
unsafe { ptr::addr_of!(self.ptr).read_unaligned() }
}
/// Tries to construct a `BitPtr` from a memory location and a bit index.
///
/// ## Parameters
///
/// - `ptr`: The address of a memory element. `Address` wraps raw pointers
/// or references, and enforces that they are not null. `BitPtr`
/// additionally requires that the address be well-aligned to its type;
/// misaligned addresses cause this to return an error.
/// - `bit`: The index of the selected bit within `*ptr`.
///
/// ## Returns
///
/// This returns an error if `ptr` is not aligned to `T`; otherwise, it
/// returns a new bit-pointer structure to the given element and bit.
///
/// You should typically prefer to use constructors that take directly from
/// a memory reference or pointer, such as the `TryFrom<*T>`
/// implementations, the `From<&/mut T>` implementations, or the
/// [`::from_ref()`], [`::from_mut()`], [`::from_slice()`], or
/// [`::from_slice_mut()`] functions.
///
/// [`::from_mut()`]: Self::from_mut
/// [`::from_ref()`]: Self::from_ref
/// [`::from_slice()`]: Self::from_slice
/// [`::from_slice_mut()`]: Self::from_slice_mut
#[inline]
pub fn new(
ptr: Address<M, T>,
bit: BitIdx<T::Mem>,
) -> Result<Self, MisalignError<T>> {
Ok(Self {
ptr: check_alignment(ptr)?,
bit,
..Self::DANGLING
})
}
/// Constructs a `BitPtr` from an address and head index, without checking
/// the address for validity.
///
/// ## Parameters
///
/// - `addr`: The memory address to use in the bit-pointer. See the Safety
/// section.
/// - `head`: The index of the bit in `*addr` that this bit-pointer selects.
///
/// ## Returns
///
/// A new bit-pointer composed of the parameters. No validity checking is
/// performed.
///
/// ## Safety
///
/// The `Address` type imposes a non-null requirement. `BitPtr` additionally
/// requires that `addr` is well-aligned for `T`, and presumes that the
/// caller has ensured this with [`bv_ptr::check_alignment`][0]. If this is
/// not the case, then the program is incorrect, and subsequent behavior is
/// not specified.
///
/// [0]: crate::ptr::check_alignment.
#[inline]
pub unsafe fn new_unchecked(
ptr: Address<M, T>,
bit: BitIdx<T::Mem>,
) -> Self {
if cfg!(debug_assertions) {
Self::new(ptr, bit).unwrap()
}
else {
Self {
ptr,
bit,
..Self::DANGLING
}
}
}
/// Gets the address of the base storage element.
#[inline]
pub fn address(self) -> Address<M, T> {
self.get_addr()
}
/// Gets the `BitIdx` that selects the bit within the memory element.
#[inline]
pub fn bit(self) -> BitIdx<T::Mem> {
self.bit
}
/// Decomposes a bit-pointer into its element address and bit index.
///
/// ## Parameters
///
/// - `self`
///
/// ## Returns
///
/// - `.0`: The memory address in which the referent bit is located.
/// - `.1`: The index of the referent bit in `*.0` according to the `O` type
/// parameter.
#[inline]
pub fn raw_parts(self) -> (Address<M, T>, BitIdx<T::Mem>) {
(self.address(), self.bit())
}
/// Converts a bit-pointer into a span descriptor by attaching a length
/// counter (in bits).
///
/// ## Parameters
///
/// - `self`: The base address of the produced span.
/// - `bits`: The length, in bits, of the span.
///
/// ## Returns
///
/// A span descriptor beginning at `self` and ending (exclusive) at `self +
/// bits`. This fails if it is unable to encode the requested span into a
/// descriptor.
pub(crate) fn span(
self,
bits: usize,
) -> Result<BitSpan<M, T, O>, BitSpanError<T>> {
BitSpan::new(self.ptr, self.bit, bits)
}
/// Converts a bit-pointer into a span descriptor, without performing
/// encoding validity checks.
///
/// ## Parameters
///
/// - `self`: The base address of the produced span.
/// - `bits`: The length, in bits, of the span.
///
/// ## Returns
///
/// An encoded span descriptor of `self` and `bits`. Note that no validity
/// checks are performed!
///
/// ## Safety
///
/// The caller must ensure that the rules of `BitSpan::new` are not
/// violated. Typically this method should only be used on parameters that
/// have already passed through `BitSpan::new` and are known to be good.
pub(crate) unsafe fn span_unchecked(self, bits: usize) -> BitSpan<M, T, O> {
BitSpan::new_unchecked(self.get_addr(), self.bit, bits)
}
/// Produces a bit-pointer range beginning at `self` (inclusive) and ending
/// at `self + count` (exclusive).
///
/// ## Safety
///
/// `self + count` must be within the same provenance region as `self`. The
/// first bit past the end of an allocation is included in provenance
/// regions, though it is not dereferenceable and will not be dereferenced.
///
/// It is unsound to *even construct* a pointer that departs the provenance
/// region, even if that pointer is never dereferenced!
pub(crate) unsafe fn range(self, count: usize) -> BitPtrRange<M, T, O> {
(self .. self.add(count)).into()
}
/// Removes write permissions from a bit-pointer.
#[inline]
pub fn to_const(self) -> BitPtr<Const, T, O> {
let Self {
ptr: addr,
bit: head,
..
} = self;
BitPtr {
ptr: addr.immut(),
bit: head,
..BitPtr::DANGLING
}
}
/// Adds write permissions to a bit-pointer.
///
/// ## Safety
///
/// This pointer must have been derived from a `*mut` pointer.
#[inline]
pub unsafe fn to_mut(self) -> BitPtr<Mut, T, O> {
let Self {
ptr: addr,
bit: head,
..
} = self;
BitPtr {
ptr: addr.assert_mut(),
bit: head,
..BitPtr::DANGLING
}
}
/// Freezes a bit-pointer, forbidding direct mutation.
///
/// This is used as a necessary prerequisite to all mutation of memory.
/// `BitPtr` uses an implementation scoped to `Frozen<_>` to perform
/// alias-aware writes; see below.
pub(crate) fn freeze(self) -> BitPtr<Frozen<M>, T, O> {
let Self {
ptr: addr,
bit: head,
..
} = self;
BitPtr {
ptr: addr.freeze(),
bit: head,
..BitPtr::DANGLING
}
}
}
impl<T, O> BitPtr<Const, T, O>
where
T: BitStore,
O: BitOrder,
{
/// Constructs a `BitPtr` to the zeroth bit in a single element.
#[inline]
pub fn from_ref(elem: &T) -> Self {
unsafe { Self::new_unchecked(elem.into(), BitIdx::MIN) }
}
/// Constructs a `BitPtr` to the zeroth bit in the zeroth element of a
/// slice.
///
/// This method is distinct from `Self::from_ref(&elem[0])`, because it
/// ensures that the returned bit-pointer has provenance over the entire
/// slice. Indexing within a slice narrows the provenance range, and makes
/// departure from the subslice, *even within the original slice*, illegal.
#[inline]
pub fn from_slice(slice: &[T]) -> Self {
unsafe {
Self::new_unchecked(slice.as_ptr().into_address(), BitIdx::MIN)
}
}
/// Gets a raw pointer to the memory element containing the selected bit.
#[inline]
#[cfg(not(tarpaulin_include))]
pub fn pointer(&self) -> *const T {
self.get_addr().to_const()
}
}
impl<T, O> BitPtr<Mut, T, O>
where
T: BitStore,
O: BitOrder,
{
/// Constructs a mutable `BitPtr` to the zeroth bit in a single element.
#[inline]
pub fn from_mut(elem: &mut T) -> Self {
unsafe { Self::new_unchecked(elem.into(), BitIdx::MIN) }
}
/// Constructs a `BitPtr` to the zeroth bit in the zeroth element of a
/// mutable slice.
///
/// This method is distinct from `Self::from_mut(&mut elem[0])`, because it
/// ensures that the returned bit-pointer has provenance over the entire
/// slice. Indexing within a slice narrows the provenance range, and makes
/// departure from the subslice, *even within the original slice*, illegal.
#[inline]
pub fn from_mut_slice(slice: &mut [T]) -> Self {
unsafe {
Self::new_unchecked(slice.as_mut_ptr().into_address(), BitIdx::MIN)
}
}
/// Constructs a mutable `BitPtr` to the zeroth bit in the zeroth element of
/// a slice.
///
/// This method is distinct from `Self::from_mut(&mut elem[0])`, because it
/// ensures that the returned bit-pointer has provenance over the entire
/// slice. Indexing within a slice narrows the provenance range, and makes
/// departure from the subslice, *even within the original slice*, illegal.
#[inline]
pub fn from_slice_mut(slice: &mut [T]) -> Self {
unsafe {
Self::new_unchecked(slice.as_mut_ptr().into_address(), BitIdx::MIN)
}
}
/// Gets a raw pointer to the memory location containing the selected bit.
#[inline]
#[cfg(not(tarpaulin_include))]
pub fn pointer(&self) -> *mut T {
self.get_addr().to_mut()
}
}
/// Port of the `*bool` inherent API.
impl<M, T, O> BitPtr<M, T, O>
where
M: Mutability,
T: BitStore,
O: BitOrder,
{
/// Tests if a bit-pointer is the null value.
///
/// This is always false, as a `BitPtr` is a `NonNull` internally. Use
/// `Option<BitPtr>` to express the potential for a null pointer.
///
/// ## Original
///
/// [`pointer::is_null`](https://doc.rust-lang.org/std/primitive.pointer.html#method.is_null)
#[inline]
#[deprecated = "`BitPtr` is never null"]
pub fn is_null(self) -> bool {
false
}
/// Casts to a `BitPtr` with a different storage parameter.
///
/// This is not free! In order to maintain value integrity, it encodes a
/// `BitSpan` encoded descriptor with its value, casts that, then decodes
/// into a `BitPtr` of the target type. If `T` and `U` have different
/// `::Mem` associated types, then this may change the selected bit in
/// memory. This is an unavoidable cost of the addressing and encoding
/// schemes.
///
/// ## Original
///
/// [`pointer::cast`](https://doc.rust-lang.org/std/primitive.pointer.html#method.cast)
#[inline]
pub fn cast<U>(self) -> BitPtr<M, U, O>
where U: BitStore {
let (addr, head, _) =
unsafe { self.span_unchecked(1) }.cast::<U>().raw_parts();
unsafe { BitPtr::new_unchecked(addr, head) }
}
/// Decomposes a bit-pointer into its address and head-index components.
///
/// ## Original
///
/// [`pointer::to_raw_parts`](https://doc.rust-lang.org/std/primitive.pointer.html#method.to_raw_parts)
///
/// ## API Differences
///
/// The original method is unstable as of 1.54.0; however, because `BitPtr`
/// already has a similar API, the name is optimistically stabilized here.
/// Prefer [`.raw_parts()`] until the original inherent stabilizes.
///
/// [`.raw_parts()`]: Self::raw_parts
#[inline]
#[cfg(not(tarpaulin_include))]
pub fn to_raw_parts(self) -> (Address<M, T>, BitIdx<T::Mem>) {
self.raw_parts()
}
/// Produces a proxy reference to the referent bit.
///
/// Because `BitPtr` guarantees that it is non-null and well-aligned, this
/// never returns `None`. However, this is still unsafe to call on any
/// bit-pointers created from conjured values rather than known references.
///
/// ## Original
///
/// [`pointer::as_ref`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref)
///
/// ## API Differences
///
/// This produces a proxy type rather than a true reference. The proxy
/// implements `Deref<Target = bool>`, and can be converted to `&bool` with
/// a reborrow `&*`.
///
/// ## Safety
///
/// Since `BitPtr` does not permit null or misaligned pointers, this method
/// will always dereference the pointer in order to create the proxy. As
/// such, you must ensure the following conditions are met:
///
/// - the pointer must be dereferenceable as defined in the standard library
/// documentation
/// - the pointer must point to an initialized instance of `T`
/// - you must ensure that no other pointer will race to modify the referent
/// location while this call is reading from memory to produce the proxy
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
///
/// let data = 1u8;
/// let ptr = BitPtr::<_, _, Lsb0>::from_ref(&data);
/// let val = unsafe { ptr.as_ref() }.unwrap();
/// assert!(*val);
/// ```
#[inline]
pub unsafe fn as_ref<'a>(self) -> Option<BitRef<'a, Const, T, O>> {
Some(BitRef::from_bitptr(self.to_const()))
}
/// Creates a new bit-pointer at a specified offset from the original.
///
/// `count` is in units of bits.
///
/// ## Original
///
/// [`pointer::offset`](https://doc.rust-lang.org/std/primitive.pointer.html#method.offset)
///
/// ## Safety
///
/// `BitPtr` is implemented with Rust raw pointers internally, and is
/// subject to all of Rust’s rules about provenance and permission tracking.
/// You must abide by the safety rules established in the original method,
/// to which this internally delegates.
///
/// Additionally, `bitvec` imposes its own rules: while Rust cannot observe
/// provenance beyond an element or byte level, `bitvec` demands that
/// `&mut BitSlice` have exclusive view over all bits it observes. You must
/// not produce a bit-pointer that departs a `BitSlice` region and intrudes
/// on any `&mut BitSlice`’s handle, and you must not produce a
/// write-capable bit-pointer that intrudes on a `&BitSlice` handle that
/// expects its contents to be immutable.
///
/// Note that it is illegal to *construct* a bit-pointer that invalidates
/// any of these rules. If you wish to defer safety-checking to the point of
/// dereferencing, and allow the temporary construction *but not*
/// *dereference* of illegal `BitPtr`s, use [`.wrapping_offset()`] instead.
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
///
/// let data = 5u8;
/// let ptr = BitPtr::<_, _, Lsb0>::from_ref(&data);
/// unsafe {
/// assert!(ptr.read());
/// assert!(!ptr.offset(1).read());
/// assert!(ptr.offset(2).read());
/// }
/// ```
///
/// [`.wrapping_offset()`]: Self::wrapping_offset
#[inline]
#[must_use = "returns a new bit-pointer rather than modifying its argument"]
pub unsafe fn offset(self, count: isize) -> Self {
let (elts, head) = self.bit.offset(count);
Self::new_unchecked(self.ptr.offset(elts), head)
}
/// Creates a new bit-pointer at a specified offset from the original.
///
/// `count` is in units of bits.
///
/// ## Original
///
/// [`pointer::wrapping_offset`](https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_offset)
///
/// ## API Differences
///
/// `bitvec` makes it explicitly illegal to wrap a pointer around the high
/// end of the address space, because it is incapable of representing a null
/// pointer.
///
/// However, `<*T>::wrapping_offset` has additional properties as a result
/// of its tolerance for wrapping the address space: it tolerates departing
/// a provenance region, and is not unsafe to use to *create* a bit-pointer
/// that is outside the bounds of its original provenance.
///
/// ## Safety
///
/// This function is safe to use because the bit-pointers it creates defer
/// their provenance checks until the point of dereference. As such, you
/// can safely use this to perform arbitrary pointer arithmetic that Rust
/// considers illegal in ordinary arithmetic, as long as you do not
/// dereference the bit-pointer until it has been brought in bounds of the
/// originating provenance region.
///
/// This means that, to the Rust rule engine,
/// `let z = x.wrapping_add(y as usize).wrapping_sub(x as usize);` is not
/// equivalent to `y`, but `z` is safe to construct, and
/// `z.wrapping_add(x as usize).wrapping_sub(y as usize)` produces a
/// bit-pointer that *is* equivalent to `x`.
///
/// See the documentation of the original method for more details about
/// provenance regions, and the distinctions that the optimizer makes about
/// them.
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
///
/// let data = 0u32;
/// let mut ptr = BitPtr::<_, _, Lsb0>::from_ref(&data);
/// let end = ptr.wrapping_offset(32);
/// while ptr < end {
/// # #[cfg(feature = "std")] {
/// println!("{}", unsafe { ptr.read() });
/// # }
/// ptr = ptr.wrapping_offset(3);
/// }
/// ```
#[inline]
#[must_use = "returns a new bit-pointer rather than modifying its argument"]
pub fn wrapping_offset(self, count: isize) -> Self {
let (elts, head) = self.bit.offset(count);
unsafe { Self::new_unchecked(self.ptr.wrapping_offset(elts), head) }
}
/// Calculates the distance (in bits) between two bit-pointers.
///
/// This method is the inverse of [`.offset()`].
///
/// ## Original
///
/// [`pointer::offset_from`](https://doc.rust-lang.org/std/primitive.pointer.html#method.offset_from)
///
/// ## API Differences
///
/// The base pointer may have a different `BitStore` type parameter, as long
/// as they share an underlying memory type. This is necessary in order to
/// accommodate aliasing markers introduced between when an origin pointer
/// was taken and when `self` compared against it.
///
/// ## Safety
///
/// Both `self` and `origin` **must** be drawn from the same provenance
/// region. This means that they must be created from the same Rust
/// allocation, whether with `let` or the allocator API, and must be in the
/// (inclusive) range `base ..= base + len`. The first bit past the end of
/// a region can be addressed, just not dereferenced.
///
/// See the original `<*T>::offset_from` for more details on region safety.
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
///
/// let data = 0u32;
/// let base = BitPtr::<_, _, Lsb0>::from_ref(&data);
/// let low = unsafe { base.add(10) };
/// let high = unsafe { low.add(15) };
/// unsafe {
/// assert_eq!(high.offset_from(low), 15);
/// assert_eq!(low.offset_from(high), -15);
/// assert_eq!(low.offset(15), high);
/// assert_eq!(high.offset(-15), low);
/// }
/// ```
///
/// While this method is safe to *construct* bit-pointers that depart a
/// provenance region, it remains illegal to *dereference* those pointers!
///
/// This usage is incorrect, and a program that contains it is not
/// well-formed.
///
/// ```rust,no_run
/// use bitvec::prelude::*;
///
/// let a = 0u8;
/// let b = !0u8;
///
/// let a_ptr = BitPtr::<_, _, Lsb0>::from_ref(&a);
/// let b_ptr = BitPtr::<_, _, Lsb0>::from_ref(&b);
/// let diff = (b_ptr.pointer() as isize)
/// .wrapping_sub(a_ptr.pointer() as isize)
/// // Remember: raw pointers are byte-stepped,
/// // but bit-pointers are bit-stepped.
/// .wrapping_mul(8);
/// // This pointer to `b` has `a`’s provenance:
/// let b_ptr_2 = a_ptr.wrapping_offset(diff);
///
/// // They are *arithmetically* equal:
/// assert_eq!(b_ptr, b_ptr_2);
/// // But it is still undefined behavior to cross provenances!
/// assert_eq!(0, unsafe { b_ptr_2.offset_from(b_ptr) });
/// ```
///
/// [`.offset()`]: Self::offset
#[inline]
pub unsafe fn offset_from<U>(self, origin: BitPtr<M, U, O>) -> isize
where U: BitStore<Mem = T::Mem> {
self.get_addr()
.cast::<T::Mem>()
.offset_from(origin.get_addr().cast::<T::Mem>())
.wrapping_mul(mem::bits_of::<T::Mem>() as isize)
.wrapping_add(self.bit.into_inner() as isize)
.wrapping_sub(origin.bit.into_inner() as isize)
}
/// Adjusts a bit-pointer upwards in memory. This is equivalent to
/// `.offset(count as isize)`.
///
/// `count` is in units of bits.
///
/// ## Original
///
/// [`pointer::add`](https://doc.rust-lang.org/std/primitive.pointer.html#method.add)
///
/// ## Safety
///
/// See [`.offset()`](Self::offset).
#[inline]
#[must_use = "returns a new bit-pointer rather than modifying its argument"]
pub unsafe fn add(self, count: usize) -> Self {
self.offset(count as isize)
}
/// Adjusts a bit-pointer downwards in memory. This is equivalent to
/// `.offset((count as isize).wrapping_neg())`.
///
/// `count` is in units of bits.
///
/// ## Original
///
/// [`pointer::sub`](https://doc.rust-lang.org/std/primitive.pointer.html#method.sub)
///
/// ## Safety
///
/// See [`.offset()`](Self::offset).
#[inline]
#[must_use = "returns a new bit-pointer rather than modifying its argument"]
pub unsafe fn sub(self, count: usize) -> Self {
self.offset((count as isize).wrapping_neg())
}
/// Adjusts a bit-pointer upwards in memory, using wrapping semantics. This
/// is equivalent to `.wrapping_offset(count as isize)`.
///
/// `count` is in units of bits.
///
/// ## Original
///
/// [`pointer::wrapping_add`](https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_add)
///
/// ## Safety
///
/// See [`.wrapping_offset()`](Self::wrapping_offset).
#[inline]
#[must_use = "returns a new bit-pointer rather than modifying its argument"]
pub fn wrapping_add(self, count: usize) -> Self {
self.wrapping_offset(count as isize)
}
/// Adjusts a bit-pointer downwards in memory, using wrapping semantics.
/// This is equivalent to
/// `.wrapping_offset((count as isize).wrapping_neg())`.
///
/// `count` is in units of bits.
///
/// ## Original
///
/// [`pointer::wrapping_add`](https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_add)
///
/// ## Safety
///
/// See [`.wrapping_offset()`](Self::wrapping_offset).
#[inline]
#[must_use = "returns a new bit-pointer rather than modifying its argument"]
pub fn wrapping_sub(self, count: usize) -> Self {
self.wrapping_offset((count as isize).wrapping_neg())
}
/// Reads the bit from `*self`.
///
/// ## Original
///
/// [`pointer::read`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read)
///
/// ## Safety
///
/// See [`ptr::read`](crate::ptr::read).
#[inline]
pub unsafe fn read(self) -> bool {
(*self.ptr.to_const()).load_value().get_bit::<O>(self.bit)
}
/// Reads the bit from `*self` using a volatile load.
///
/// Prefer using a crate such as [`voladdress`][0] to manage volatile I/O
/// and use `bitvec` only on the local objects it provides. Individual I/O
/// operations for individual bits are likely not the behavior you want.
///
/// ## Original
///
/// [`pointer::read_volatile`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read_volatile)
///
/// ## Safety
///
/// See [`ptr::read_volatile`](crate::ptr::read_volatile).
///
/// [0]: https://docs.rs/voladdress/later/voladdress
#[inline]
pub unsafe fn read_volatile(self) -> bool {
self.ptr.to_const().read_volatile().get_bit::<O>(self.bit)
}
/// Reads the bit from `*self` using an unaligned memory access.
///
/// `BitPtr` forbids unaligned addresses. If you have such an address, you
/// must perform your memory accesses on the raw element, and only use
/// `bitvec` on a well-aligned stack temporary. This method should never be
/// necessary.
///
/// ## Original
///
/// [`pointer::read_unaligned`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read_unaligned)
///
/// ## Safety
///
/// See [`ptr::read_unaligned`](crate::ptr::read_unaligned)
#[inline]
#[deprecated = "`BitPtr` does not have unaligned addresses"]
pub unsafe fn read_unaligned(self) -> bool {
self.ptr.to_const().read_unaligned().get_bit::<O>(self.bit)
}
/// Copies `count` bits from `self` to `dest`. The source and destination
/// may overlap.
///
/// Note that overlap is only defined when `O` and `O2` are the same type.
/// If they differ, then `bitvec` does not define overlap, and assumes that
/// they are wholly discrete in memory.
///
/// ## Original
///
/// [`pointer::copy_to`](https://doc.rust-lang.org/std/primitive.pointer.html#method.copy_to)
///
/// ## Safety
///
/// See [`ptr::copy`](crate::ptr::copy).
#[inline]
#[cfg(not(tarpaulin_include))]
pub unsafe fn copy_to<T2, O2>(self, dest: BitPtr<Mut, T2, O2>, count: usize)
where
T2: BitStore,
O2: BitOrder,
{
super::copy(self.to_const(), dest, count);
}
/// Copies `count` bits from `self` to `dest`. The source and destination
/// may *not* overlap.
///
/// ## Original
///
/// [`pointer::copy_to_nonoverlapping`](https://doc.rust-lang.org/std/primitive.pointer.html#method.copy_to_nonoverlapping)
///
/// ## Safety
///
/// See [`ptr::copy_nonoverlapping`](crate::ptr::copy_nonoverlapping).
#[inline]
#[cfg(not(tarpaulin_include))]
pub unsafe fn copy_to_nonoverlapping<T2, O2>(
self,
dest: BitPtr<Mut, T2, O2>,
count: usize,
) where
T2: BitStore,
O2: BitOrder,
{
super::copy_nonoverlapping(self.to_const(), dest, count);
}
/// Computes the offset (in bits) that needs to be applied to the
/// bit-pointer in order to make it aligned to the given *byte* alignment.
///
/// “Alignment” here means that the bit-pointer selects the starting bit of
/// a memory location whose address satisfies the requested alignment.
///
/// `align` is measured in **bytes**. If you wish to align your bit-pointer
/// to a specific fraction (½, ¼, or ⅛ of one byte), please file an issue
/// and I will work on adding this functionality.
///
/// ## Original
///
/// [`pointer::align_offset`](https://doc.rust-lang.org/std/primitive.pointer.html#method.align_offset)
///
/// ## Notes
///
/// If the base-element address of the bit-pointer is already aligned to
/// `align`, then this will return the bit-offset required to select the
/// first bit of the successor element.
///
/// If it is not possible to align the bit-pointer, then the implementation
/// returns `usize::MAX`.
///
/// The return value is measured in bits, not `T` elements or bytes. The
/// only thing you can do with it is pass it into [`.add()`] or
/// [`.wrapping_add()`].
///
/// Note from the standard library: It is permissible for the implementation
/// to *always* return `usize::MAX`. Only your algorithm’s performance can
/// depend on getting a usable offset here; it must be correct independently
/// of this function providing a useful value.
///
/// ## Safety
///
/// There are no guarantees whatsoëver that offsetting the bit-pointer will
/// not overflow or go beyond the allocation that the bit-pointer selects.
/// It is up to the caller to ensure that the returned offset is correct in
/// all terms other than alignment.
///
/// ## Panics
///
/// This method panics if `align` is not a power of two.
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
///
/// let data = [0u8; 3];
/// let ptr = BitPtr::<_, _, Lsb0>::from_slice(&data);
/// let ptr = unsafe { ptr.add(2) };
/// let count = ptr.align_offset(2);
/// assert!(count >= 6);
/// ```
///
/// [`.add()`]: Self::add
/// [`.wrapping_add()`]: Self::wrapping_add
#[inline]
pub fn align_offset(self, align: usize) -> usize {
let width = mem::bits_of::<T::Mem>();
match (
self.ptr.to_const().align_offset(align),
self.bit.into_inner() as usize,
) {
(0, 0) => 0,
(0, head) => align * mem::bits_of::<u8>() - head,
(usize::MAX, _) => usize::MAX,
(elts, head) => elts.wrapping_mul(width).wrapping_sub(head),
}
}
}
/// Port of the `*mut bool` inherent API.
impl<T, O> BitPtr<Mut, T, O>
where
T: BitStore,
O: BitOrder,
{
/// Produces a proxy reference to the referent bit.
///
/// Because `BitPtr` guarantees that it is non-null and well-aligned, this
/// never returns `None`. However, this is still unsafe to call on any
/// bit-pointers created from conjured values rather than known references.
///
/// ## Original
///
/// [`pointer::as_mut`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_mut)
///
/// ## API Differences
///
/// This produces a proxy type rather than a true reference. The proxy
/// implements `DerefMut<Target = bool>`, and can be converted to
/// `&mut bool` with a reborrow `&mut *`.
///
/// Writes to the proxy are not reflected in the proxied location until the
/// proxy is destroyed, either through `Drop` or its [`.commit()`] method.
///
/// ## Safety
///
/// Since `BitPtr` does not permit null or misaligned pointers, this method
/// will always dereference the pointer in order to create the proxy. As
/// such, you must ensure the following conditions are met:
///
/// - the pointer must be dereferenceable as defined in the standard library
/// documentation
/// - the pointer must point to an initialized instance of `T`
/// - you must ensure that no other pointer will race to modify the referent
/// location while this call is reading from memory to produce the proxy
/// - you must ensure that no other `bitvec` handle targets the referent bit
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
///
/// let mut data = 0u8;
/// let ptr = BitPtr::<_, _, Lsb0>::from_mut(&mut data);
/// let mut val = unsafe { ptr.as_mut() }.unwrap();
/// assert!(!*val);
/// *val = true;
/// assert!(*val);
/// ```
///
/// [`.commit()`]: crate::ptr::BitRef::commit
#[inline]
pub unsafe fn as_mut<'a>(self) -> Option<BitRef<'a, Mut, T, O>> {
Some(BitRef::from_bitptr(self))
}
/// Copies `count` bits from the region starting at `src` to the region
/// starting at `self`.
///
/// The regions are free to overlap; the implementation will detect overlap
/// and correctly avoid it.
///