Skip to content

Commit 82e212f

Browse files
authored
ref: Avoid double-indirection through Arc<Box<T>> (#380)
This also removes the need to manually box the argument to `add_event_processor`.
1 parent d54c912 commit 82e212f

File tree

5 files changed

+15
-17
lines changed

5 files changed

+15
-17
lines changed

Diff for: sentry-actix/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,7 @@ where
214214
let (tx, sentry_req) = sentry_request_from_http(&req, with_pii);
215215
hub.configure_scope(|scope| {
216216
scope.set_transaction(tx.as_deref());
217-
scope.add_event_processor(Box::new(move |event| {
218-
Some(process_event(event, &sentry_req))
219-
}))
217+
scope.add_event_processor(move |event| Some(process_event(event, &sentry_req)))
220218
});
221219

222220
let fut = self.service.call(req).bind_hub(hub.clone());

Diff for: sentry-core/src/scope/noop.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ impl Scope {
9595
}
9696

9797
/// Add an event processor to the scope.
98-
pub fn add_event_processor(
99-
&mut self,
100-
f: Box<dyn Fn(Event<'static>) -> Option<Event<'static>> + Send + Sync>,
101-
) {
98+
pub fn add_event_processor<F>(&mut self, f: F)
99+
where
100+
F: Fn(Event<'static>) -> Option<Event<'static>> + Send + Sync + 'static,
101+
{
102102
let _f = f;
103103
minimal_unreachable!();
104104
}

Diff for: sentry-core/src/scope/real.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Stack {
1212
layers: Vec<StackLayer>,
1313
}
1414

15-
pub type EventProcessor = Box<dyn Fn(Event<'static>) -> Option<Event<'static>> + Send + Sync>;
15+
pub type EventProcessor = Arc<dyn Fn(Event<'static>) -> Option<Event<'static>> + Send + Sync>;
1616

1717
/// Holds contextual data for the current scope.
1818
///
@@ -42,7 +42,7 @@ pub struct Scope {
4242
pub(crate) extra: Arc<HashMap<String, Value>>,
4343
pub(crate) tags: Arc<HashMap<String, String>>,
4444
pub(crate) contexts: Arc<HashMap<String, Context>>,
45-
pub(crate) event_processors: Arc<Vec<Arc<EventProcessor>>>,
45+
pub(crate) event_processors: Arc<Vec<EventProcessor>>,
4646
pub(crate) session: Arc<Mutex<Option<Session>>>,
4747
}
4848

@@ -215,10 +215,10 @@ impl Scope {
215215
}
216216

217217
/// Add an event processor to the scope.
218-
pub fn add_event_processor(
219-
&mut self,
220-
f: Box<dyn Fn(Event<'static>) -> Option<Event<'static>> + Send + Sync>,
221-
) {
218+
pub fn add_event_processor<F>(&mut self, f: F)
219+
where
220+
F: Fn(Event<'static>) -> Option<Event<'static>> + Send + Sync + 'static,
221+
{
222222
Arc::make_mut(&mut self.event_processors).push(Arc::new(f));
223223
}
224224

Diff for: sentry/examples/event-processors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ fn main() {
22
let _sentry = sentry::init(());
33

44
sentry::configure_scope(|scope| {
5-
scope.add_event_processor(Box::new(move |mut event| {
5+
scope.add_event_processor(|mut event| {
66
event.request = Some(sentry::protocol::Request {
77
url: Some("https://example.com/".parse().unwrap()),
88
method: Some("GET".into()),
99
..Default::default()
1010
});
1111
Some(event)
12-
}));
12+
});
1313
});
1414

1515
sentry::configure_scope(|scope| {

Diff for: sentry/tests/test_processors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ fn test_event_processors() {
77
let events = sentry::test::with_captured_events(|| {
88
sentry::configure_scope(|scope| {
99
scope.set_tag("worker", "worker1");
10-
scope.add_event_processor(Box::new(move |mut event| {
10+
scope.add_event_processor(|mut event| {
1111
event.user = Some(sentry::User {
1212
email: Some("[email protected]".into()),
1313
..Default::default()
1414
});
1515
Some(event)
16-
}));
16+
});
1717
});
1818
sentry::capture_message("Hello World!", sentry::Level::Warning);
1919
});

0 commit comments

Comments
 (0)