Skip to main content

FactoryWrapper

Struct FactoryWrapper 

Source
pub struct FactoryWrapper<Raw, F> { /* private fields */ }
Expand description

A wrapper around a clack factory of a given F type, as well as its Raw CLAP representation.

This wrapper allows to safely create a CLAP-compatible factory pointer, as well as soundly casting that pointer back to this wrapper’s type, allowing to access the F type using the handle function.

Implementations§

Source§

impl<Raw, F> FactoryWrapper<Raw, F>

Source

pub const fn new(raw: Raw, inner: F) -> Self

Creates a new factory wrapper from a Raw CLAP type instance and an associated instance of F.

Source

pub fn as_raw(&self) -> RawFactoryPointer<'_, Raw>

Returns a raw factory pointer compatible with this wrapper’s Raw CLAP representation

Source

pub fn as_raw_ptr(&self) -> *const Raw

Returns a raw pointer to the Raw CLAP representation contained in this wrapper.

Source

pub const fn factory(&self) -> &F

Returns a shared reference to the F factory instance.

Source

pub unsafe fn handle<T>( raw: *const Raw, handler: impl FnOnce(&F) -> Result<T, FactoryWrapperError>, ) -> Option<T>

Provides a shared reference to the F factory instance to the given handler closure.

Besides providing a reference, this function does a few extra safety checks:

If any of the above safety check fails, an error message is logged through stderr (as CLAP logging facilities are not available at the factory stage).

Note that some safety checks (e.g. the Raw pointer null-checks) may result in the closure never being called, and an error being returned only. Users of this function must not rely on the completion of this closure for safety, and must handle this function returning None gracefully.

If all goes well, the return value of the handler closure is forwarded and returned by this function.

§Errors

If any safety check failed, or any error or panic occurred inside the handler closure, this function returns None, and the error message is logged.

§Safety

The raw pointer must be created by the as_raw method, from an instance that is still valid for the duration of the handler call.

While this function does a couple of simple safety checks, only a few common cases are actually covered (i.e. null checks), and those must not be relied upon: those checks only exist to help debugging faulty hosts.

§Example

This is the implementation of the plugin_count callback’s C wrapper.

use clap_sys::factory::plugin_factory::clap_plugin_factory;
use clack_plugin::plugin::{Plugin, PluginMainThread};
use clack_plugin::factory::{FactoryWrapper, plugin::PluginFactoryImpl};

unsafe extern "C" fn get_plugin_count<F: PluginFactoryImpl>(factory: *const clap_plugin_factory) -> u32 {
   FactoryWrapper::<clap_plugin_factory, F>::handle(factory, |factory| {
       Ok(factory.plugin_count())
   })
   .unwrap_or(0)
}

Auto Trait Implementations§

§

impl<Raw, F> Freeze for FactoryWrapper<Raw, F>
where Raw: Freeze, F: Freeze,

§

impl<Raw, F> RefUnwindSafe for FactoryWrapper<Raw, F>

§

impl<Raw, F> Send for FactoryWrapper<Raw, F>
where Raw: Send, F: Send,

§

impl<Raw, F> Sync for FactoryWrapper<Raw, F>
where Raw: Sync, F: Sync,

§

impl<Raw, F> Unpin for FactoryWrapper<Raw, F>
where Raw: Unpin, F: Unpin,

§

impl<Raw, F> UnsafeUnpin for FactoryWrapper<Raw, F>
where Raw: UnsafeUnpin, F: UnsafeUnpin,

§

impl<Raw, F> UnwindSafe for FactoryWrapper<Raw, F>
where Raw: UnwindSafe, F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.