Rollup merge of #35895 - F001:patch-1, r=steveklabnik
Fix documentation in cell mod The implementation of Rc type in this doc is outdated.
This commit is contained in:
commit
325b7111c2
@ -119,26 +119,55 @@
|
|||||||
//! `Cell<T>`.
|
//! `Cell<T>`.
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
|
//! #![feature(core_intrinsics)]
|
||||||
|
//! #![feature(shared)]
|
||||||
//! use std::cell::Cell;
|
//! use std::cell::Cell;
|
||||||
|
//! use std::ptr::Shared;
|
||||||
|
//! use std::intrinsics::abort;
|
||||||
|
//! use std::intrinsics::assume;
|
||||||
//!
|
//!
|
||||||
//! struct Rc<T> {
|
//! struct Rc<T: ?Sized> {
|
||||||
//! ptr: *mut RcBox<T>
|
//! ptr: Shared<RcBox<T>>
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! struct RcBox<T> {
|
//! struct RcBox<T: ?Sized> {
|
||||||
//! # #[allow(dead_code)]
|
//! strong: Cell<usize>,
|
||||||
|
//! refcount: Cell<usize>,
|
||||||
//! value: T,
|
//! value: T,
|
||||||
//! refcount: Cell<usize>
|
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! impl<T> Clone for Rc<T> {
|
//! impl<T: ?Sized> Clone for Rc<T> {
|
||||||
//! fn clone(&self) -> Rc<T> {
|
//! fn clone(&self) -> Rc<T> {
|
||||||
//! unsafe {
|
//! self.inc_strong();
|
||||||
//! (*self.ptr).refcount.set((*self.ptr).refcount.get() + 1);
|
//! Rc { ptr: self.ptr }
|
||||||
//! Rc { ptr: self.ptr }
|
|
||||||
//! }
|
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
|
//!
|
||||||
|
//! trait RcBoxPtr<T: ?Sized> {
|
||||||
|
//!
|
||||||
|
//! fn inner(&self) -> &RcBox<T>;
|
||||||
|
//!
|
||||||
|
//! fn strong(&self) -> usize {
|
||||||
|
//! self.inner().strong.get()
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn inc_strong(&self) {
|
||||||
|
//! self.inner()
|
||||||
|
//! .strong
|
||||||
|
//! .set(self.strong()
|
||||||
|
//! .checked_add(1)
|
||||||
|
//! .unwrap_or_else(|| unsafe { abort() }));
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! impl<T: ?Sized> RcBoxPtr<T> for Rc<T> {
|
||||||
|
//! fn inner(&self) -> &RcBox<T> {
|
||||||
|
//! unsafe {
|
||||||
|
//! assume(!(*(&self.ptr as *const _ as *const *const ())).is_null());
|
||||||
|
//! &(**self.ptr)
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user