libobs_wrapper\data\object/
mod.rs

1use std::fmt::Debug;
2
3use crate::{
4    data::{ImmutableObsData, ObsData, ObsObjectUpdater},
5    macros::trait_with_optional_send_sync,
6    runtime::ObsRuntime,
7    unsafe_send::SmartPointerSendable,
8    utils::{ObsError, ObsString},
9};
10
11mod macros;
12pub(crate) use macros::*;
13
14/// Helper trait to enable cloning boxed outputs.
15pub trait ObsObjectClone<K: Clone> {
16    fn clone_box(&self) -> Box<dyn ObsObjectTrait<K>>;
17}
18
19impl<T, K: Clone> ObsObjectClone<K> for T
20where
21    T: ObsObjectTrait<K> + Clone + 'static,
22{
23    fn clone_box(&self) -> Box<dyn ObsObjectTrait<K>> {
24        Box::new(self.clone())
25    }
26}
27
28impl<K: Clone> Clone for Box<dyn ObsObjectTrait<K>> {
29    fn clone(&self) -> Self {
30        self.clone_box()
31    }
32}
33
34trait_with_optional_send_sync! {
35    #[doc(hidden)]
36    pub trait ObsObjectTraitPrivate: Debug {
37        /// Replaces the settings data of the object. This should only be called if the actual OBS object has been updated.
38        ///
39        /// DO NOT USE THIS METHOD UNLESS YOU KNOW WHAT YOU ARE DOING.
40        fn __internal_replace_settings(&self, settings: ImmutableObsData) -> Result<(), ObsError>;
41        /// Replaces the hotkey data of the object. This should only be called if the actual OBS object has been updated.
42        ///
43        /// DO NOT USE THIS METHOD UNLESS YOU KNOW WHAT YOU ARE DOING.
44        fn __internal_replace_hotkey_data(&self, hotkey_data: ImmutableObsData)
45            -> Result<(), ObsError>;
46    }
47}
48
49#[allow(private_bounds)]
50/// Trait representing an OBS object.
51/// A OBs object has an id, a name, `settings` and `hotkey_data`.
52pub trait ObsObjectTrait<K: Clone>: ObsObjectClone<K> + ObsObjectTraitPrivate {
53    fn runtime(&self) -> &ObsRuntime;
54    fn settings(&self) -> Result<ImmutableObsData, ObsError>;
55    fn hotkey_data(&self) -> Result<ImmutableObsData, ObsError>;
56
57    fn id(&self) -> ObsString;
58    fn name(&self) -> ObsString;
59
60    /// Updates the settings of this output. Fails if active.
61    fn update_settings(&self, settings: ObsData) -> Result<(), ObsError>;
62
63    /// Updates the object with the current settings.
64    /// For examples please take a look at the [Github repository](https://github.com/libobs-rs/libobs-rs/blob/main/examples).
65    fn create_updater<'a, T: ObsObjectUpdater<'a, K, ToUpdate = Self> + Send + Sync>(
66        &'a mut self,
67    ) -> Result<T, ObsError>
68    where
69        Self: Sized + Send + Sync,
70    {
71        let runtime = self.runtime().clone();
72        T::create_update(runtime, self)
73    }
74
75    /// Creates a new reference to the drop guard.
76    /// This is useful if you are using the underlying raw pointer, make sure to store it along the drop guard
77    fn as_ptr(&self) -> SmartPointerSendable<K>;
78}