1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::context::Context;
use crate::error::{to_option_error, SteamAudioError};
#[cfg(doc)]
use crate::geometry::Scene;
#[cfg(doc)]
use crate::probe::ProbeBatch;
/// A serialized representation of an API object, like a [`Scene`] or [`ProbeBatch`].
///
/// Create an empty serialized object if you want to serialize an existing object to a byte array, or create a serialized object that wraps an existing byte array if you want to deserialize it.
#[derive(Debug)]
pub struct SerializedObject(pub(crate) audionimbus_sys::IPLSerializedObject);
impl SerializedObject {
/// Creates a new empty serialized object for serialization purposes.
///
/// # Errors
///
/// Returns an error if the underlying Steam Audio library fails to create the serialized object.
///
/// # Examples
///
/// ```
/// # use audionimbus::{Context, SerializedObject, SteamAudioError};
/// let context = Context::default();
/// let serialized_object = SerializedObject::try_new(&context)?;
/// # Ok::<(), audionimbus::SteamAudioError>(())
/// ```
pub fn try_new(context: &Context) -> Result<Self, SteamAudioError> {
let serialized_object_settings = audionimbus_sys::IPLSerializedObjectSettings {
data: std::ptr::null_mut(),
size: 0,
};
Self::try_with_settings(context, serialized_object_settings)
}
/// Creates a serialized object that wraps an existing byte buffer for deserialization.
///
/// # Errors
///
/// Returns an error if the underlying Steam Audio library fails to create the serialized
/// object or if the buffer contains invalid data.
///
/// # Examples
///
/// ```
/// # use audionimbus::{Context, SerializedObject, SteamAudioError};
/// let context = Context::default();
/// let mut buffer = vec![0u8; 1024]; // Load your serialized data here.
/// let serialized_object = SerializedObject::try_with_buffer(&context, &mut buffer)?;
/// # Ok::<(), audionimbus::SteamAudioError>(())
/// ```
pub fn try_with_buffer(
context: &Context,
buffer: &mut Vec<u8>,
) -> Result<Self, SteamAudioError> {
let serialized_object_settings = audionimbus_sys::IPLSerializedObjectSettings {
data: buffer.as_mut_ptr().cast::<audionimbus_sys::IPLbyte>(),
size: buffer.len(),
};
Self::try_with_settings(context, serialized_object_settings)
}
/// Creates a serialized object with the given settings.
///
/// # Errors
///
/// Returns an error if the underlying Steam Audio library fails to create the object.
fn try_with_settings(
context: &Context,
mut serialized_object_settings: audionimbus_sys::IPLSerializedObjectSettings,
) -> Result<Self, SteamAudioError> {
let mut serialized_object = Self(std::ptr::null_mut());
let status = unsafe {
audionimbus_sys::iplSerializedObjectCreate(
context.raw_ptr(),
&raw mut serialized_object_settings,
serialized_object.raw_ptr_mut(),
)
};
if let Some(error) = to_option_error(status) {
return Err(error);
}
Ok(serialized_object)
}
/// Returns the raw FFI pointer to the underlying object.
///
/// This is intended for internal use and advanced scenarios.
pub const fn raw_ptr(&self) -> audionimbus_sys::IPLSerializedObject {
self.0
}
/// Returns a mutable reference to the raw FFI pointer.
///
/// This is intended for internal use and advanced scenarios.
pub const fn raw_ptr_mut(&mut self) -> &mut audionimbus_sys::IPLSerializedObject {
&mut self.0
}
/// Extracts the serialized data as a byte vector.
///
/// This method retrieves the underlying serialized data and copies it into a new
/// `Vec<u8>`. Use this to extract serialized data that can be saved to a file or
/// transmitted over the network.
///
/// # Returns
///
/// A `Vec<u8>` containing a copy of the serialized data.
///
/// # Examples
///
/// ```
/// # use audionimbus::{Context, SerializedObject, Scene};
/// # let context = Context::default();
/// # let serialized_object = SerializedObject::try_new(&context)?;
/// // After serializing some object into serialized_object...
/// let bytes = serialized_object.to_vec();
/// # Ok::<(), audionimbus::SteamAudioError>(())
/// ```
pub fn to_vec(&self) -> Vec<u8> {
let raw_ptr = self.raw_ptr();
let data_ptr = unsafe { audionimbus_sys::iplSerializedObjectGetData(raw_ptr) };
let size = unsafe { audionimbus_sys::iplSerializedObjectGetSize(raw_ptr) } as usize;
if data_ptr.is_null() || size == 0 {
return Vec::new();
}
let data_slice = unsafe { std::slice::from_raw_parts(data_ptr, size) };
data_slice.to_vec()
}
}
impl Drop for SerializedObject {
fn drop(&mut self) {
unsafe { audionimbus_sys::iplSerializedObjectRelease(&raw mut self.0) }
}
}
unsafe impl Send for SerializedObject {}
unsafe impl Sync for SerializedObject {}
impl Clone for SerializedObject {
/// Retains an additional reference to the serialized object.
///
/// The returned [`SerializedObject`] shares the same underlying Steam Audio object.
fn clone(&self) -> Self {
// SAFETY: The serialized object will not be destroyed until all references are released.
Self(unsafe { audionimbus_sys::iplSerializedObjectRetain(self.0) })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_try_new() {
let context = Context::default();
let serialized_object = SerializedObject::try_new(&context);
assert!(serialized_object.is_ok());
}
#[test]
fn test_try_with_buffer() {
let context = Context::default();
let mut buffer = vec![0u8; 1024];
let serialized_object = SerializedObject::try_with_buffer(&context, &mut buffer);
assert!(serialized_object.is_ok());
}
#[test]
fn test_clone() {
let context = Context::default();
let serialized_object = SerializedObject::try_new(&context).unwrap();
let clone = serialized_object.clone();
assert_eq!(serialized_object.raw_ptr(), clone.raw_ptr());
drop(serialized_object);
assert!(!clone.raw_ptr().is_null());
}
}