From 2ff0d1f9bf7161cffaf9d05036e277737e53381b Mon Sep 17 00:00:00 2001 From: Johnnie Birch Date: Thu, 4 Dec 2025 14:40:43 -0800 Subject: [PATCH] Exposes StartedEvent as public and removes lifetimes 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 --- rust/ittapi/src/event.rs | 26 +++++++++++++++++--------- rust/ittapi/src/lib.rs | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/rust/ittapi/src/event.rs b/rust/ittapi/src/event.rs index 76daa719..6a95cc19 100644 --- a/rust/ittapi/src/event.rs +++ b/rust/ittapi/src/event.rs @@ -1,4 +1,4 @@ -use std::{ffi::CString, marker::PhantomData}; +use std::ffi::CString; /// See the [Event API] documentation. /// @@ -51,19 +51,18 @@ impl Event { let result = unsafe { start_fn(self.0) }; assert!(result == 0, "unable to start event"); } - StartedEvent { - event: self.0, - phantom: PhantomData, - } + StartedEvent { event: self.0 } } } -pub struct StartedEvent<'a> { +/// Contains the information about a started event. +/// +/// Allows for the drop implementation to end the event when it goes out of scope. +pub struct StartedEvent { event: ittapi_sys::__itt_event, - phantom: PhantomData<&'a mut ()>, } -impl StartedEvent<'_> { +impl StartedEvent { /// End the event. #[allow(clippy::unused_self)] pub fn end(self) { @@ -72,7 +71,7 @@ impl StartedEvent<'_> { } } -impl Drop for StartedEvent<'_> { +impl Drop for StartedEvent { fn drop(&mut self) { if let Some(end_fn) = unsafe { ittapi_sys::__itt_event_end_ptr__3_0 } { let result = unsafe { end_fn(self.event) }; @@ -91,4 +90,13 @@ mod tests { let _started_event = event.start(); // Dropping `started_event` ends the event. } + + #[test] + fn double_end() { + let event = Event::new("test"); + let s1 = event.start(); + let s2 = event.start(); + s2.end(); + s1.end(); + } } diff --git a/rust/ittapi/src/lib.rs b/rust/ittapi/src/lib.rs index 32e841e8..b693d0a8 100644 --- a/rust/ittapi/src/lib.rs +++ b/rust/ittapi/src/lib.rs @@ -20,7 +20,7 @@ mod util; pub use collection_control::{detach, pause, resume}; pub use domain::Domain; -pub use event::Event; +pub use event::{Event, StartedEvent}; pub use region::{MarkedRegion, Region}; pub use string::StringHandle; pub use task::Task;