pipewire/registry/box_.rs
1// Copyright The pipewire-rs Contributors.
2// SPDX-License-Identifier: MIT
3
4use std::{marker::PhantomData, ops::Deref, ptr};
5
6use crate::core::Core;
7
8use super::Registry;
9
10/// Smart pointer providing unique ownership of a PipeWire [registry](super).
11///
12/// For shared ownership, see [`RegistryRc`](super::RegistryRc).
13///
14/// For an explanation of these, see [Smart pointers to PipeWire objects](crate#smart-pointers-to-pipewire-objects).
15#[derive(Debug)]
16pub struct RegistryBox<'c> {
17 ptr: ptr::NonNull<pw_sys::pw_registry>,
18 core: PhantomData<&'c Core>,
19}
20
21impl<'c> RegistryBox<'c> {
22 /// Create a `RegistryBox` by taking ownership of a raw `pw_registry`.
23 ///
24 /// # Safety
25 /// The provided pointer must point to a valid, well aligned [`pw_registry`](`pw_sys::pw_registry`).
26 ///
27 /// The raw registry must not be manually destroyed or moved, as the new [`RegistryBox`] takes
28 /// ownership of it.
29 ///
30 /// The lifetime of the returned box is unbounded. The caller is responsible to make sure
31 /// that the core used with this registry outlives the registry.
32 pub unsafe fn from_raw(ptr: ptr::NonNull<pw_sys::pw_registry>) -> Self {
33 Self {
34 ptr,
35 core: PhantomData,
36 }
37 }
38}
39
40impl<'c> std::ops::Deref for RegistryBox<'c> {
41 type Target = Registry;
42
43 fn deref(&self) -> &Self::Target {
44 unsafe { self.ptr.cast::<Registry>().as_ref() }
45 }
46}
47
48impl<'c> AsRef<Registry> for RegistryBox<'c> {
49 fn as_ref(&self) -> &Registry {
50 self.deref()
51 }
52}
53
54impl<'c> Drop for RegistryBox<'c> {
55 fn drop(&mut self) {
56 unsafe {
57 pw_sys::pw_proxy_destroy(self.as_raw_ptr().cast());
58 }
59 }
60}