Skip to content

Commit

Permalink
Update template examples in macro-only template
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniusnaumann committed Dec 12, 2023
1 parent f19ca77 commit 214ec00
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions templates/lib.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
pub value: Option<f64>,
}

// ... 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 }
}
Expand All @@ -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 %}

0 comments on commit 214ec00

Please sign in to comment.