Skip to content

Commit

Permalink
[illicit] Guard returned from lookups impls Debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
anp committed Aug 9, 2020
1 parent 8e2b67b commit 724740a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ edition = "2018"
[dependencies]
dyn-cache = { path = "dyn-cache", version = "0.11.0"}
futures = "0.3.5"
illicit = { path = "illicit", version = "1.1.0"}
illicit = { path = "illicit", version = "1.1.1"}
parking_lot = "0.11"
scopeguard = "1"
topo = { path = "topo", version = "0.13.0"}
Expand Down
2 changes: 1 addition & 1 deletion dom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ webdom = ["augdom/webdom", "raf", "wasm-bindgen-futures"]
[dependencies]
augdom = { path = "augdom", version = "0.2.0-pre"}
futures = "0.3.5"
illicit = { path = "../illicit", version = "1.1.0"}
illicit = { path = "../illicit", version = "1.1.1"}
moxie = { path = "../", version = "0.3.0-pre"}
paste = "1.0.0"
scopeguard = "1"
Expand Down
2 changes: 1 addition & 1 deletion dyn-cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ edition = "2018"
downcast-rs = "1.1.1"
hash_hasher = "2.0.3"
hashbrown = "0.8.0"
illicit = { path = "../illicit", version = "1.1.0"}
illicit = { path = "../illicit", version = "1.1.1"}
parking_lot = "0.11.0"
paste = "1.0.0"

Expand Down
2 changes: 1 addition & 1 deletion illicit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "illicit"
version = "1.1.0"
version = "1.1.1"
description = "An implicit thread-local environment which is indexed by type."
categories = ["rust-patterns"]
keywords = ["context", "environment", "global", "singleton"]
Expand Down
4 changes: 3 additions & 1 deletion illicit/src/anon_rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ impl AnonRc {
}
}

pub(crate) fn downcast_deref<T: 'static>(self) -> Option<impl Deref<Target = T> + 'static> {
pub(crate) fn downcast_deref<T: Debug + 'static>(
self,
) -> Option<impl Deref<Target = T> + Debug + 'static> {
OwningRef::new(self.inner)
.try_map(|anon| {
let res: Result<&T, &str> = anon.downcast_ref().ok_or("invalid cast");
Expand Down
35 changes: 16 additions & 19 deletions illicit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ thread_local! {
/// ```
/// assert!(illicit::get::<String>().is_err());
/// ```
pub fn get<E>() -> Result<impl Deref<Target = E> + 'static, GetFailed>
pub fn get<E>() -> Result<impl Deref<Target = E> + Debug + 'static, GetFailed>
where
E: Any + 'static,
E: Any + Debug + 'static,
{
let key = TypeId::of::<E>();
let anon = CURRENT_SCOPE.with(|current| {
Expand Down Expand Up @@ -279,7 +279,7 @@ where
#[track_caller]
pub fn expect<E>() -> impl Deref<Target = E> + 'static
where
E: Any + 'static,
E: Any + Debug + 'static,
{
get().unwrap()
}
Expand Down Expand Up @@ -351,7 +351,16 @@ pub struct Layer {
impl Default for Layer {
#[track_caller]
fn default() -> Self {
Self::new()
let mut values = Vec::new();
let mut depth = 0;

CURRENT_SCOPE.with(|current| {
let current = current.borrow();
depth = current.depth + 1;
values = current.values.clone();
});

Self { values, depth }
}
}

Expand All @@ -368,16 +377,7 @@ impl Layer {
/// values.
#[track_caller]
pub fn new() -> Self {
let mut values = Vec::new();
let mut depth = 0;

CURRENT_SCOPE.with(|current| {
let current = current.borrow();
depth = current.depth + 1;
values = current.values.clone();
});

Self { values, depth }
Self::default()
}

/// Adds the new item and returns the modified layer.
Expand Down Expand Up @@ -589,11 +589,8 @@ mod tests {

#[test]
fn failure_error() {
if let Err(e) = get::<u8>() {
assert_display_snapshot!(e);
} else {
panic!("got a u8 from the environment when we shouldn't have");
}
let e = get::<u8>().unwrap_err();
assert_display_snapshot!(e);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion topo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2018"

[dependencies]
dyn-cache = { path = "../dyn-cache", version = "0.11.0"}
illicit = { path = "../illicit", version = "1.1.0"}
illicit = { path = "../illicit", version = "1.1.1"}
once_cell = "1.4.0"
parking_lot = "0.11.0"
topo-macro = { path = "macro", version = "0.10.0"}
Expand Down

0 comments on commit 724740a

Please sign in to comment.