Skip to content

Commit 347d0fb

Browse files
authored
Exposes StartedEvent as public and removes lifetimes (#193)
When this was first written there was concern that we could create memory leaks by allowing the event to be ended without calling _itt_event_end_ptr. However based on documentation at docs/src/ittapi/event-api.rst we've concluded that calling __itt_event_end_ptr does no ptr work as we are actually dealing with integer handles. Closes #188
1 parent 4d27e3c commit 347d0fb

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

rust/ittapi/src/event.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{ffi::CString, marker::PhantomData};
1+
use std::ffi::CString;
22

33
/// See the [Event API] documentation.
44
///
@@ -51,19 +51,18 @@ impl Event {
5151
let result = unsafe { start_fn(self.0) };
5252
assert!(result == 0, "unable to start event");
5353
}
54-
StartedEvent {
55-
event: self.0,
56-
phantom: PhantomData,
57-
}
54+
StartedEvent { event: self.0 }
5855
}
5956
}
6057

61-
pub struct StartedEvent<'a> {
58+
/// Contains the information about a started event.
59+
///
60+
/// Allows for the drop implementation to end the event when it goes out of scope.
61+
pub struct StartedEvent {
6262
event: ittapi_sys::__itt_event,
63-
phantom: PhantomData<&'a mut ()>,
6463
}
6564

66-
impl StartedEvent<'_> {
65+
impl StartedEvent {
6766
/// End the event.
6867
#[allow(clippy::unused_self)]
6968
pub fn end(self) {
@@ -72,7 +71,7 @@ impl StartedEvent<'_> {
7271
}
7372
}
7473

75-
impl Drop for StartedEvent<'_> {
74+
impl Drop for StartedEvent {
7675
fn drop(&mut self) {
7776
if let Some(end_fn) = unsafe { ittapi_sys::__itt_event_end_ptr__3_0 } {
7877
let result = unsafe { end_fn(self.event) };
@@ -91,4 +90,13 @@ mod tests {
9190
let _started_event = event.start();
9291
// Dropping `started_event` ends the event.
9392
}
93+
94+
#[test]
95+
fn double_end() {
96+
let event = Event::new("test");
97+
let s1 = event.start();
98+
let s2 = event.start();
99+
s2.end();
100+
s1.end();
101+
}
94102
}

rust/ittapi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod util;
2020

2121
pub use collection_control::{detach, pause, resume};
2222
pub use domain::Domain;
23-
pub use event::Event;
23+
pub use event::{Event, StartedEvent};
2424
pub use region::{MarkedRegion, Region};
2525
pub use string::StringHandle;
2626
pub use task::Task;

0 commit comments

Comments
 (0)