Vendor things

This commit is contained in:
John Doty 2024-03-08 11:03:01 -08:00
parent 5deceec006
commit 977e3c17e5
19434 changed files with 10682014 additions and 0 deletions

View file

@ -0,0 +1 @@
{"files":{"Cargo.toml":"c2eba2f5e3601337017688c0483374a00c7e5196f740189f1aa19e28055c9920","README.md":"f1c1a511432c2c3fc897ddc54b1bc6a3a8ba93301b6110101c11695cad14e3c9","src/lib.rs":"bbc9932593ac8e1352209a6dadc0db1ce333c9e4d977e17bfc19aee09e87536c"},"package":"794edcc9b3fb07bb4aecaa11f093fd45663b4feadb782d68303a2268bc2701de"}

View file

@ -0,0 +1,28 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
name = "better_scoped_tls"
version = "0.1.1"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "scoped-tls, but with good error message"
documentation = "https://rustdoc.swc.rs/better_scoped_tls/"
readme = "README.md"
license = "Apache-2.0"
repository = "https://github.com/swc-project/swc.git"
resolver = "1"
[lib]
bench = false
[dependencies.scoped-tls]
version = "1.0.1"

View file

@ -0,0 +1,11 @@
# better_scoped_tls
This crate provides an opinionated version of [scoped-tls](https://docs.rs/scoped-tls/1.0.0/scoped_tls/index.html).
Scoped thread local variables created by this crate will panic with a good message on usage without `.set`, like
```
You should perform this operation in the closure passed to `set` of better_scoped_tls::tests::TESTTLS
```
Syntax is exactly same to the original scoped-tls.

View file

@ -0,0 +1,109 @@
//! Better scoped thread local storage.
#[doc(hidden)]
pub extern crate scoped_tls;
/// See [scoped_tls::scoped_thread_local] for actual documentation.
///
/// This is identical as [scoped_tls::scoped_thread_local] on release builds.
#[macro_export]
macro_rules! scoped_tls {
($(#[$attrs:meta])* $vis:vis static $name:ident: $ty:ty) => {
$crate::scoped_tls::scoped_thread_local!(
static INNER: $ty
);
$(#[$attrs])*
$vis static $name: $crate::ScopedKey<$ty> = $crate::ScopedKey {
inner: &INNER,
#[cfg(debug_assertions)]
module_path: module_path!(),
#[cfg(debug_assertions)]
name: stringify!($name),
};
};
}
/// Wrapper for [scoped_tls::ScopedKey] with better error messages.
pub struct ScopedKey<T>
where
T: 'static,
{
#[doc(hidden)]
pub inner: &'static scoped_tls::ScopedKey<T>,
#[cfg(debug_assertions)]
#[doc(hidden)]
pub module_path: &'static str,
#[cfg(debug_assertions)]
#[doc(hidden)]
pub name: &'static str,
}
impl<T> ScopedKey<T>
where
T: 'static,
{
/// See [scoped_tls::ScopedKey] for actual documentation.
#[cfg_attr(not(debug_assertions), inline(always))]
pub fn set<F, R>(&'static self, t: &T, f: F) -> R
where
F: FnOnce() -> R,
{
self.inner.set(t, f)
}
/// See [scoped_tls::ScopedKey] for actual documentation.
#[cfg_attr(not(debug_assertions), inline(always))]
pub fn with<F, R>(&'static self, f: F) -> R
where
F: FnOnce(&T) -> R,
{
#[cfg(debug_assertions)]
if !self.inner.is_set() {
// Override panic message
panic!(
"You should perform this operation in the closure passed to `set` of {}::{}",
self.module_path, self.name
)
}
self.inner.with(f)
}
/// See [scoped_tls::ScopedKey] for actual documentation.
#[cfg_attr(not(debug_assertions), inline(always))]
pub fn is_set(&'static self) -> bool {
self.inner.is_set()
}
}
#[cfg(test)]
mod tests {
scoped_tls!(
pub static TESTTLS: String
);
#[cfg(debug_assertions)]
#[test]
#[should_panic = "You should perform this operation in the closure passed to `set` of \
better_scoped_tls::tests::TESTTLS"]
fn panic_on_with() {
TESTTLS.with(|s| {
println!("S: {}", s);
})
}
#[test]
fn valid() {
TESTTLS.set(&String::new(), || {
TESTTLS.with(|s| {
assert_eq!(*s, String::new());
})
})
}
}