Skip to main content

lv2_urid/
feature.rs

1//! Thin but safe wrappers for the URID mapping features.
2use core::feature::Feature;
3use core::prelude::*;
4use std::ffi::c_void;
5use urid::*;
6
7/// Host feature to map URIs to integers
8#[repr(transparent)]
9pub struct LV2Map<'a> {
10    internal: &'a sys::LV2_URID_Map,
11}
12
13unsafe impl<'a> UriBound for LV2Map<'a> {
14    const URI: &'static [u8] = sys::LV2_URID_MAP_URI;
15}
16
17unsafe impl<'a> Feature for LV2Map<'a> {
18    unsafe fn from_feature_ptr(feature: *const c_void, class: ThreadingClass) -> Option<Self> {
19        if class != ThreadingClass::Audio {
20            (feature as *const sys::LV2_URID_Map)
21                .as_ref()
22                .map(|internal| Self { internal })
23        } else {
24            panic!("The URID mapping feature isn't allowed in the audio threading class");
25        }
26    }
27}
28
29impl<'a> LV2Map<'a> {
30    pub fn new(internal: &'a sys::LV2_URID_Map) -> Self {
31        Self { internal }
32    }
33}
34
35impl<'a> Map for LV2Map<'a> {
36    fn map_uri(&self, uri: &Uri) -> Option<URID> {
37        let uri = uri.as_ptr();
38        let urid = unsafe { (self.internal.map.unwrap())(self.internal.handle, uri) };
39        URID::new(urid)
40    }
41}
42
43/// Host feature to revert the URI -> URID mapping.
44#[repr(transparent)]
45pub struct LV2Unmap<'a> {
46    internal: &'a sys::LV2_URID_Unmap,
47}
48
49unsafe impl<'a> UriBound for LV2Unmap<'a> {
50    const URI: &'static [u8] = sys::LV2_URID_UNMAP_URI;
51}
52
53unsafe impl<'a> Feature for LV2Unmap<'a> {
54    unsafe fn from_feature_ptr(feature: *const c_void, class: ThreadingClass) -> Option<Self> {
55        if class != ThreadingClass::Audio {
56            (feature as *const sys::LV2_URID_Unmap)
57                .as_ref()
58                .map(|internal| Self { internal })
59        } else {
60            panic!("The URID unmapping feature isn't allowed in the audio threading class");
61        }
62    }
63}
64
65impl<'a> LV2Unmap<'a> {
66    pub fn new(internal: &'a sys::LV2_URID_Unmap) -> Self {
67        Self { internal }
68    }
69}
70
71impl<'a> Unmap for LV2Unmap<'a> {
72    fn unmap<T: ?Sized>(&self, urid: URID<T>) -> Option<&Uri> {
73        let uri_ptr = unsafe { (self.internal.unmap.unwrap())(self.internal.handle, urid.get()) };
74        if uri_ptr.is_null() {
75            None
76        } else {
77            Some(unsafe { Uri::from_ptr(uri_ptr) })
78        }
79    }
80}