diff --git a/templates/lib.rs b/templates/lib.rs index 5c46ebe..5b172bf 100644 --- a/templates/lib.rs +++ b/templates/lib.rs @@ -1,29 +1,49 @@ -{% if macro_only %} +{%- if macro_only -%} uniffi::setup_scaffolding!(); -{% else %} +{%- else -%} uniffi::include_scaffolding!("lib"); {%- endif %} {% if !plain %} +{% if !macro_only -%} // Bindings have to be exposed in a .udl file with the same name as the corresponding .rs file, i.e. lib.udl // You can expose top-level functions... +{%- else -%} +// You can annotate items with uniffi macros to make them available in your swift package. +// You can export functions... +#[uniffi::export] +{%- endif %} pub fn add(a: u64, b: u64) -> u64 { a + b } // ... data structs without methods ... +{%- if macro_only ~%} +#[derive(uniffi::Record)] +{%- endif %} pub struct Example { pub items: Vec, pub value: Option, } // ... classes with methods ... +{%- if macro_only ~%} +#[derive(uniffi::Object)] +{%- endif %} pub struct Greeter { name: String, } +{% if macro_only -%} +#[uniffi::export] +{%~ endif -%} impl Greeter { + {%- if macro_only %} + // Constructors need to be annotated as such + #[uniffi::constructor] + {%- else %} // By convention, a method called new is exposed as a constructor + {%- endif %} pub fn new(name: String) -> Self { Self { name } } @@ -33,14 +53,9 @@ impl Greeter { } } -// Instead of adding bindings in lib.udl, you can also use UniFFIs macros -// Note that you have to keep the .udl file and its namespace if you are using macros -// -// You can read more about UniFFI macros here: https://mozilla.github.io/uniffi-rs/proc_macro/index.html -#[uniffi::export] -pub fn subtract(a: u64, b: u64) -> u64 { - a - b -} - +{% if macro_only -%} +// ... and much more! For more information about uniffi macros, read the UniFFI book: https://mozilla.github.io/uniffi-rs/proc_macro/index.html +{%- else -%} // ... and much more! For more information about bindings, read the UniFFI book: https://mozilla.github.io/uniffi-rs/udl_file_spec.html {%- endif %} +{%- endif %}