qcell/qcell.rs
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
use core::cell::UnsafeCell;
use core::marker::PhantomPinned;
use core::pin::Pin;
use core::sync::atomic::{AtomicUsize, Ordering};
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
// Ensure the alignment is 2 so we can use odd-numbered IDs for those
// created via `QCellOwnerSeq`.
#[repr(align(2))]
#[derive(Clone, Copy)]
struct OwnerIDTarget {
_data: u16,
}
const MAGIC_OWNER_ID_TARGET: OwnerIDTarget = OwnerIDTarget { _data: 0xCE11 };
#[cold]
#[inline(never)]
fn bad_owner_panic() -> ! {
panic!("QCell accessed with incorrect owner");
}
macro_rules! owner_check {
($owner:expr $(, $qcell:expr)+) => {
$(
if $qcell.owner.0 != $owner.id().0 {
bad_owner_panic();
}
)+
}
}
#[cold]
#[inline(never)]
fn not_distinct_panic() -> ! {
panic!("Illegal to borrow same QCell twice with rw2() or rw3()");
}
macro_rules! distinct_check {
($qc1:expr, $qc2:expr) => {{
let qc1 = $qc1 as *const _ as *const () as usize;
let qc2 = $qc2 as *const _ as *const () as usize;
if qc1 == qc2 {
not_distinct_panic();
}
}};
($qc1:expr, $qc2:expr, $qc3:expr) => {{
let qc1 = $qc1 as *const _ as *const () as usize;
let qc2 = $qc2 as *const _ as *const () as usize;
let qc3 = $qc3 as *const _ as *const () as usize;
if qc1 == qc2 || qc2 == qc3 || qc3 == qc1 {
not_distinct_panic();
}
}};
}
/// Internal ID associated with a [`QCell`] owner.
///
/// The only purpose of this is to create [`QCell`] instances without
/// requiring a borrow on the owner. A [`QCellOwnerID`] can be passed
/// to any code that needs to create [`QCell`] instances. However to
/// access those [`QCell`] instances after creation will still require
/// a borrow on the original owner. Create a [`QCellOwnerID`] from an
/// owner using `owner.into()` or `owner.id()`.
///
/// # Safety
///
/// Whilst the existence of this type does mean that an ID can exist
/// longer than than the owner, all that allows is new [`QCell`]
/// instances to be created after the owner has gone. But [`QCell`]
/// instances can outlive the owner in any case, so this makes no
/// difference to safety.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct QCellOwnerID(usize);
impl QCellOwnerID {
/// Create a new cell owned by this owner-ID. See also
/// [`QCell::new`].
///
/// [`QCell::new`]: struct.QCell.html
pub fn cell<T>(self, value: T) -> QCell<T> {
QCell {
value: UnsafeCell::new(value),
owner: self,
}
}
}
#[cfg(feature = "alloc")]
impl From<&QCellOwner> for QCellOwnerID {
fn from(owner: &QCellOwner) -> Self {
owner.id()
}
}
impl From<&QCellOwnerSeq> for QCellOwnerID {
fn from(owner: &QCellOwnerSeq) -> Self {
owner.id()
}
}
impl From<Pin<&QCellOwnerPinned>> for QCellOwnerID {
fn from(owner: Pin<&QCellOwnerPinned>) -> Self {
owner.id()
}
}
/// Cell whose contents is owned (for borrowing purposes) by a
/// [`QCellOwner`], a [`QCellOwnerSeq`] or a [`QCellOwnerPinned`].
///
/// To borrow from this cell, use the borrowing calls on the owner
/// instance that was used to create it. For [`QCellOwner`], there
/// are also convenience methods [`QCell::ro`] and [`QCell::rw`]. See
/// also [crate documentation](index.html).
///
/// [`QCellOwner`]: struct.QCellOwner.html
/// [`QCell::ro`]: struct.QCell.html#method.ro
/// [`QCell::rw`]: struct.QCell.html#method.rw
pub struct QCell<T: ?Sized> {
owner: QCellOwnerID,
value: UnsafeCell<T>,
}
// QCell already automatically implements Send, but not Sync.
// We can add this implementation though, since it's fine to
// send a &QCell to another thread, and even mutably borrow the value
// there, as long as T is Send and Sync.
//
// The reason why QCell<T>'s impl of Sync requires T: Send + Sync
// instead of just T: Sync is that QCell provides interior mutability.
// If you send a &QCell<T> (and its owner) to a different thread, you
// can call .rw() to get a &mut T, and use std::mem::swap() to move
// the T, effectively sending the T to that other thread. That's not
// allowed if T: !Send.
//
// Note that the bounds on T for QCell<T>'s impl of Sync are the same
// as those of std::sync::RwLock<T>. That's not a coincidence.
// The way these types let you access T concurrently is the same,
// even though the locking mechanisms are different.
unsafe impl<T: Send + Sync + ?Sized> Sync for QCell<T> {}
impl<T> QCell<T> {
/// Create a new [`QCell`] owned for borrowing purposes by the
/// owner with the given [`QCellOwnerID`], or a type that can be
/// converted into a [`QCellOwnerID`], such as `&owner`. So calls
/// will typically take the form `QCell::new(&owner, value)` or
/// `QCell::new(owner_id, value)`.
#[inline]
pub fn new(id: impl Into<QCellOwnerID>, value: T) -> QCell<T> {
QCell {
value: UnsafeCell::new(value),
owner: id.into(),
}
}
/// Destroy the cell and return the contained value
///
/// Safety: Since this consumes the cell, there can be no other
/// references to the cell or the data at this point.
#[inline]
pub fn into_inner(self) -> T {
self.value.into_inner()
}
}
#[cfg(feature = "alloc")]
impl<T: ?Sized> QCell<T> {
/// Convenience method to borrow a cell immutably when the owner
/// is a [`QCellOwner`]. Equivalent to [`QCellOwner::ro`]. See
/// [`QCellOwnerSeq::ro`] or [`QCellOwnerPinned::ro`] to borrow
/// for other owner types.
#[inline]
pub fn ro<'a>(&'a self, owner: &'a QCellOwner) -> &'a T {
owner.ro(self)
}
/// Convenience method to borrow a cell mutably when the owner is
/// a [`QCellOwner`]. Equivalent to [`QCellOwner::rw`]. See
/// [`QCellOwnerSeq::rw`] or [`QCellOwnerPinned::rw`] to borrow
/// for other owner types.
#[inline]
pub fn rw<'a>(&'a self, owner: &'a mut QCellOwner) -> &'a mut T {
owner.rw(self)
}
/// Returns a mutable reference to the underlying data
///
/// Note that this is only useful at the beginning-of-life or
/// end-of-life of the cell when you have exclusive access to it.
/// Normally you'd use [`QCell::rw`] or [`QCellOwner::rw`] to get
/// a mutable reference to the contents of the cell.
///
/// Safety: This call borrows `QCell` mutably which guarantees
/// that we possess the only reference. This means that there can
/// be no active borrows of other forms, even ones obtained using
/// an immutable reference.
#[inline]
pub fn get_mut(&mut self) -> &mut T {
self.value.get_mut()
}
}
/// Borrowing-owner of zero or more [`QCell`] instances.
///
/// The owner will have a temporally unique ID associated with it to
/// detect use of the wrong owner to access a cell at runtime, which
/// is a programming error. Temporally unique means that at any one
/// time, only one owner will hold that ID. This type derives the
/// owner ID from the address of an internal memory allocation which
/// this owner holds until it is dropped, which ensures that the ID is
/// temporally unique. The allocation is aligned to ensure that its
/// ID cannot collide with those created using [`QCellOwnerSeq`].
///
/// In a `no_std` environment this requires the `alloc` feature
/// because it allocates memory. For a `no_std` environment without
/// `alloc`, consider using [`QCellOwnerSeq`] or [`QCellOwnerPinned`].
///
/// # Safety
///
/// This should successfully defend against all malicious and unsafe
/// use. If not, please raise an issue. The same unique ID may later
/// be allocated to someone else once you drop the returned owner, but
/// this cannot be abused to cause unsafe access to cells because
/// there will still be only one owner active at any one time with
/// that ID. Also it cannot be used maliciously to access cells which
/// don't belong to the new caller, because you also need a reference
/// to the cells. So for example if you have a graph of cells that is
/// only accessible through a private structure, then if someone else
/// gets the same owner ID later, it makes no difference, because they
/// have no way to get a reference to those cells. In any case, you
/// are probably going to drop all those cells at the same time as
/// dropping the owner, because they are no longer of any use without
/// the owner ID.
///
/// See [crate documentation](index.html).
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub struct QCellOwner {
// `Box` should be enough to ensure that the address is unique and
// stable, but add `Pin` as a safeguard against any future
// optimisation of `Box`.
handle: Pin<Box<OwnerIDTarget>>,
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl Default for QCellOwner {
fn default() -> Self {
QCellOwner::new()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl QCellOwner {
/// Create an owner that can be used for creating many [`QCell`]
/// instances.
#[inline]
pub fn new() -> Self {
let handle = Box::pin(MAGIC_OWNER_ID_TARGET);
Self { handle }
}
/// Get the internal owner ID. This may be used to create [`QCell`]
/// instances without needing a borrow on this structure, which is
/// useful if this structure is already borrowed.
#[inline]
pub fn id(&self) -> QCellOwnerID {
let raw_ptr: *const OwnerIDTarget = &*self.handle;
QCellOwnerID(raw_ptr as usize)
}
/// Create a new cell owned by this owner instance. See also
/// [`QCell::new`].
#[inline]
pub fn cell<T>(&self, value: T) -> QCell<T> {
let id: QCellOwnerID = self.into();
id.cell(value)
}
/// Borrow contents of a [`QCell`] immutably (read-only). Many
/// [`QCell`] instances can be borrowed immutably at the same time
/// from the same owner. Panics if the [`QCell`] is not owned by
/// this [`QCellOwner`].
#[inline]
pub fn ro<'a, T: ?Sized>(&'a self, qc: &'a QCell<T>) -> &'a T {
owner_check!(self, qc);
unsafe { &*qc.value.get() }
}
/// Borrow contents of a [`QCell`] mutably (read-write). Only one
/// [`QCell`] at a time can be borrowed from the owner using this
/// call. The returned reference must go out of scope before
/// another can be borrowed. Panics if the [`QCell`] is not owned
/// by this [`QCellOwner`].
#[inline]
pub fn rw<'a, T: ?Sized>(&'a mut self, qc: &'a QCell<T>) -> &'a mut T {
owner_check!(self, qc);
unsafe { &mut *qc.value.get() }
}
/// Borrow contents of two [`QCell`] instances mutably. Panics if
/// the two [`QCell`] instances point to the same memory. Panics
/// if either [`QCell`] is not owned by this [`QCellOwner`].
#[inline]
pub fn rw2<'a, T: ?Sized, U: ?Sized>(
&'a mut self,
qc1: &'a QCell<T>,
qc2: &'a QCell<U>,
) -> (&'a mut T, &'a mut U) {
owner_check!(self, qc1, qc2);
distinct_check!(qc1, qc2);
unsafe { (&mut *qc1.value.get(), &mut *qc2.value.get()) }
}
/// Borrow contents of three [`QCell`] instances mutably. Panics
/// if any pair of [`QCell`] instances point to the same memory.
/// Panics if any [`QCell`] is not owned by this [`QCellOwner`].
#[inline]
pub fn rw3<'a, T: ?Sized, U: ?Sized, V: ?Sized>(
&'a mut self,
qc1: &'a QCell<T>,
qc2: &'a QCell<U>,
qc3: &'a QCell<V>,
) -> (&'a mut T, &'a mut U, &'a mut V) {
owner_check!(self, qc1, qc2, qc3);
distinct_check!(qc1, qc2, qc3);
unsafe {
(
&mut *qc1.value.get(),
&mut *qc2.value.get(),
&mut *qc3.value.get(),
)
}
}
}
// Used to generate a unique QCellOwnerID number for each
// QCellOwnerSeq. Start at index 1 and increment by 2 each time so
// the number is always odd to ensure it will never conflict with the
// address of a OwnerIDTarget.
static FAST_QCELLOWNER_ID: AtomicUsize = AtomicUsize::new(1);
/// Borrowing-owner of zero or more [`QCell`] instances, using an ID
/// sequence.
///
/// The owner will have a unique(-ish) ID associated with it to detect
/// use of the wrong owner to access a cell at runtime, which is a
/// programming error. This type allocates the owner ID from a
/// wrapping sequence sourced from a global atomic variable, so it is
/// very fast to allocate.
///
/// # Safety
///
/// A malicious coder could cause an intentional ID collision, e.g. by
/// creating 2^63 owners on a 64-bit build (or 2^31 on 32-bit, etc),
/// which would cause the ID to wrap. This would allow that coder to
/// cause undefined behaviour in their own code. So at a stretch this
/// could allow a coder to hide unsound code from a safety review.
/// Because of that the [`QCellOwnerSeq::new`] method is marked as
/// `unsafe`. However it is not possible to use it unsafely by
/// accident, only through making an intentional, determined and
/// CPU-intensive effort to exploit it.
///
/// See [crate documentation](index.html).
pub struct QCellOwnerSeq {
id: QCellOwnerID,
}
// Default implementation not possible, due to `unsafe`
impl QCellOwnerSeq {
/// Create an owner that can be used for creating many [`QCell`]
/// instances.
///
/// # Safety
///
/// The contract with the caller is that the caller must not
/// intentionally create an owner-ID collision and exploit it to
/// create undefined behaviour. The caller could do this by
/// creating 2^63 more owners on a 64-bit build (or 2^31 on
/// 32-bit, etc), causing the ID to wrap, and then using two
/// owners that they know to have the same ID to access the same
/// memory mutably from two references at the same time. This is
/// totally impossible to do by accident, so any normal use of
/// this call will be 100% safe.
///
/// To get unsound behaviour requires both an owner ID collision
/// (which might just about happen by accident in very unusual
/// situations), and then also intentionally using the wrong owner
/// to access a cell. Usually using the wrong owner to access a
/// cell would cause an immediate panic because it is a
/// programming error. It is extremely unlikely that there would
/// always be the same ID collision in testing, so this panic
/// would soon be fixed. Once it is fixed, there is absolutely no
/// way that even an accidental collision could cause any unsound
/// behaviour, because the bug has been eliminated, and the
/// correct owner is always used to access each cell.
#[inline]
pub unsafe fn new() -> Self {
// Must increment by 2 to ensure we never collide with an ID
// derived from the address of an `OwnerIDTarget`. Use
// `Relaxed` ordering because we don't care who gets which ID,
// just that they are different.
Self {
id: QCellOwnerID(FAST_QCELLOWNER_ID.fetch_add(2, Ordering::Relaxed)),
}
}
/// Get the internal owner ID. This may be used to create
/// [`QCell`] instances without needing a borrow on this
/// structure, which is useful if this structure is already
/// borrowed.
#[inline]
pub fn id(&self) -> QCellOwnerID {
self.id
}
/// Create a new cell owned by this owner instance. See also
/// [`QCell::new`].
///
/// [`QCell::new`]: struct.QCell.html
#[inline]
pub fn cell<T>(&self, value: T) -> QCell<T> {
self.id.cell(value)
}
/// Borrow contents of a [`QCell`] immutably (read-only). Many
/// [`QCell`] instances can be borrowed immutably at the same time
/// from the same owner. Panics if the [`QCell`] is not owned by
/// this [`QCellOwnerSeq`].
#[inline]
pub fn ro<'a, T: ?Sized>(&'a self, qc: &'a QCell<T>) -> &'a T {
owner_check!(self, qc);
unsafe { &*qc.value.get() }
}
/// Borrow contents of a [`QCell`] mutably (read-write). Only one
/// [`QCell`] at a time can be borrowed from the owner using this
/// call. The returned reference must go out of scope before
/// another can be borrowed. Panics if the [`QCell`] is not owned
/// by this [`QCellOwnerSeq`].
#[inline]
pub fn rw<'a, T: ?Sized>(&'a mut self, qc: &'a QCell<T>) -> &'a mut T {
owner_check!(self, qc);
unsafe { &mut *qc.value.get() }
}
/// Borrow contents of two [`QCell`] instances mutably. Panics if
/// the two [`QCell`] instances point to the same memory. Panics
/// if either [`QCell`] is not owned by this [`QCellOwnerSeq`].
#[inline]
pub fn rw2<'a, T: ?Sized, U: ?Sized>(
&'a mut self,
qc1: &'a QCell<T>,
qc2: &'a QCell<U>,
) -> (&'a mut T, &'a mut U) {
owner_check!(self, qc1, qc2);
distinct_check!(qc1, qc2);
unsafe { (&mut *qc1.value.get(), &mut *qc2.value.get()) }
}
/// Borrow contents of three [`QCell`] instances mutably. Panics
/// if any pair of [`QCell`] instances point to the same memory.
/// Panics if any [`QCell`] is not owned by this
/// [`QCellOwnerSeq`].
#[inline]
pub fn rw3<'a, T: ?Sized, U: ?Sized, V: ?Sized>(
&'a mut self,
qc1: &'a QCell<T>,
qc2: &'a QCell<U>,
qc3: &'a QCell<V>,
) -> (&'a mut T, &'a mut U, &'a mut V) {
owner_check!(self, qc1, qc2, qc3);
distinct_check!(qc1, qc2, qc3);
unsafe {
(
&mut *qc1.value.get(),
&mut *qc2.value.get(),
&mut *qc3.value.get(),
)
}
}
}
/// Borrowing-owner of zero or more [`QCell`] instances, based on a
/// pinned struct
///
/// This type uses its own memory address to provide a unique owner
/// ID, which requires no allocations and only 2 bytes of storage. So
/// this is suitable for a `no_std` environment without an allocator.
/// The owner can be created on the stack, or on the heap, as
/// required. To ensure its memory address cannot change while cells
/// exist that are owned by it, it requires itself to be pinned before
/// any operation interacting with the ID is attempted.
///
/// There are many ways to safely pin a value, such as
/// [`Box::pin`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.pin),
/// [`pin-utils::pin_mut!`](https://docs.rs/pin-utils/latest/pin_utils/macro.pin_mut.html),
/// [`tokio::pin!`](https://docs.rs/tokio/latest/tokio/macro.pin.html),
/// or the [`pin-project`](https://github.com/taiki-e/pin-project)
/// crate.
///
/// The following example uses the `pin_mut!` macro from the
/// `pin-utils` crate:
///
/// ```
/// use pin_utils::pin_mut;
/// use qcell::{QCell, QCellOwnerPinned};
///# use std::rc::Rc;
///# use std::pin::Pin;
/// let mut owner = QCellOwnerPinned::new();
/// pin_mut!(owner);
/// let item = Rc::new(owner.as_ref().cell(Vec::<u8>::new()));
/// owner.as_mut().rw(&item).push(1);
/// test(owner, &item);
///
/// fn test(owner: Pin<&mut QCellOwnerPinned>, item: &Rc<QCell<Vec<u8>>>) {
/// owner.rw(&item).push(2);
/// }
/// ```
///
/// This example incorporates the [`QCellOwnerPinned`] into a larger
/// structure kept on the stack, and accesses it using the
/// [`pin-project`](https://github.com/taiki-e/pin-project) crate:
///
/// ```
/// use crate::qcell::{QCell, QCellOwnerPinned};
/// use pin_project::pin_project;
/// use pin_utils::pin_mut;
///# use std::pin::Pin;
///# use std::rc::Rc;
///
/// #[pin_project]
/// struct MyStruct {
/// _misc: usize, // Unpinned value
/// #[pin]
/// owner: QCellOwnerPinned,
/// }
///
/// let mystruct = MyStruct {
/// _misc: 0,
/// owner: QCellOwnerPinned::new(),
/// };
///
/// pin_mut!(mystruct);
///
/// let item = Rc::new(
/// mystruct.as_mut().project().owner.as_ref().cell(Vec::<u8>::new())
/// );
/// mystruct.as_mut().project().owner.rw(&item).push(1);
/// test(mystruct.as_mut().project().owner, &item);
///
/// fn test(owner: Pin<&mut QCellOwnerPinned>, item: &Rc<QCell<Vec<u8>>>) {
/// owner.rw(&item).push(2);
/// }
/// ```
///
/// # Safety
///
/// After the owner is pinned, its address is used as a temporally
/// unique ID. This detects use of the wrong owner to access a cell
/// at runtime, which is a programming error.
///
/// Note that even without `Pin`, this would still be sound, because
/// there would still be only one owner valid at any one time with the
/// same ID, because two owners cannot occupy the same memory.
/// However `Pin` is useful because it helps the coder avoid
/// accidentally moving an owner from one address to another without
/// realizing it, and causing panics due to the changed owner ID.
///
/// The ID generated from this type cannot clash with IDs generated by
/// [`QCellOwner`] (which is also based on the addresses of occupied
/// memory, but always on the heap), or [`QCellOwnerSeq`] (which only
/// allocates odd IDs, which cannot clash with addresses from this
/// type which always have an alignment of 2). So this should
/// successfully defend against all malicious and unsafe use. If not,
/// please raise an issue.
///
/// The same unique ID may later be allocated to someone else once you
/// drop the returned owner, but this cannot be abused to cause unsafe
/// access to cells because there will still be only one owner active
/// at any one time with that ID. Also it cannot be used maliciously
/// to access cells which don't belong to the new caller, because you
/// also need a reference to the cells. So for example if you have a
/// graph of cells that is only accessible through a private
/// structure, then someone else getting the same owner ID makes no
/// difference, because they have no way to get a reference to those
/// cells. In any case, you are probably going to drop all those
/// cells at the same time as dropping the owner, because they are no
/// longer of any use without the owner ID.
///
/// [`QCellOwner`]: struct.QCellOwner.html
pub struct QCellOwnerPinned {
target: OwnerIDTarget,
// ensure this type is !Unpin
_marker: PhantomPinned,
}
impl Default for QCellOwnerPinned {
fn default() -> Self {
QCellOwnerPinned::new()
}
}
impl QCellOwnerPinned {
/// Create an owner that can be used for creating many [`QCell`]
/// instances.
#[inline]
pub fn new() -> Self {
Self {
target: MAGIC_OWNER_ID_TARGET,
_marker: PhantomPinned,
}
}
/// Get the internal owner ID. This may be used to create
/// [`QCell`] instances without needing a borrow on this
/// structure, which is useful if this structure is already
/// borrowed.
///
/// Requires this owner to be pinned before use.
pub fn id(self: Pin<&Self>) -> QCellOwnerID {
// Pin guarantees that our address will not change until we
// are dropped, so we can use it as a unique ID.
let raw_ptr: *const OwnerIDTarget = &self.target;
QCellOwnerID(raw_ptr as usize)
}
/// Create a new cell owned by this owner instance.
///
/// Requires this owner to be pinned before use.
#[inline]
pub fn cell<T>(self: Pin<&Self>, value: T) -> QCell<T> {
let id: QCellOwnerID = self.into();
id.cell(value)
}
/// Borrow contents of a [`QCell`] immutably (read-only). Many
/// [`QCell`] instances can be borrowed immutably at the same time
/// from the same owner. Panics if the [`QCell`] is not owned by
/// this [`QCellOwnerPinned`].
///
/// Requires this owner to be pinned before use.
#[inline]
pub fn ro<'a, T: ?Sized>(self: Pin<&'a Self>, qc: &'a QCell<T>) -> &'a T {
owner_check!(self, qc);
unsafe { &*qc.value.get() }
}
/// Borrow contents of a [`QCell`] mutably (read-write). Only one
/// [`QCell`] at a time can be borrowed from the owner using this
/// call. The returned reference must go out of scope before
/// another can be borrowed. Panics if the [`QCell`] is not owned
/// by this [`QCellOwnerPinned`].
///
/// Requires this owner to be pinned before use.
#[inline]
#[allow(clippy::mut_from_ref)]
pub fn rw<'a, T: ?Sized>(self: Pin<&'a mut Self>, qc: &'a QCell<T>) -> &'a mut T {
owner_check!(self.as_ref(), qc);
unsafe { &mut *qc.value.get() }
}
/// Borrow contents of two [`QCell`] instances mutably. Panics if
/// the two [`QCell`] instances point to the same memory. Panics
/// if either [`QCell`] is not owned by this [`QCellOwnerPinned`].
///
/// Requires this owner to be pinned before use.
#[inline]
pub fn rw2<'a, T: ?Sized, U: ?Sized>(
self: Pin<&'a mut Self>,
qc1: &'a QCell<T>,
qc2: &'a QCell<U>,
) -> (&'a mut T, &'a mut U) {
owner_check!(self.as_ref(), qc1, qc2);
distinct_check!(qc1, qc2);
unsafe { (&mut *qc1.value.get(), &mut *qc2.value.get()) }
}
/// Borrow contents of three [`QCell`] instances mutably. Panics
/// if any pair of [`QCell`] instances point to the same memory.
/// Panics if any [`QCell`] is not owned by this
/// [`QCellOwnerPinned`].
///
/// Requires this owner to be pinned before use.
#[inline]
pub fn rw3<'a, T: ?Sized, U: ?Sized, V: ?Sized>(
self: Pin<&'a mut Self>,
qc1: &'a QCell<T>,
qc2: &'a QCell<U>,
qc3: &'a QCell<V>,
) -> (&'a mut T, &'a mut U, &'a mut V) {
owner_check!(self.as_ref(), qc1, qc2, qc3);
distinct_check!(qc1, qc2, qc3);
unsafe {
(
&mut *qc1.value.get(),
&mut *qc2.value.get(),
&mut *qc3.value.get(),
)
}
}
}
#[cfg(test)]
mod tests {
use core::pin::Pin;
use pin_utils::pin_mut;
use super::{QCell, QCellOwnerPinned, QCellOwnerSeq};
#[test]
fn qcell_pinned() {
let owner = QCellOwnerPinned::new();
pin_mut!(owner);
let c1 = owner.as_ref().cell(100u32);
let c2 = owner.as_ref().cell(200u32);
(*owner.as_mut().rw(&c1)) += 1;
(*owner.as_mut().rw(&c2)) += 2;
let c1ref = owner.as_ref().ro(&c1);
let c2ref = owner.as_ref().ro(&c2);
let total = *c1ref + *c2ref;
assert_eq!(total, 303);
}
#[test]
fn qcell_fast_ids_pinned() {
let owner1 = QCellOwnerPinned::new();
pin_mut!(owner1);
let id1 = owner1.as_ref().id();
let owner2 = unsafe { QCellOwnerSeq::new() };
let id2 = owner2.id;
assert_ne!(id1.0, id2.0, "Expected ID 1/2 to be different");
let owner3 = unsafe { QCellOwnerSeq::new() };
let id3 = owner3.id;
assert_ne!(id2.0, id3.0, "Expected ID 2/3 to be different");
drop(owner2);
drop(owner3);
let owner4 = QCellOwnerPinned::new();
pin_mut!(owner4);
let id4 = owner4.as_ref().id();
assert_ne!(id1.0, id4.0, "Expected ID 1/4 to be different");
assert_ne!(id2.0, id4.0, "Expected ID 2/4 to be different");
assert_ne!(id3.0, id4.0, "Expected ID 3/4 to be different");
}
#[test]
fn qcell_sep_ids_pinned() {
let owner1 = QCellOwnerPinned::new();
let owner2 = QCellOwnerPinned::new();
pin_mut!(owner1);
pin_mut!(owner2);
let id1 = owner1.as_ref().id();
let id2 = owner2.as_ref().id();
let c11 = id1.cell(1u32);
let c12 = id2.cell(2u32);
let c21 = owner1.as_ref().cell(4u32);
let c22 = owner2.as_ref().cell(8u32);
assert_eq!(
15,
owner1.as_ref().ro(&c11)
+ owner2.as_ref().ro(&c12)
+ owner1.as_ref().ro(&c21)
+ owner2.as_ref().ro(&c22)
);
}
#[test]
fn qcell_unsized_pinned() {
let owner = QCellOwnerPinned::new();
struct Squares(u32);
struct Integers(u64);
trait Series {
fn step(&mut self);
fn value(&self) -> u64;
}
impl Series for Squares {
fn step(&mut self) {
self.0 += 1;
}
fn value(&self) -> u64 {
(self.0 as u64) * (self.0 as u64)
}
}
impl Series for Integers {
fn step(&mut self) {
self.0 += 1;
}
fn value(&self) -> u64 {
self.0
}
}
fn series(
init: u32,
is_squares: bool,
owner: Pin<&QCellOwnerPinned>,
) -> Box<QCell<dyn Series>> {
if is_squares {
Box::new(owner.cell(Squares(init)))
} else {
Box::new(owner.cell(Integers(init as u64)))
}
}
pin_mut!(owner);
let cell1 = series(4, false, owner.as_ref());
let cell2 = series(7, true, owner.as_ref());
let cell3 = series(3, true, owner.as_ref());
assert_eq!(owner.as_ref().ro(&cell1).value(), 4);
owner.as_mut().rw(&cell1).step();
assert_eq!(owner.as_ref().ro(&cell1).value(), 5);
assert_eq!(owner.as_ref().ro(&cell2).value(), 49);
owner.as_mut().rw(&cell2).step();
assert_eq!(owner.as_ref().ro(&cell2).value(), 64);
let (r1, r2, r3) = owner.as_mut().rw3(&cell1, &cell2, &cell3);
r1.step();
r2.step();
r3.step();
assert_eq!(owner.as_ref().ro(&cell1).value(), 6);
assert_eq!(owner.as_ref().ro(&cell2).value(), 81);
assert_eq!(owner.as_ref().ro(&cell3).value(), 16);
let (r1, r2) = owner.as_mut().rw2(&cell1, &cell2);
r1.step();
r2.step();
assert_eq!(owner.as_ref().ro(&cell1).value(), 7);
assert_eq!(owner.as_ref().ro(&cell2).value(), 100);
}
}
#[cfg(all(test, feature = "alloc"))]
mod tests_with_alloc {
use super::{QCell, QCellOwner, QCellOwnerSeq};
#[test]
fn qcell() {
let mut owner = QCellOwner::new();
let c1 = QCell::new(&owner, 100u32);
let c2 = QCell::new(&owner, 200u32);
(*owner.rw(&c1)) += 1;
(*owner.rw(&c2)) += 2;
let c1ref = owner.ro(&c1);
let c2ref = owner.ro(&c2);
let total = *c1ref + *c2ref;
assert_eq!(total, 303);
}
#[test]
fn qcell_ids() {
let owner1 = QCellOwner::new();
let id1 = owner1.id();
let owner2 = QCellOwner::new();
let id2 = owner2.id();
assert_ne!(id1.0, id2.0, "Expected ID 1/2 to be different");
drop(owner2);
let owner3 = QCellOwner::new();
let id3 = owner3.id();
assert_ne!(id1.0, id3.0, "Expected ID 1/3 to be different");
drop(owner3);
drop(owner1);
let owner4 = QCellOwner::new();
let id4 = owner4.id();
let owner5 = QCellOwner::new();
let id5 = owner5.id();
assert_ne!(id4.0, id5.0, "Expected ID 4/5 to be different");
}
#[test]
fn qcell_fast_ids() {
let owner1 = QCellOwner::new();
let id1 = owner1.id();
let owner2 = unsafe { QCellOwnerSeq::new() };
let id2 = owner2.id();
assert_ne!(id1.0, id2.0, "Expected ID 1/2 to be different");
let owner3 = unsafe { QCellOwnerSeq::new() };
let id3 = owner3.id();
assert_ne!(id2.0, id3.0, "Expected ID 2/3 to be different");
drop(owner2);
drop(owner3);
let owner4 = QCellOwner::new();
let id4 = owner4.id();
assert_ne!(id1.0, id4.0, "Expected ID 1/4 to be different");
assert_ne!(id2.0, id4.0, "Expected ID 2/4 to be different");
assert_ne!(id3.0, id4.0, "Expected ID 3/4 to be different");
}
#[test]
fn qcell_sep_ids() {
let owner1 = QCellOwner::new();
let owner2 = QCellOwner::new();
let id1 = owner1.id();
let id2 = owner2.id();
let c11 = id1.cell(1u32);
let c12 = id2.cell(2u32);
let c21 = owner1.cell(4u32);
let c22 = owner2.cell(8u32);
assert_eq!(
15,
owner1.ro(&c11) + owner2.ro(&c12) + owner1.ro(&c21) + owner2.ro(&c22)
);
}
#[test]
fn qcell_get_mut() {
let owner = QCellOwner::new();
let mut cell = QCell::new(&owner, 100u32);
let mut_ref = cell.get_mut();
*mut_ref = 50;
let cell_ref = owner.ro(&cell);
assert_eq!(*cell_ref, 50);
}
#[test]
fn qcell_into_inner() {
let owner = QCellOwner::new();
let cell = QCell::new(&owner, 100u32);
assert_eq!(cell.into_inner(), 100);
}
#[test]
fn qcell_unsized() {
let mut owner = QCellOwner::new();
struct Squares(u32);
struct Integers(u64);
trait Series {
fn step(&mut self);
fn value(&self) -> u64;
}
impl Series for Squares {
fn step(&mut self) {
self.0 += 1;
}
fn value(&self) -> u64 {
(self.0 as u64) * (self.0 as u64)
}
}
impl Series for Integers {
fn step(&mut self) {
self.0 += 1;
}
fn value(&self) -> u64 {
self.0
}
}
fn series(init: u32, is_squares: bool, owner: &QCellOwner) -> Box<QCell<dyn Series>> {
if is_squares {
Box::new(QCell::new(owner, Squares(init)))
} else {
Box::new(QCell::new(owner, Integers(init as u64)))
}
}
let own = &mut owner;
let cell1 = series(4, false, own);
let cell2 = series(7, true, own);
let cell3 = series(3, true, own);
assert_eq!(cell1.ro(own).value(), 4);
cell1.rw(own).step();
assert_eq!(cell1.ro(own).value(), 5);
assert_eq!(own.ro(&cell2).value(), 49);
own.rw(&cell2).step();
assert_eq!(own.ro(&cell2).value(), 64);
let (r1, r2, r3) = own.rw3(&cell1, &cell2, &cell3);
r1.step();
r2.step();
r3.step();
assert_eq!(cell1.ro(own).value(), 6);
assert_eq!(cell2.ro(own).value(), 81);
assert_eq!(cell3.ro(own).value(), 16);
let (r1, r2) = own.rw2(&cell1, &cell2);
r1.step();
r2.step();
assert_eq!(cell1.ro(own).value(), 7);
assert_eq!(cell2.ro(own).value(), 100);
}
}