pxl_rust/runtime/
stream.rs

1//! Provides control over execution ordering and concurrency via streams.
2//!
3//! This module provides a Rust abstraction around the C++ `pxl::runtime::Stream` class.
4use crate::ffi;
5
6/// Provides control over execution ordering and concurrency via streams.
7///
8/// This struct wraps a FFI pointer to the C++ `pxl::runtime::Stream` class.
9pub struct Stream {
10    /// Pointer to the underlying FFI `Stream` object.
11    inner: *mut ffi::Stream,
12}
13
14/// Implementation of the `Stream` struct.
15impl Stream {
16    /// Creates a new `Stream` wrapper from a raw FFI pointer.
17    /// This is called in runtime wrapper APIs requiring an actual `Stream` object.
18    /// # Arguments
19    /// * `ptr` - A raw pointer to the underlying FFI `Stream` object.
20    /// # Safety
21    /// Caller must ensure that `ptr` is valid and remains valid for the lifetime of `Stream`.
22    pub(crate) fn new() -> Self {
23        let stream_ptr = unsafe { ffi::createStream() };
24        Stream { inner: stream_ptr }
25    }
26
27    /// Destroys the stream and frees associated resources.
28    pub(crate) fn destroy(&mut self) {
29        if !self.inner.is_null() {
30            unsafe { ffi::destroyStream(self.inner) };
31            self.inner = std::ptr::null_mut();
32        }
33    }
34
35    /// Returns a raw pointer to the underlying FFI `Stream` object.
36    /// This is called in runtime wrapper APIs requiring a raw pointer.
37    /// # Safety
38    /// Caller must ensure that FFI object is valid.
39    pub fn get(&self) -> *mut ffi::Stream {
40        self.inner
41    }
42
43    /// Gets the stream ID.
44    ///
45    /// # Returns
46    /// The ID of the stream.
47    /// # Safety
48    /// Caller must ensure that the stream is valid.
49    pub fn stream_id(&self) -> u32 {
50        unsafe { (*self.inner).streamId() }
51    }
52}