mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 19:11:04 +00:00
Merge branch 'master' into governance
This commit is contained in:
@@ -183,6 +183,33 @@ macro_rules! environmental {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
($name:ident : trait $t:ident) => {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
struct $name { __private_field: () }
|
||||||
|
|
||||||
|
thread_local!(static GLOBAL: ::std::cell::RefCell<Option<*mut ($t + 'static)>>
|
||||||
|
= ::std::cell::RefCell::new(None));
|
||||||
|
|
||||||
|
impl $name {
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
|
||||||
|
pub fn using<R, F: FnOnce() -> R>(
|
||||||
|
protected: &mut $t,
|
||||||
|
f: F
|
||||||
|
) -> R {
|
||||||
|
let lifetime_extended = unsafe {
|
||||||
|
::std::mem::transmute::<&mut $t, &mut ($t + 'static)>(protected)
|
||||||
|
};
|
||||||
|
$crate::using(&GLOBAL, lifetime_extended, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with<R, F: for<'a> FnOnce(&'a mut ($t + 'a)) -> R>(
|
||||||
|
f: F
|
||||||
|
) -> Option<R> {
|
||||||
|
$crate::with(&GLOBAL, |x| f(x))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -269,4 +296,23 @@ mod tests {
|
|||||||
|
|
||||||
assert!(was_cleared);
|
assert!(was_cleared);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn use_non_static_trait() {
|
||||||
|
trait Sum { fn sum(&self) -> usize; }
|
||||||
|
impl<'a> Sum for &'a [usize] {
|
||||||
|
fn sum(&self) -> usize {
|
||||||
|
self.iter().fold(0, |a, c| a + c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
environmental!(sum: trait Sum);
|
||||||
|
let numbers = vec![1, 2, 3, 4, 5];
|
||||||
|
let mut numbers = &numbers[..];
|
||||||
|
let got_sum = sum::using(&mut numbers, || {
|
||||||
|
sum::with(|x| x.sum())
|
||||||
|
}).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(got_sum, 15);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user