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#[derive(Debug)]
11pub struct RegistryBox<'c> {
12    ptr: ptr::NonNull<pw_sys::pw_registry>,
13    core: PhantomData<&'c Core>,
14}
15
16impl<'c> RegistryBox<'c> {
17    /// Create a `RegistryBox` by taking ownership of a raw `pw_registry`.
18    ///
19    /// # Safety
20    /// The provided pointer must point to a valid, well aligned [`pw_registry`](`pw_sys::pw_registry`).
21    ///
22    /// The raw registry must not be manually destroyed or moved, as the new [`RegistryBox`] takes
23    /// ownership of it.
24    ///
25    /// The lifetime of the returned box is unbounded. The caller is responsible to make sure
26    /// that the core used with this registry outlives the registry.
27    pub unsafe fn from_raw(ptr: ptr::NonNull<pw_sys::pw_registry>) -> Self {
28        Self {
29            ptr,
30            core: PhantomData,
31        }
32    }
33}
34
35impl<'c> std::ops::Deref for RegistryBox<'c> {
36    type Target = Registry;
37
38    fn deref(&self) -> &Self::Target {
39        unsafe { self.ptr.cast::<Registry>().as_ref() }
40    }
41}
42
43impl<'c> AsRef<Registry> for RegistryBox<'c> {
44    fn as_ref(&self) -> &Registry {
45        self.deref()
46    }
47}
48
49impl<'c> Drop for RegistryBox<'c> {
50    fn drop(&mut self) {
51        unsafe {
52            pw_sys::pw_proxy_destroy(self.as_raw_ptr().cast());
53        }
54    }
55}