Commit 4c0de8cc authored by Ben Dean-Kawamura's avatar Ben Dean-Kawamura
Browse files

Bindgen refactors

Added `Enum::variant_discr_iter` and use that to implement `variant_discr`.
This provides a simple and efficient way for other code to get a Vec of
discriminants.

Renamed `uniffi_checksum_derive` -> `uniffi_internal_macros`.  This way
we can use the same crate for future internal macros.

This is prep work for the bindings IR patch I'm hoping to land.
parent fa02b83d
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -2066,14 +2066,6 @@ dependencies = [
 "uniffi_bindgen",
]

[[package]]
name = "uniffi_checksum_derive"
version = "0.28.3"
dependencies = [
 "quote",
 "syn",
]

[[package]]
name = "uniffi_core"
version = "0.28.3"
@@ -2086,6 +2078,14 @@ dependencies = [
 "static_assertions",
]

[[package]]
name = "uniffi_internal_macros"
version = "0.28.3"
dependencies = [
 "quote",
 "syn",
]

[[package]]
name = "uniffi_macros"
version = "0.28.3"
@@ -2110,7 +2110,7 @@ dependencies = [
 "anyhow",
 "bytes",
 "siphasher",
 "uniffi_checksum_derive",
 "uniffi_internal_macros",
]

[[package]]
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ done
set -ex

# Note: make sure these are ordered so that dependencies come before the crates that depend on them
cargo publish -p uniffi_checksum_derive
cargo publish -p uniffi_internal_macros
cargo publish -p uniffi_meta
cargo publish -p uniffi_core
cargo publish -p uniffi_testing
+14 −8
Original line number Diff line number Diff line
@@ -209,14 +209,20 @@ impl Enum {
    // in those cases, so by the time this get's run we can be confident these
    // error cases can't exist.
    pub fn variant_discr(&self, variant_index: usize) -> Result<Literal> {
        if variant_index >= self.variants.len() {
        for (i, lit) in self.variant_discr_iter().enumerate() {
            let lit = lit?;
            if i == variant_index {
                return Ok(lit);
            }
        }
        anyhow::bail!("Invalid variant index {variant_index}");
    }

    // Iterate over variant discriminants
    pub fn variant_discr_iter(&self) -> impl Iterator<Item = Result<Literal>> + '_ {
        let mut next = 0;
        let mut this;
        let mut this_lit = Literal::new_uint(0);
        for v in self.variants().iter().take(variant_index + 1) {
            (this, this_lit) = match v.discr {
        self.variants().iter().map(move |v| {
            let (this, this_lit) = match v.discr {
                None => (
                    next,
                    if (next as i64) < 0 {
@@ -231,8 +237,8 @@ impl Enum {
                _ => anyhow::bail!("Invalid literal type {v:?}"),
            };
            next = this.wrapping_add(1);
        }
            Ok(this_lit)
        })
    }

    pub fn variant_discr_type(&self) -> &Option<Type> {
+2 −2
Original line number Diff line number Diff line
[package]
name = "uniffi_checksum_derive"
name = "uniffi_internal_macros"
version = "0.28.3"
description = "a multi-language bindings generator for rust (checksum custom derive)"
description = "a multi-language bindings generator for rust (interal macro crate)"
documentation = "https://mozilla.github.io/uniffi-rs"
homepage = "https://mozilla.github.io/uniffi-rs"
repository = "https://github.com/mozilla/uniffi-rs"
+1 −2
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Custom derive for uniffi_meta::Checksum

use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{
@@ -23,6 +21,7 @@ fn has_ignore_attribute(attrs: &[Attribute]) -> bool {
    })
}

/// Custom derive for uniffi_meta::Checksum
#[proc_macro_derive(Checksum, attributes(checksum_ignore))]
pub fn checksum_derive(input: TokenStream) -> TokenStream {
    let input: DeriveInput = parse_macro_input!(input);
Loading