bennetthardwick / rust-obs-plugins

A safe wrapper around the OBS API, useful for creating OBS sources, filters and effects.
GNU General Public License v2.0
186 stars 34 forks source link

refactor PtrWrapper and add SceneRef #49

Closed clouds56 closed 6 months ago

clouds56 commented 7 months ago
  1. we have from_raw and from_raw_unchecked
    • from_raw for object own/managed in C, a temporary pointer is returned (e.g. obs_filter_get_target).
    • from_raw_unchecked for object own/managed in Rust, will not addref the pointer, in order to handle with thing like obs_data_create().
    • now DataObj<'_>, DataArray<'_>, Properties are impl_ptr_wrapper with no Clone
    • DisplayRef, SourceRef, OutputRef, SceneRef, SceneItemRef are impl_ptr_wrapper
    • ModuleRef is special since it has no release.
  2. now from_raw and from_raw_uncheced would return Option<Self> and convert to nullptr to None.
  3. rename SourceContext to SourceRef, ModuleContext to ModuleRef, in order to keep consistence with other AudioRef, VideoRef. Now Context means state in rust and Ref means a pointer and the state in C. (we could add alias in order to keep backward compatibility)
  4. add .display() to ObsString
  5. add SceneRef and DisplayRef for corresponding obs_scene_t and obs_display_t.
  6. make existing declared using macro native_enum
clouds56 commented 7 months ago

I see now DataObj and DataArray is not cloneable, is it by design? When refactoring from_raw for data.rs, I keep current behavior (no clone), it's easy to make it cloneable as uncomment @addref version of impl_ptr_wrapper

// use this to make `DataObj` clonable since it has `obs_data_addref`.
// which means it's underlying pointer is managed by reference count.
impl_ptr_wrapper!(DataObj<'_>, obs_data_t, @addref: obs_data_addref, obs_data_release);

// when `get_ref` set to `@identity`, it means we could not add reference count for underlying pointer,
// so `impl_ptr_wrapper` would not generate `Clone` trait for this
impl_ptr_wrapper!(DataObj<'_>, obs_data_t, @identity, obs_data_release);
bennetthardwick commented 6 months ago

Awesome work, thank you!

I see now DataObj and DataArray is not cloneable, is it by design?

Probably not by design - likely just being cautious with the underlying pointers. I'm happy for you to choose and go with either version.