Sha256: 16bfa1da16baa831cc926c41df869b99bfbc6d96cf39a04aa4914a9e4b19a2f0

Contents?: true

Size: 1.16 KB

Versions: 30

Compression:

Stored size: 1.16 KB

Contents

use std::cell::Cell;
use std::ptr;

/// Scoped thread-local storage
pub(super) struct Scoped<T> {
    pub(super) inner: Cell<*const T>,
}

impl<T> Scoped<T> {
    pub(super) const fn new() -> Scoped<T> {
        Scoped {
            inner: Cell::new(ptr::null()),
        }
    }

    /// Inserts a value into the scoped cell for the duration of the closure
    pub(super) fn set<F, R>(&self, t: &T, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        struct Reset<'a, T> {
            cell: &'a Cell<*const T>,
            prev: *const T,
        }

        impl<T> Drop for Reset<'_, T> {
            fn drop(&mut self) {
                self.cell.set(self.prev);
            }
        }

        let prev = self.inner.get();
        self.inner.set(t as *const _);

        let _reset = Reset {
            cell: &self.inner,
            prev,
        };

        f()
    }

    /// Gets the value out of the scoped cell;
    pub(super) fn with<F, R>(&self, f: F) -> R
    where
        F: FnOnce(Option<&T>) -> R,
    {
        let val = self.inner.get();

        if val.is_null() {
            f(None)
        } else {
            unsafe { f(Some(&*val)) }
        }
    }
}

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/tokio-1.43.0/src/runtime/context/scoped.rs
wasmtime-28.0.0 ./ext/cargo-vendor/tokio-1.43.0/src/runtime/context/scoped.rs
wasmtime-27.0.0 ./ext/cargo-vendor/tokio-1.41.1/src/runtime/context/scoped.rs
wasmtime-26.0.0 ./ext/cargo-vendor/tokio-1.41.0/src/runtime/context/scoped.rs
wasmtime-25.0.2 ./ext/cargo-vendor/tokio-1.40.0/src/runtime/context/scoped.rs
wasmtime-25.0.1 ./ext/cargo-vendor/tokio-1.39.3/src/runtime/context/scoped.rs
wasmtime-25.0.0 ./ext/cargo-vendor/tokio-1.39.3/src/runtime/context/scoped.rs
wasmtime-24.0.0 ./ext/cargo-vendor/tokio-1.39.3/src/runtime/context/scoped.rs
wasmtime-23.0.2 ./ext/cargo-vendor/tokio-1.36.0/src/runtime/context/scoped.rs
wasmtime-22.0.0 ./ext/cargo-vendor/tokio-1.36.0/src/runtime/context/scoped.rs
wasmtime-21.0.1 ./ext/cargo-vendor/tokio-1.36.0/src/runtime/context/scoped.rs
wasmtime-20.0.2 ./ext/cargo-vendor/tokio-1.36.0/src/runtime/context/scoped.rs
wasmtime-20.0.0 ./ext/cargo-vendor/tokio-1.36.0/src/runtime/context/scoped.rs
wasmtime-18.0.3 ./ext/cargo-vendor/tokio-1.36.0/src/runtime/context/scoped.rs
wasmtime-17.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/runtime/context/scoped.rs
wasmtime-17.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/runtime/context/scoped.rs
wasmtime-16.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/runtime/context/scoped.rs
wasmtime-15.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/runtime/context/scoped.rs
wasmtime-15.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/runtime/context/scoped.rs
wasmtime-14.0.4 ./ext/cargo-vendor/tokio-1.33.0/src/runtime/context/scoped.rs