id stringlengths 22 133 | text stringlengths 40 40.2k | arch stringclasses 1
value | syntax stringclasses 1
value | kind stringclasses 4
values | repo stringclasses 27
values | path stringlengths 5 116 | license stringclasses 6
values | commit stringlengths 40 40 | source_host stringclasses 1
value | category stringclasses 16
values | source_url stringlengths 85 196 | line_start int64 1 4.28k | line_end int64 4 4.31k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
tokio-rs/tokio:tokio/src/sync/once_cell.rs:12 | *self.value_set.get_mut() = false;
Some(unsafe { self.value.with(|ptr| ptr::read(ptr).assume_init()) })
} else {
None
}
}
/// Takes ownership of the current value, leaving the cell empty. Returns
/// `None` if the cell is empty.
pub fn take(&mut self) -> Option<... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8ccf2fb92e7568bf16318dc8f3205cad14a9bc5d | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8ccf2fb92e7568bf16318dc8f3205cad14a9bc5d/tokio/src/sync/once_cell.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:13 | impl<T> fmt::Display for SetError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SetError::AlreadyInitializedError(_) => write!(f, "AlreadyInitializedError"),
SetError::InitializingError(_) => write!(f, "InitializingError"),
}
}
}
impl<T: fm... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8ccf2fb92e7568bf16318dc8f3205cad14a9bc5d | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8ccf2fb92e7568bf16318dc8f3205cad14a9bc5d/tokio/src/sync/once_cell.rs | 481 | 508 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:2 | ///
/// static ONCE: OnceCell<u32> = OnceCell::const_new();
///
/// #[tokio::main]
/// async fn main() {
/// let result = ONCE.get_or_init(some_computation).await;
/// assert_eq!(*result, 2);
/// }
/// ```
///
/// It is often useful to write a wrapper method for accessing the value.
///
/// ```
/// use tokio::s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 4ca13e60155851b58e64f135f55c66493631a7d7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/4ca13e60155851b58e64f135f55c66493631a7d7/tokio/src/sync/once_cell.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:4 | }
}
}
impl<T> OnceCell<T> {
/// Creates a new empty `OnceCell` instance.
pub fn new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
semaphore: Semaphore::new(1),
}
}
/// Creates a new empty... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 4ca13e60155851b58e64f135f55c66493631a7d7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/4ca13e60155851b58e64f135f55c66493631a7d7/tokio/src/sync/once_cell.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:5 | /// assert_eq!(*result, 2);
/// }
/// ```
///
/// [`tokio-console`]: https://github.com/tokio-rs/console
/// [unstable feature]: crate#unstable-features
#[cfg(not(all(loom, test)))]
pub const fn const_new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 4ca13e60155851b58e64f135f55c66493631a7d7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/4ca13e60155851b58e64f135f55c66493631a7d7/tokio/src/sync/once_cell.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | ///
/// ```
/// use tokio::sync::OnceCell;
///
/// static ONCE: OnceCell<u32> = OnceCell::const_new_with(1);
///
/// async fn get_global_integer() -> &'static u32 {
/// ONCE.get_or_init(|| async {
/// 1 + 1
/// }).await
/// }
///
/// #[tokio::main]
///... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 4ca13e60155851b58e64f135f55c66493631a7d7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/4ca13e60155851b58e64f135f55c66493631a7d7/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:5 | /// assert_eq!(*result, 2);
/// }
/// ```
///
/// [`tokio-console`]: https://github.com/tokio-rs/console
/// [unstable feature]: crate#unstable-features
#[cfg(not(all(loom, test)))]
pub const fn const_new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | ea1cfbdb97f524a1ae465c0f5454dd6690dd5e6e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/ea1cfbdb97f524a1ae465c0f5454dd6690dd5e6e/tokio/src/sync/once_cell.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:3 | impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("OnceCell")
.field("value", &self.get())
.finish()
}
}
impl<T: Clone> Clone for OnceCell<T> {
fn clone(&self) -> OnceCell<T> {
OnceCell::new... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | d247e7f5df4bd861287467ecc5f827538bee4d63 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d247e7f5df4bd861287467ecc5f827538bee4d63/tokio/src/sync/once_cell.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:4 | }
}
}
impl<T> OnceCell<T> {
/// Creates a new empty `OnceCell` instance.
pub fn new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
semaphore: Semaphore::new(1),
}
}
/// Creates a new `Once... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | d247e7f5df4bd861287467ecc5f827538bee4d63 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d247e7f5df4bd861287467ecc5f827538bee4d63/tokio/src/sync/once_cell.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | ///
/// ```
/// use tokio::sync::OnceCell;
///
/// static ONCE: OnceCell<u32> = OnceCell::const_new();
///
/// async fn get_global_integer() -> &'static u32 {
/// ONCE.get_or_init(|| async {
/// 1 + 1
/// }).await
/// }
///
/// #[tokio::main]
/// async... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | d247e7f5df4bd861287467ecc5f827538bee4d63 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d247e7f5df4bd861287467ecc5f827538bee4d63/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:4 | }
}
}
impl<T> OnceCell<T> {
/// Creates a new empty `OnceCell` instance.
pub fn new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
semaphore: Semaphore::new(1),
}
}
/// Creates a new `Once... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:5 | /// async fn get_global_integer() -> &'static u32 {
/// ONCE.get_or_init(|| async {
/// 1 + 1
/// }).await
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let result = get_global_integer().await;
/// assert_eq!(*result, 1);
/// }
/// ```
#[c... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | /// async fn main() {
/// let result = get_global_integer().await;
/// assert_eq!(*result, 2);
/// }
/// ```
#[cfg(not(all(loom, test)))]
pub const fn const_new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::u... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:7 | unsafe {
self.value.with_mut(|ptr| (*ptr).as_mut_ptr().write(value));
}
// Using release ordering so any threads that read a true from this
// atomic is able to read the value we just stored.
self.value_set.store(true, Ordering::Release);
self.semaphore.close();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:8 | ///
/// If the `OnceCell` already has a value, this call will fail with an
/// [`SetError::AlreadyInitializedError`].
///
/// If the `OnceCell` is empty, but some other task is currently trying to
/// set the value, this call will fail with [`SetError::InitializingError`].
///
/// [`SetError... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:9 | /// this call will wait for that other task to finish, then return the value
/// that the other task produced.
///
/// If the provided operation is cancelled or panics, the initialization
/// attempt is cancelled. If there are other tasks waiting for the value to
/// be initialized, one of them will... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | unsafe { self.get_unchecked() }
}
}
}
}
/// Gets the value currently in the `OnceCell`, or initialize it with the
/// given asynchronous operation.
///
/// If some other task is currently working on initializing the `OnceCell`,
/// this call will wait for tha... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:11 | // permit is dropped.
let value = f().await;
match value {
Ok(value) => Ok(self.set_value(value, permit)),
Err(e) => Err(e),
}
}
Err(_) => {
debug_assert!(self... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:12 | // references of !Sync values across threads. We need T to be Send in order for
// OnceCell to by Sync because we can use `set` on `&OnceCell<T>` to send values
// (of type T) across threads.
unsafe impl<T: Sync + Send> Sync for OnceCell<T> {}
// Access to OnceCell's value is guarded by the semaphore permit
// and ato... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 441 | 492 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:13 | SetError::InitializingError(_) => false,
}
}
/// Whether `SetError` is `SetError::InitializingError`
pub fn is_initializing_err(&self) -> bool {
match self {
SetError::AlreadyInitializedError(_) => false,
SetError::InitializingError(_) => true,
}
}
} | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 8955ed5f8525d3585d99e762452978edb69a73ad | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8955ed5f8525d3585d99e762452978edb69a73ad/tokio/src/sync/once_cell.rs | 481 | 492 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:4 | }
}
}
impl<T> OnceCell<T> {
/// Creates a new empty `OnceCell` instance.
pub fn new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
semaphore: Semaphore::new(1),
}
}
/// Creates a new `Once... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:5 | ///
/// static ONCE: OnceCell<u32> = OnceCell::const_new();
///
/// async fn get_global_integer() -> &'static u32 {
/// ONCE.get_or_init(|| async {
/// 1 + 1
/// }).await
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let result = get_global_intege... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | &*self.value.with(|ptr| (*ptr).as_ptr())
}
// SAFETY: The OnceCell must not be empty.
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
&mut *self.value.with_mut(|ptr| (*ptr).as_mut_ptr())
}
fn set_value(&self, value: T, permit: SemaphorePermit<'_>) -> &T {
// SAFETY: We are holdi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:7 | pub fn get_mut(&mut self) -> Option<&mut T> {
if self.initialized_mut() {
Some(unsafe { self.get_unchecked_mut() })
} else {
None
}
}
/// Sets the value of the `OnceCell` to the given value if the `OnceCell` is
/// empty.
///
/// If the `OnceCell` alr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:8 | // the value.
Err(SetError::AlreadyInitializedError(value))
}
}
}
/// Gets the value currently in the `OnceCell`, or initialize it with the
/// given asynchronous operation.
///
/// If some other task is currently working on initializing the `OnceCell`,
/// t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:9 | // dropped.
let value = f().await;
self.set_value(value, permit)
}
Err(_) => {
debug_assert!(self.initialized());
// SAFETY: The semaphore has been closed. This only happens
// when the ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | // Here we try to acquire the semaphore permit. Holding the permit
// will allow us to set the value of the OnceCell, and prevents
// other tasks from initializing the OnceCell while we are holding
// it.
match self.semaphore.acquire().await {
Ok(permit) =... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:11 | /// Takes ownership of the current value, leaving the cell empty. Returns
/// `None` if the cell is empty.
pub fn take(&mut self) -> Option<T> {
std::mem::take(self).into_inner()
}
}
// Since `get` gives us access to immutable references of the OnceCell, OnceCell
// can only be Sync if T is Sync, ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:12 | }
}
impl<T: fmt::Debug> Error for SetError<T> {}
impl<T> SetError<T> {
/// Whether `SetError` is `SetError::AlreadyInitializedError`.
pub fn is_already_init_err(&self) -> bool {
match self {
SetError::AlreadyInitializedError(_) => true,
SetError::InitializingError(_) => false,
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | efe3ab679a05f3da3fcc511a44120239830254f2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efe3ab679a05f3da3fcc511a44120239830254f2/tokio/src/sync/once_cell.rs | 441 | 462 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:3 | impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("OnceCell")
.field("value", &self.get())
.finish()
}
}
impl<T: Clone> Clone for OnceCell<T> {
fn clone(&self) -> OnceCell<T> {
OnceCell::new... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:4 | value: UnsafeCell::new(MaybeUninit::new(value)),
semaphore,
}
}
}
impl<T> OnceCell<T> {
/// Creates a new empty `OnceCell` instance.
pub fn new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:5 | ///
/// async fn get_global_integer() -> &'static u32 {
/// ONCE.get_or_init(|| async {
/// 1 + 1
/// }).await
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let result = get_global_integer().await;
/// assert_eq!(*result, 2);
/// }
/// ```... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | }
// SAFETY: The OnceCell must not be empty.
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
&mut *self.value.with_mut(|ptr| (*ptr).as_mut_ptr())
}
fn set_value(&self, value: T, permit: SemaphorePermit<'_>) -> &T {
// SAFETY: We are holding the only permit on the semaphore.
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:7 | if self.initialized_mut() {
Some(unsafe { self.get_unchecked_mut() })
} else {
None
}
}
/// Sets the value of the `OnceCell` to the given value if the `OnceCell` is
/// empty.
///
/// If the `OnceCell` already has a value, this call will fail with an
/// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:8 | Err(SetError::AlreadyInitializedError(value))
}
}
}
/// Gets the value currently in the `OnceCell`, or initialize it with the
/// given asynchronous operation.
///
/// If some other task is currently working on initializing the `OnceCell`,
/// this call will wait for that ot... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:9 | let value = f().await;
self.set_value(value, permit)
}
Err(_) => {
debug_assert!(self.initialized());
// SAFETY: The semaphore has been closed. This only happens
// when the OnceCell is fully initialized.
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | // will allow us to set the value of the OnceCell, and prevents
// other tasks from initializing the OnceCell while we are holding
// it.
match self.semaphore.acquire().await {
Ok(permit) => {
debug_assert!(!self.initialized());
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:11 | /// Takes ownership of the current value, leaving the cell empty. Returns
/// `None` if the cell is empty.
pub fn take(&mut self) -> Option<T> {
std::mem::take(self).into_inner()
}
}
// Since `get` gives us access to immutable references of the OnceCell, OnceCell
// can only be Sync if T is Sync, ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:12 | }
impl<T: fmt::Debug> Error for SetError<T> {}
impl<T> SetError<T> {
/// Whether `SetError` is `SetError::AlreadyInitializedError`.
pub fn is_already_init_err(&self) -> bool {
match self {
SetError::AlreadyInitializedError(_) => true,
SetError::InitializingError(_) => false,
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/once_cell.rs | 441 | 461 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:8 | Err(SetError::AlreadyInitializedError(value))
}
}
}
/// Gets the value currently in the `OnceCell`, or initialize it with the
/// given asynchronous operation.
///
/// If some other task is currently working on initializing the `OnceCell`,
/// this call will wait for that ot... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | fc9518b62714daac9a38b46c698b94ac5d5b1ca2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/fc9518b62714daac9a38b46c698b94ac5d5b1ca2/tokio/src/sync/once_cell.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:9 | self.set_value(value, permit)
}
Err(_) => {
debug_assert!(self.initialized());
// SAFETY: The semaphore has been closed. This only happens
// when the OnceCell is fully initialized.
unsafe { self.get_uncheck... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | fc9518b62714daac9a38b46c698b94ac5d5b1ca2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/fc9518b62714daac9a38b46c698b94ac5d5b1ca2/tokio/src/sync/once_cell.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | Ok(permit) => {
debug_assert!(!self.initialized());
// If `f()` panics or `select!` is called, this
// `get_or_try_init` call is aborted and the semaphore
// permit is dropped.
let value = f().await;
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | fc9518b62714daac9a38b46c698b94ac5d5b1ca2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/fc9518b62714daac9a38b46c698b94ac5d5b1ca2/tokio/src/sync/once_cell.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:11 | }
}
// Since `get` gives us access to immutable references of the OnceCell, OnceCell
// can only be Sync if T is Sync, otherwise OnceCell would allow sharing
// references of !Sync values across threads. We need T to be Send in order for
// OnceCell to by Sync because we can use `set` on `&OnceCell<T>` to send values
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | fc9518b62714daac9a38b46c698b94ac5d5b1ca2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/fc9518b62714daac9a38b46c698b94ac5d5b1ca2/tokio/src/sync/once_cell.rs | 401 | 457 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:12 | impl<T> SetError<T> {
/// Whether `SetError` is `SetError::AlreadyInitializedError`.
pub fn is_already_init_err(&self) -> bool {
match self {
SetError::AlreadyInitializedError(_) => true,
SetError::InitializingError(_) => false,
}
}
/// Whether `SetError` is `Set... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | fc9518b62714daac9a38b46c698b94ac5d5b1ca2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/fc9518b62714daac9a38b46c698b94ac5d5b1ca2/tokio/src/sync/once_cell.rs | 441 | 457 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:3 | impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("OnceCell")
.field("value", &self.get())
.finish()
}
}
impl<T: Clone> Clone for OnceCell<T> {
fn clone(&self) -> OnceCell<T> {
OnceCell::new... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/sync/once_cell.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | Ok(permit) => {
debug_assert!(!self.initialized());
// If `f()` panics or `select!` is called, this
// `get_or_try_init` call is aborted and the semaphore
// permit is dropped.
let value = f().await;
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/sync/once_cell.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:11 | }
}
// Since `get` gives us access to immutable references of the OnceCell, OnceCell
// can only be Sync if T is Sync, otherwise OnceCell would allow sharing
// references of !Sync values across threads. We need T to be Send in order for
// OnceCell to by Sync because we can use `set` on `&OnceCell<T>` to send values
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/sync/once_cell.rs | 401 | 457 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | }
// SAFETY: The OnceCell must not be empty.
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
&mut *self.value.with_mut(|ptr| (*ptr).as_mut_ptr())
}
fn set_value(&self, value: T, permit: SemaphorePermit<'_>) -> &T {
// SAFETY: We are holding the only permit on the semaphore.
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 80d8d40a341e37f6c416cc46d30dd3e7d601c972 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/80d8d40a341e37f6c416cc46d30dd3e7d601c972/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:7 | if self.initialized_mut() {
Some(unsafe { self.get_unchecked_mut() })
} else {
None
}
}
/// Set the value of the `OnceCell` to the given value if the `OnceCell` is
/// empty.
///
/// If the `OnceCell` already has a value, this call will fail with an
/// [... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 80d8d40a341e37f6c416cc46d30dd3e7d601c972 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/80d8d40a341e37f6c416cc46d30dd3e7d601c972/tokio/src/sync/once_cell.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:8 | Err(SetError::AlreadyInitializedError(value))
}
}
}
/// Get the value currently in the `OnceCell`, or initialize it with the
/// given asynchronous operation.
///
/// If some other task is currently working on initializing the `OnceCell`,
/// this call will wait for that oth... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 80d8d40a341e37f6c416cc46d30dd3e7d601c972 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/80d8d40a341e37f6c416cc46d30dd3e7d601c972/tokio/src/sync/once_cell.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:9 | self.set_value(value, permit)
}
Err(_) => {
debug_assert!(self.initialized());
// SAFETY: The semaphore has been closed. This only happens
// when the OnceCell is fully initialized.
unsafe { self.get_uncheck... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 80d8d40a341e37f6c416cc46d30dd3e7d601c972 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/80d8d40a341e37f6c416cc46d30dd3e7d601c972/tokio/src/sync/once_cell.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | Ok(permit) => {
debug_assert!(!self.initialized());
// If `f()` panics or `select!` is called, this
// `get_or_try_init` call is aborted and the semaphore
// permit is dropped.
let value = f().await;
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 80d8d40a341e37f6c416cc46d30dd3e7d601c972 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/80d8d40a341e37f6c416cc46d30dd3e7d601c972/tokio/src/sync/once_cell.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:1 | use super::Semaphore;
use crate::loom::cell::UnsafeCell;
use std::error::Error;
use std::fmt;
use std::future::Future;
use std::mem::MaybeUninit;
use std::ops::Drop;
use std::ptr;
use std::sync::atomic::{AtomicBool, Ordering};
/// A thread-safe cell which can be written to only once.
///
/// Provides the functionality... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:2 | impl<T> Default for OnceCell<T> {
fn default() -> OnceCell<T> {
OnceCell::new()
}
}
impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("OnceCell")
.field("value", &self.get())
.finish()
}... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:3 | fn from(value: T) -> Self {
let semaphore = Semaphore::new(0);
semaphore.close();
OnceCell {
value_set: AtomicBool::new(true),
value: UnsafeCell::new(MaybeUninit::new(value)),
semaphore,
}
}
}
impl<T> OnceCell<T> {
/// Creates a new uninitiali... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:4 | semaphore: Semaphore::const_new(1),
}
}
/// Whether the value of the OnceCell is set or not.
pub fn initialized(&self) -> bool {
self.value_set.load(Ordering::Acquire)
}
// SAFETY: safe to call only once self.initialized() is true
unsafe fn get_unchecked(&self) -> &T {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:5 | ///
/// Returns None if the cell hasn't previously been initialized.
pub fn get_mut(&mut self) -> Option<&mut T> {
if self.initialized() {
Some(unsafe { self.get_unchecked_mut() })
} else {
None
}
}
/// Sets the value of the OnceCell to the argument value... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | _ => {
// Couldn't acquire the permit, look if initializing process is already completed
if !self.initialized() {
return Err(SetError::InitializingError(value));
}
}
}
}
Err(SetError::Already... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:7 | // SAFETY: There is only one permit on the semaphore, hence only one
// mutable reference is created
unsafe { self.set_value(value) };
// SAFETY: once the value is initialized, no mutable references are given out, so
// we ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:8 | if self.initialized() {
// SAFETY: once the value is initialized, no mutable references are given out, so
// we can give out arbitrarily many immutable references
unsafe { Ok(self.get_unchecked()) }
} else {
// After acquire().await we have either acquired a permi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:9 | );
}
}
}
}
}
/// Moves the value out of the cell, destroying the cell in the process.
///
/// Returns `None` if the cell is uninitialized.
pub fn into_inner(mut self) -> Option<T> {
if self.initialized() {
// Set to uniniti... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | /// Errors that can be returned from [`OnceCell::set`]
///
/// [`OnceCell::set`]: crate::sync::OnceCell::set
#[derive(Debug, PartialEq)]
pub enum SetError<T> {
/// Error resulting from [`OnceCell::set`] calls if the cell was previously initialized.
///
/// [`OnceCell::set`]: crate::sync::OnceCell::set
A... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 08ed41f339e345286f26bdbc6c67bf9f2975ed07 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/08ed41f339e345286f26bdbc6c67bf9f2975ed07/tokio/src/sync/once_cell.rs | 361 | 406 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | _ => {
// Couldn't acquire the permit, look if initializing process is already completed
if !self.initialized() {
return Err(SetError::InitializingError(value));
}
}
}
}
Err(SetError::Already... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 845626410a2e42902882f9750a9f8e2b38cf5a36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/845626410a2e42902882f9750a9f8e2b38cf5a36/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:7 | // SAFETY: There is only one permit on the semaphore, hence only one
// mutable reference is created
unsafe { self.set_value(value) };
// SAFETY: once the value is initialized, no mutable references are given out, so
// we ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 845626410a2e42902882f9750a9f8e2b38cf5a36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/845626410a2e42902882f9750a9f8e2b38cf5a36/tokio/src/sync/once_cell.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:8 | if self.initialized() {
// SAFETY: once the value is initialized, no mutable references are given out, so
// we can give out arbitrarily many immutable references
unsafe { Ok(self.get_unchecked()) }
} else {
// After acquire().await we have either acquired a permi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 845626410a2e42902882f9750a9f8e2b38cf5a36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/845626410a2e42902882f9750a9f8e2b38cf5a36/tokio/src/sync/once_cell.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:9 | );
}
}
}
}
}
/// Moves the value out of the cell, destroying the cell in the process.
///
/// Returns `None` if the cell is uninitialized.
pub fn into_inner(mut self) -> Option<T> {
if self.initialized() {
// Set to uniniti... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 845626410a2e42902882f9750a9f8e2b38cf5a36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/845626410a2e42902882f9750a9f8e2b38cf5a36/tokio/src/sync/once_cell.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | /// Errors that can be returned from [`OnceCell::set`]
///
/// [`OnceCell::set`]: crate::sync::OnceCell::set
#[derive(Debug, PartialEq)]
pub enum SetError<T> {
/// Error resulting from [`OnceCell::set`] calls if the cell was previously initialized.
///
/// [`OnceCell::set`]: crate::sync::OnceCell::set
A... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | 845626410a2e42902882f9750a9f8e2b38cf5a36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/845626410a2e42902882f9750a9f8e2b38cf5a36/tokio/src/sync/once_cell.rs | 361 | 406 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:2 | impl<T> Default for OnceCell<T> {
fn default() -> OnceCell<T> {
OnceCell::new()
}
}
impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("OnceCell")
.field("value", &self.get())
.finish()
}... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:3 | /// Creates a new uninitialized OnceCell instance.
pub fn new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
semaphore: Semaphore::new(1),
}
}
/// Creates a new initialized OnceCell instance if `va... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:4 | self.value_set.load(Ordering::Acquire)
}
// SAFETY: safe to call only once self.initialized() is true
unsafe fn get_unchecked(&self) -> &T {
&*self.value.with(|ptr| (*ptr).as_ptr())
}
// SAFETY: safe to call only once self.initialized() is true. Safe because
// because of the mutable r... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:5 | None
}
}
/// Sets the value of the OnceCell to the argument value.
///
/// If the value of the OnceCell was already set prior to this call
/// then [`SetError::AlreadyInitializedError`] is returned. If another thread
/// is initializing the cell while this method is called,
/// [`Se... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:6 | }
}
Err(SetError::AlreadyInitializedError(value))
}
/// Tries to initialize the value of the OnceCell using the async function `f`.
/// If the value of the OnceCell was already initialized prior to this call,
/// a reference to that initialized value is returned. If some other thread
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:7 | unsafe { self.get_unchecked() }
} else {
unreachable!("acquired semaphore after value was already initialized.");
}
}
Err(_) => {
if self.initialized() {
// SAFETY: once the value ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:8 | // is still uninitialized, or the current thread is awoken after another thread
// has intialized the value and closed the semaphore, in which case self.initialized
// is true and we don't set the value here
match self.semaphore.acquire().await {
Ok(_permit) => {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:9 | /// Moves the value out of the cell, destroying the cell in the process.
///
/// Returns `None` if the cell is uninitialized.
pub fn into_inner(mut self) -> Option<T> {
if self.initialized() {
// Set to uninitialized for the destructor of `OnceCell` to work properly
*self.val... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/once_cell.rs:10 | /// Error resulting from [`OnceCell::set`] calls if the cell was previously initialized.
///
/// [`OnceCell::set`]: crate::sync::OnceCell::set
AlreadyInitializedError(T),
/// Error resulting from [`OnceCell::set`] calls when the cell is currently being
/// inintialized during the calls to that meth... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/once_cell.rs | MIT | f6e4e85dfb60459e3ff430a7480f37338548e014 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f6e4e85dfb60459e3ff430a7480f37338548e014/tokio/src/sync/once_cell.rs | 361 | 400 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:9 | /// break;
/// }
/// }
/// }
/// # handle.await.unwrap();
/// # }
/// ```
#[derive(Debug)]
pub struct Receiver<T> {
inner: Option<Arc<Inner<T>>>,
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span,
#[cfg(all(tokio_unstable, feature = "tracing"))]
as... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:10 | impl fmt::Display for RecvError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "channel closed")
}
}
impl std::error::Error for RecvError {}
// ===== impl TryRecvError =====
impl fmt::Display for TryRecvError {
fn fmt(&self, fmt: &mut fm... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:11 | /// The task to notify when the value is sent.
///
/// ## Safety
///
/// The `RX_TASK_SET` bit in the `state` field is set if this field is
/// initialized. If that bit is unset, this field may be uninitialized.
rx_task: Task,
}
struct Task(UnsafeCell<MaybeUninit<Waker>>);
impl Task {
/// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:12 | self.0.with_mut(|ptr| {
let ptr: *mut Waker = unsafe { (*ptr).as_mut_ptr() };
unsafe {
ptr.drop_in_place();
}
});
}
/// # Safety
///
/// The caller must do the necessary synchronization to ensure that
/// the [`Self::0`] contains the valid... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:13 | /// # async fn main() {
/// let (tx, rx) = oneshot::channel();
///
/// tokio::spawn(async move {
/// if let Err(_) = tx.send(3) {
/// println!("the receiver dropped");
/// }
/// });
///
/// match rx.await {
/// Ok(v) => println!("got = {:?}", v),
/// Err(_) => println!("the sender dropped"),
///... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:14 | tracing::trace!(
target: "runtime::resource::state_update",
rx_dropped = false,
rx_dropped.op = "override",
)
});
resource_span.in_scope(|| {
tracing::trace!(
target: "runtime::resource::state_update",
value_sent = fals... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:15 | let async_op_span = resource_span
.in_scope(|| tracing::trace_span!("runtime.resource.async_op", source = "Receiver::await"));
#[cfg(all(tokio_unstable, feature = "tracing"))]
let async_op_poll_span =
async_op_span.in_scope(|| tracing::trace_span!("runtime.resource.async_op.poll"));
let rx... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:16 | /// Send a value to another task
///
/// ```
/// use tokio::sync::oneshot;
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// let (tx, rx) = oneshot::channel();
///
/// tokio::spawn(async move {
/// if let Err(_) = tx.send(3) {
/// pri... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:19 | /// });
///
/// // Wait for up to 10 seconds
/// let _ = time::timeout(Duration::from_secs(10), rx).await;
/// # }
/// ```
pub async fn closed(&mut self) {
use std::future::poll_fn;
#[cfg(all(tokio_unstable, feature = "tracing"))]
let resource_span = self.resource_span.c... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:21 | ///
/// ```
/// use tokio::sync::oneshot;
///
/// use std::future::poll_fn;
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// let (mut tx, mut rx) = oneshot::channel::<()>();
///
/// tokio::spawn(async move {
/// rx.close();
/// });
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:22 | if state.is_closed() {
// Set the flag again so that the waker is released in drop
State::set_tx_task(&inner.state);
coop.made_progress();
return Ready(());
} else {
unsafe { inner.tx_task.drop_task() };
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:30 | ///
/// drop(tx);
///
/// match rx.try_recv() {
/// // The channel will never receive a value.
/// Err(TryRecvError::Closed) => {}
/// _ => unreachable!(),
/// }
/// # }
/// ```
pub fn try_recv(&mut self) -> Result<T, TryRecvError> {
let result = if let Some(i... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,161 | 1,220 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:31 | } else {
Err(TryRecvError::Closed)
};
self.inner = None;
result
}
/// Blocking receive to call outside of asynchronous contexts.
///
/// # Panics
///
/// This function panics if called within an asynchronous execution
/// context.
///
/// # Examp... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,201 | 1,260 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:32 | crate::future::block_on(self)
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
if let Some(inner) = self.inner.as_ref() {
let state = inner.close();
if state.is_complete() {
// SAFETY: we have ensured that the `VALUE_SENT` bit has been set,
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,241 | 1,300 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:33 | #[cfg(all(tokio_unstable, feature = "tracing"))]
let res = ready!(trace_poll_op!("poll_recv", inner.poll_recv(cx)));
#[cfg(any(not(tokio_unstable), not(feature = "tracing")))]
let res = ready!(inner.poll_recv(cx));
res
} else {
panic!("called after c... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,281 | 1,340 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:34 | let mut state = State::load(&self.state, Acquire);
if state.is_complete() {
coop.made_progress();
match unsafe { self.consume_value() } {
Some(value) => Ready(Ok(value)),
None => Ready(Err(RecvError(()))),
}
} else if state.is_closed()... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,321 | 1,380 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:35 | // Attempt to set the task
unsafe {
self.rx_task.set_task(cx);
}
// Update the state
state = State::set_rx_task(&self.state);
if state.is_complete() {
coop.made_progress();
match... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,361 | 1,420 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:36 | // set `CLOSED`, the sender can no longer set `VALUE_SENT` and
// will never access `rx_task`. Therefore, we have exclusive
// access here.
unsafe { self.rx_task.drop_task() };
}
prev
}
/// Consumes the value. This function does not check `state`.
///
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,401 | 1,460 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:37 | this.with_mut(|v| *v)
}
impl<T> Drop for Inner<T> {
fn drop(&mut self) {
let state = State(mut_load(&mut self.state));
if state.is_rx_task_set() {
unsafe {
self.rx_task.drop_task();
}
}
if state.is_tx_task_set() {
unsafe {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,441 | 1,500 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:38 | /// Indicates that a waker for the receiving task has been set.
///
/// # Safety
///
/// If this bit is not set, the `rx_task` field may be uninitialized.
const RX_TASK_SET: usize = 0b00001;
/// Indicates that a value has been stored in the channel's inner `UnsafeCell`.
///
/// # Safety
///
/// This bit controls which ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,481 | 1,540 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:39 | // bit is already set, because `VALUE_SENT` will tell the receiver that
// it's okay to access the inner `UnsafeCell`. Immediately after calling
// `set_complete`, if the channel was closed, the sender will _also_
// access the `UnsafeCell` to take the value back out, so if a
// `poll_re... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,521 | 1,580 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:40 | }
fn is_closed(self) -> bool {
self.0 & CLOSED == CLOSED
}
fn set_closed(cell: &AtomicUsize) -> State {
// Acquire because we want all later writes (attempting to poll) to be
// ordered after this.
let val = cell.fetch_or(CLOSED, Acquire);
State(val)
}
fn s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/sync/oneshot.rs | 1,561 | 1,607 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:32 | crate::future::block_on(self)
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
if let Some(inner) = self.inner.as_ref() {
let state = inner.close();
if state.is_complete() {
// SAFETY: we have ensured that the `VALUE_SENT` bit has been set,
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | 0121120b6d91c438ca3a088a286ad78d293eea0b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0121120b6d91c438ca3a088a286ad78d293eea0b/tokio/src/sync/oneshot.rs | 1,241 | 1,300 |
tokio-rs/tokio:tokio/src/sync/oneshot.rs:33 | #[cfg(all(tokio_unstable, feature = "tracing"))]
let res = ready!(trace_poll_op!("poll_recv", inner.poll_recv(cx))).map_err(Into::into);
#[cfg(any(not(tokio_unstable), not(feature = "tracing")))]
let res = ready!(inner.poll_recv(cx)).map_err(Into::into);
res
} e... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/oneshot.rs | MIT | 0121120b6d91c438ca3a088a286ad78d293eea0b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0121120b6d91c438ca3a088a286ad78d293eea0b/tokio/src/sync/oneshot.rs | 1,281 | 1,340 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.