From 4c0de8cc528f0e310fe7e745e72de0a3ae270ff9 Mon Sep 17 00:00:00 2001 From: Ben Dean-Kawamura Date: Wed, 18 Dec 2024 10:17:31 -0500 Subject: [PATCH] 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. --- Cargo.lock | 18 +++++++-------- tools/publish-release.sh | 2 +- uniffi_bindgen/src/interface/enum_.rs | 22 ++++++++++++------- .../Cargo.toml | 4 ++-- .../src/lib.rs | 3 +-- uniffi_meta/Cargo.toml | 2 +- uniffi_meta/src/lib.rs | 2 +- 7 files changed, 29 insertions(+), 24 deletions(-) rename {uniffi_checksum_derive => uniffi_internal_macros}/Cargo.toml (78%) rename {uniffi_checksum_derive => uniffi_internal_macros}/src/lib.rs (99%) diff --git a/Cargo.lock b/Cargo.lock index 7264c4877f..7dffc9d6b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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]] diff --git a/tools/publish-release.sh b/tools/publish-release.sh index 00bca016f6..bdac52cb3f 100755 --- a/tools/publish-release.sh +++ b/tools/publish-release.sh @@ -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 diff --git a/uniffi_bindgen/src/interface/enum_.rs b/uniffi_bindgen/src/interface/enum_.rs index 4eac0fb700..b3dc5198de 100644 --- a/uniffi_bindgen/src/interface/enum_.rs +++ b/uniffi_bindgen/src/interface/enum_.rs @@ -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 { - if variant_index >= self.variants.len() { - anyhow::bail!("Invalid variant index {variant_index}"); + 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> + '_ { 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) + Ok(this_lit) + }) } pub fn variant_discr_type(&self) -> &Option { diff --git a/uniffi_checksum_derive/Cargo.toml b/uniffi_internal_macros/Cargo.toml similarity index 78% rename from uniffi_checksum_derive/Cargo.toml rename to uniffi_internal_macros/Cargo.toml index 7cb3ec678b..1840a0fae8 100644 --- a/uniffi_checksum_derive/Cargo.toml +++ b/uniffi_internal_macros/Cargo.toml @@ -1,7 +1,7 @@ [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" diff --git a/uniffi_checksum_derive/src/lib.rs b/uniffi_internal_macros/src/lib.rs similarity index 99% rename from uniffi_checksum_derive/src/lib.rs rename to uniffi_internal_macros/src/lib.rs index 449183dedf..3111679b98 100644 --- a/uniffi_checksum_derive/src/lib.rs +++ b/uniffi_internal_macros/src/lib.rs @@ -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); diff --git a/uniffi_meta/Cargo.toml b/uniffi_meta/Cargo.toml index 1f7fa9fa99..de8b5fee14 100644 --- a/uniffi_meta/Cargo.toml +++ b/uniffi_meta/Cargo.toml @@ -13,4 +13,4 @@ readme = "../README.md" anyhow = "1" bytes = "1.3" siphasher = "0.3" -uniffi_checksum_derive = { version = "0.28.3", path = "../uniffi_checksum_derive" } +uniffi_internal_macros = { version = "0.28.3", path = "../uniffi_internal_macros" } diff --git a/uniffi_meta/src/lib.rs b/uniffi_meta/src/lib.rs index d8e9e47d76..39315fbdf1 100644 --- a/uniffi_meta/src/lib.rs +++ b/uniffi_meta/src/lib.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::{collections::BTreeMap, hash::Hasher}; -pub use uniffi_checksum_derive::Checksum; +pub use uniffi_internal_macros::Checksum; mod ffi_names; pub use ffi_names::*;