-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GEN-35: error-stack
implement serde
hooks
#1558
Changes from 33 commits
2885f9b
fb4520b
50d9993
f6b721e
6b82a51
a3167a1
b755a0c
5725e59
724a0a0
3a6f360
5c44c7d
4ae6bd5
e321ff5
3b85638
9f5d32b
bdea88b
4ab9d0b
ddac2d2
05baaaf
1c4871b
1c05422
77f2008
91ced98
845fff8
0abe3db
1d7221d
38ffd86
ceab0d2
1d976d0
aaca87f
b37c9a4
81253a2
43c600b
83edbf2
0941b84
7b39856
efc3db4
3f57310
8c30be3
35fdfe2
28780d3
2ccccfe
f4c30af
491c94b
c528da9
b44832e
cb766d0
47364dd
14506e0
8a7b3fe
1b4db94
cce1439
58b90e8
64077b4
d9b1f78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ type RwLock<T> = std::sync::RwLock<T>; | |
type RwLock<T> = spin::rwlock::RwLock<T>; | ||
|
||
static FMT_HOOK: RwLock<Hooks> = RwLock::new(Hooks { inner: Vec::new() }); | ||
#[cfg(feature = "serde")] | ||
static SERDE_HOOK: RwLock<crate::serde::Hooks> = RwLock::new(crate::serde::Hooks::new()); | ||
|
||
impl Report<()> { | ||
/// Can be used to globally set a [`Debug`] format hook, for a specific type `T`. | ||
|
@@ -181,4 +183,50 @@ impl Report<()> { | |
|
||
closure(&hook) | ||
} | ||
|
||
#[cfg(all(any(feature = "std", feature = "hooks"), feature = "serde"))] | ||
pub fn install_serde_hook<T: serde::Serialize + Send + Sync + 'static>() { | ||
crate::serde::install_builtin_hooks(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, could we also fully qualify the debug hooks or rename the functions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 83edbf2 |
||
|
||
#[cfg(feature = "std")] | ||
let mut lock = SERDE_HOOK.write().expect("should not be poisoned"); | ||
|
||
// The spin RwLock cannot panic | ||
#[cfg(all(not(feature = "std"), feature = "hooks"))] | ||
let mut lock = SERDE_HOOK.write(); | ||
|
||
lock.insert_static::<T>(); | ||
} | ||
|
||
#[cfg(all(any(feature = "std", feature = "hooks"), feature = "serde"))] | ||
pub fn install_custom_serde_hook<F, T>(closure: F) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 7b39856 |
||
where | ||
F: for<'a> crate::serde::DynamicFn<'a, T>, | ||
T: Send + Sync + 'static, | ||
{ | ||
crate::serde::install_builtin_hooks(); | ||
|
||
#[cfg(feature = "std")] | ||
let mut lock = SERDE_HOOK.write().expect("should not be poisoned"); | ||
|
||
// The spin RwLock cannot panic | ||
#[cfg(all(not(feature = "std"), feature = "hooks"))] | ||
let mut lock = SERDE_HOOK.write(); | ||
|
||
lock.insert_dynamic(closure); | ||
} | ||
|
||
#[cfg(all(any(feature = "std", feature = "hooks"), feature = "serde"))] | ||
pub(crate) fn invoke_serde_hook<T>(closure: impl FnOnce(&crate::serde::Hooks) -> T) -> T { | ||
crate::serde::install_builtin_hooks(); | ||
|
||
#[cfg(feature = "std")] | ||
let hook = SERDE_HOOK.read().expect("should not be poisoned"); | ||
|
||
// The spin RwLock cannot panic | ||
#[cfg(all(not(feature = "std"), feature = "hooks"))] | ||
let hook = SERDE_HOOK.read(); | ||
|
||
closure(&hook) | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably also fully qualify the path to
Hooks
here as well or import them asFmtHooks
. Actually, as they are internal only, I'd prefer renaming bothHooks
structs toFmtHooks
andSerdeHooks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 83edbf2