lv2-core-derive-2.1.1/.cargo_vcs_info.json0000644000000001120000000000000137330ustar { "git": { "sha1": "43c43f8ac6bc180b170acc5fb86b45d2f1ee9620" } } lv2-core-derive-2.1.1/Cargo.toml0000644000000021610000000000000117370ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "lv2-core-derive" version = "2.1.1" authors = ["Jan-Oliver 'Janonard' Opdenhövel ", "Adrien Prokopowicz "] description = "Procedural macros for lv2-core" readme = "README.md" license = "MIT OR Apache-2.0" repository = "https://github.com/RustAudio/rust-lv2" [lib] proc-macro = true [dependencies.proc-macro2] version = "1.0.24" [dependencies.quote] version = "1.0.2" [dependencies.syn] version = "1.0.5" [badges.maintenance] status = "passively-maintained" [badges.travis-ci] branch = "master" repository = "RustAudio/rust-lv2" lv2-core-derive-2.1.1/Cargo.toml.orig000064400000000000000000000010620000000000000153750ustar 00000000000000[package] name = "lv2-core-derive" version = "2.1.1" authors = ["Jan-Oliver 'Janonard' Opdenhövel ", "Adrien Prokopowicz "] license = "MIT OR Apache-2.0" description = "Procedural macros for lv2-core" readme = "README.md" repository = "https://github.com/RustAudio/rust-lv2" [badges] travis-ci = { repository = "RustAudio/rust-lv2", branch = "master" } maintenance = { status = "passively-maintained" } [lib] proc-macro = true [dependencies] syn = "1.0.5" quote = "1.0.2" proc-macro2 = "1.0.24" lv2-core-derive-2.1.1/LICENSE-APACHE000064400000000000000000000251370000000000000144430ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. lv2-core-derive-2.1.1/LICENSE-MIT000064400000000000000000000017760000000000000141560ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.lv2-core-derive-2.1.1/README.md000064400000000000000000000013300000000000000137630ustar 00000000000000# Procedural macros for lv2-core This crate includes several produral macros used by the [lv2-core](https://crates.io/crates/lv2-core) crate. ## Documentation The original LV2 API (in the `C` programming language) is documented by ["the LV2 book"](https://lv2plug.in/book/). This book is in the process of being translated to Rust along with the development of `rust-lv2` [(link)](https://janonard.github.io/rust-lv2-book/) and describes how to properly use `rust-lv2`. ## License Licensed under either of * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option.lv2-core-derive-2.1.1/src/feature_collection_derive.rs000064400000000000000000000046770000000000000210660ustar 00000000000000use proc_macro::TokenStream; use proc_macro2::Span; use syn::Field; use syn::{parse_macro_input, Data, DataStruct, Ident}; use syn::{DeriveInput, Generics, Lifetime}; struct FeatureCollectionField<'a> { identifier: &'a Ident, } impl<'a> FeatureCollectionField<'a> { fn from_input_field(input: &'a Field) -> Self { FeatureCollectionField { identifier: input.ident.as_ref().unwrap(), } } fn make_retrieval(&self) -> impl ::quote::ToTokens { let identifier = self.identifier; quote! {#identifier: cache.retrieve_feature(class)?,} } } struct FeatureCollectionStruct<'a> { struct_name: &'a Ident, generics: &'a Generics, fields: Vec>, } impl<'a> FeatureCollectionStruct<'a> { fn from_derive_input(input: &'a DeriveInput) -> Self { let fields = match &input.data { Data::Struct(DataStruct { fields, .. }) => fields .iter() .map(FeatureCollectionField::from_input_field) .collect(), _ => panic!("Only structs can implement `FeatureCollection`"), }; FeatureCollectionStruct { struct_name: &input.ident, fields, generics: &input.generics, } } fn make_implementation(&self) -> TokenStream { let struct_name = self.struct_name; let generics = self.generics; let retrievals = self.fields.iter().map(|field| field.make_retrieval()); // retrieve the first lifetime of the struct, or set it to `'static` if there is none. let lifetime = self .generics .lifetimes() .next() .map(|l| l.lifetime.clone()) .unwrap_or_else(|| Lifetime::new("'static", Span::call_site())); (quote! { impl#generics FeatureCollection<#lifetime> for #struct_name#generics { fn from_cache( cache: &mut FeatureCache<#lifetime>, class: ThreadingClass, ) -> Result { Ok(Self { #(#retrievals)* }) } } }) .into() } } pub fn feature_collection_derive_impl(input: TokenStream) -> TokenStream { let input: DeriveInput = parse_macro_input!(input); let list = FeatureCollectionStruct::from_derive_input(&input); list.make_implementation() } lv2-core-derive-2.1.1/src/lib.rs000064400000000000000000000015410000000000000144130ustar 00000000000000//! Procedural macros for `lv2-core`. #![recursion_limit = "128"] extern crate proc_macro; extern crate proc_macro2; extern crate syn; #[macro_use] extern crate quote; mod feature_collection_derive; mod lv2_descriptors; mod port_collection_derive; use proc_macro::TokenStream; /// Generate external symbols for LV2 plugins. #[proc_macro] pub fn lv2_descriptors(input: TokenStream) -> TokenStream { lv2_descriptors::lv2_descriptors_impl(input) } /// Implement the `PortCollection` trait for a port struct. #[proc_macro_derive(PortCollection)] pub fn port_collection_derive(input: TokenStream) -> TokenStream { port_collection_derive::port_collection_derive_impl(input) } #[proc_macro_derive(FeatureCollection)] pub fn feature_collection_derive(input: TokenStream) -> TokenStream { feature_collection_derive::feature_collection_derive_impl(input) } lv2-core-derive-2.1.1/src/lv2_descriptors.rs000064400000000000000000000114460000000000000167760ustar 00000000000000use proc_macro::TokenStream; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::{parse_macro_input, Result, Token, Type}; /// An instance descriptor that should be exported. /// /// The instance descriptor is defined by the plugin type. struct Lv2InstanceDescriptor { plugin_type: Type, } impl Parse for Lv2InstanceDescriptor { fn parse(input: ParseStream) -> Result { let plugin_type = input.parse()?; Ok(Lv2InstanceDescriptor { plugin_type }) } } impl Lv2InstanceDescriptor { /// Implement the `PluginInstanceDescriptor` for the plugin. /// /// By implementing `PluginInstanceDescriptor`, two static objects are created: The URI of the /// plugin, stored as a string, and the descriptor, a struct with pointers to the plugin's /// basic functions; Like `instantiate` or `run`. pub fn make_instance_descriptor_impl(&self) -> impl ::quote::ToTokens { let plugin_type = &self.plugin_type; quote! { unsafe impl PluginInstanceDescriptor for #plugin_type { const DESCRIPTOR: LV2_Descriptor = LV2_Descriptor { URI: Self::URI.as_ptr() as *const u8 as *const ::std::os::raw::c_char, instantiate: Some(PluginInstance::::instantiate), connect_port: Some(PluginInstance::::connect_port), activate: Some(PluginInstance::::activate), run: Some(PluginInstance::::run), deactivate: Some(PluginInstance::::deactivate), cleanup: Some(PluginInstance::::cleanup), extension_data: Some(PluginInstance::::extension_data) }; } } } /// Create a matching arm for the plugin. /// /// The root function receives an index and has to return one plugin descriptor per index, /// or NULL. In this crate's implementation, this index is matched in a `match` statement and /// this method creates a match arm for this plugin. fn make_index_match_arm(&self, index: u32) -> impl ::quote::ToTokens { let plugin_type = &self.plugin_type; quote! { #index => &<#plugin_type as PluginInstanceDescriptor>::DESCRIPTOR, } } } /// A collection for instance descriptors. /// /// The contained instance descriptors are used to create the export function `lv2_descriptor` that /// tells the host of a library's plugins. struct Lv2InstanceDescriptorList { descriptors: Punctuated, } impl Parse for Lv2InstanceDescriptorList { fn parse(input: ParseStream) -> Result { Ok(Self { descriptors: Punctuated::parse_terminated(input)?, }) } } impl Lv2InstanceDescriptorList { /// Implement `PluginInstanceDescriptor` for all plugin instances. fn make_instance_descriptor_impls(&self) -> impl Iterator + '_ { self.descriptors .iter() .map(Lv2InstanceDescriptor::make_instance_descriptor_impl) } /// Create the `lv2_descriptor` function. /// /// This function tells the host of a library's plugin instances by returning one plugin /// instance per index. fn make_descriptor_function(&self) -> impl ::quote::ToTokens { let index_matchers = self .descriptors .iter() .enumerate() .map(|(i, desc)| desc.make_index_match_arm(i as u32)); quote! { /// Return a raw pointer to the plugin descriptor with the given index. /// /// This function is used by the host to discover plugins in the library. The host calls it with an ascending index and stores every returned descriptor, /// until a null pointer is returned. /// /// # Safety /// /// This function is primarily unsafe because it's a method that's directly called by the host. It doesn't actually do anything that unsafe. /// /// The returned pointer references a constant and there is valid as long as the library is loaded. #[no_mangle] pub unsafe extern "C" fn lv2_descriptor(index: u32) -> *const LV2_Descriptor { match index { #(#index_matchers)* _ => ::std::ptr::null() } } } } } /// Generate external symbols for LV2 plugins. #[inline] pub fn lv2_descriptors_impl(input: TokenStream) -> TokenStream { let list: Lv2InstanceDescriptorList = parse_macro_input!(input); let descriptors = list.make_instance_descriptor_impls(); let export_function = list.make_descriptor_function(); (quote! { #(#descriptors)* #export_function }) .into() } lv2-core-derive-2.1.1/src/port_collection_derive.rs000064400000000000000000000124010000000000000203770ustar 00000000000000use proc_macro::TokenStream; use proc_macro2::Span; use syn::DeriveInput; use syn::Field; use syn::{parse_macro_input, Data, DataStruct, Ident, Type}; /// A field in the struct we implement `PortCollection` for. struct PortCollectionField<'a> { identifier: &'a Ident, port_type: &'a Type, } impl<'a> PortCollectionField<'a> { /// Create a `Self` instance from a field object. fn from_input_field(input: &'a Field) -> Self { PortCollectionField { identifier: input.ident.as_ref().unwrap(), port_type: &input.ty, } } /// Create the field initialization line for the implementing struct. fn make_connection_from_raw(&self) -> impl ::quote::ToTokens { let identifier = self.identifier; let port_type = self.port_type; quote! { #identifier: <#port_type as PortHandle>::from_raw(connections.#identifier, sample_count)?, } } /// Create the corresponding field declaration line for the raw pointer struct. fn make_raw_field_declaration(&self) -> impl ::quote::ToTokens { let identifier = self.identifier; quote! { pub #identifier: *mut ::std::ffi::c_void, } } /// Create the corresponding field initialization line for the raw pointer struct. fn make_raw_field_initialization(&self) -> impl ::quote::ToTokens { let identifier = self.identifier; quote! { #identifier: ::std::ptr::null_mut(), } } /// Create the connection matching arm for the raw pointer struct. fn make_connect_matcher(&self, index: u32) -> impl ::quote::ToTokens { let identifier = self.identifier; quote! { #index => self.#identifier = pointer, } } } /// Representation of a struct we implement `PortCollection` for. /// /// The implementation creates a hidden, mirrored version of the implementing struct that contains /// the raw pointers for the port. Then, the ports object is created from the raw version. struct PortCollectionStruct<'a> { struct_name: &'a Ident, fields: Vec>, } impl<'a> PortCollectionStruct<'a> { /// Return an `Ident` for the internal module name. fn internal_cache_name(&self) -> Ident { Ident::new( &format!( "__lv2_plugin_ports_derive_{}_PortPointerCache", self.struct_name ), Span::call_site(), ) } /// Construct a `Self` instance from a `DeriveInput`. fn from_derive_input(input: &'a DeriveInput) -> Self { let struct_name = &input.ident; let fields = match &input.data { Data::Enum(_) | Data::Union(_) => panic!("Only structs can implement PortCollection"), Data::Struct(DataStruct { fields, .. }) => fields .iter() .map(PortCollectionField::from_input_field) .collect(), }; PortCollectionStruct { struct_name, fields, } } /// Implement `PortCollection` for the struct. fn make_derived_contents(&self) -> TokenStream { let struct_name = self.struct_name; let internal_cache_name = self.internal_cache_name(); let connections_from_raw = self .fields .iter() .map(PortCollectionField::make_connection_from_raw); let raw_field_declarations = self .fields .iter() .map(PortCollectionField::make_raw_field_declaration); let raw_field_inits = self .fields .iter() .map(PortCollectionField::make_raw_field_initialization); let connect_matchers = self .fields .iter() .enumerate() .map(|(i, f)| f.make_connect_matcher(i as u32)); (quote! { impl PortCollection for #struct_name { type Cache = #internal_cache_name; #[inline] unsafe fn from_connections(connections: &::Cache, sample_count: u32) -> Option { Some( Self { #(#connections_from_raw)* } ) } } #[doc(hidden)] #[allow(non_snake_case)] pub struct #internal_cache_name { #(#raw_field_declarations)* } impl Default for #internal_cache_name { #[inline] fn default() -> Self { Self { #(#raw_field_inits)* } } } impl PortPointerCache for #internal_cache_name { fn connect(&mut self, index: u32, pointer: *mut ::std::ffi::c_void) { match index { #(#connect_matchers)* _ => () } } } }).into() } } /// Implement `PortCollection` for a struct. #[inline] pub fn port_collection_derive_impl(input: TokenStream) -> TokenStream { let input: DeriveInput = parse_macro_input!(input); let list = PortCollectionStruct::from_derive_input(&input); list.make_derived_contents() }