From 9e61724e4440ecba29012901f54d86458dfc9168 Mon Sep 17 00:00:00 2001 From: Craig Colegrove <34786857+giarc3@users.noreply.github.com> Date: Thu, 2 May 2024 18:04:05 -0700 Subject: [PATCH] Add more functionality to get_custom_type_aliases (#2088) --- fixtures/ext-types/custom-types/src/lib.rs | 19 +++++++++++++++++++ .../src/bindings/python/gen_python/mod.rs | 8 ++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/fixtures/ext-types/custom-types/src/lib.rs b/fixtures/ext-types/custom-types/src/lib.rs index db89bb68e4..dc05f31c2e 100644 --- a/fixtures/ext-types/custom-types/src/lib.rs +++ b/fixtures/ext-types/custom-types/src/lib.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + // A trivial guid, declared as `[Custom]` in the UDL. pub struct Guid(pub String); @@ -136,6 +138,23 @@ fn get_nested_ouid(nouid: Option) -> ANestedOuid { nouid.unwrap_or_else(|| ANestedOuid(Ouid("ANestedOuid".to_string()))) } +// Dependent types that are nested deeper inside of other types +// need to also be taken into account when ordering aliases. +#[derive(PartialEq, Eq, Hash)] +pub struct StringWrapper(pub String); +uniffi::custom_newtype!(StringWrapper, String); + +pub struct IntWrapper(pub u32); +uniffi::custom_newtype!(IntWrapper, u32); + +pub struct MapUsingStringWrapper(pub HashMap); +uniffi::custom_newtype!(MapUsingStringWrapper, HashMap); + +#[uniffi::export] +fn get_map_using_string_wrapper(maybe_map: Option) -> MapUsingStringWrapper { + maybe_map.unwrap_or_else(|| MapUsingStringWrapper(HashMap::new())) +} + // And custom types around other objects. #[derive(uniffi::Object)] pub struct InnerObject; diff --git a/uniffi_bindgen/src/bindings/python/gen_python/mod.rs b/uniffi_bindgen/src/bindings/python/gen_python/mod.rs index 078816871b..b3b8e9bdc7 100644 --- a/uniffi_bindgen/src/bindings/python/gen_python/mod.rs +++ b/uniffi_bindgen/src/bindings/python/gen_python/mod.rs @@ -284,10 +284,10 @@ impl<'a> TypeRenderer<'a> { let mut ordered = vec![]; for type_ in self.ci.iter_types() { if let Type::Custom { name, builtin, .. } = type_ { - match ordered - .iter() - .position(|x: &(&str, &Type)| *name == x.1.as_codetype().type_label()) - { + match ordered.iter().position(|x: &(&str, &Type)| { + x.1.iter_types() + .any(|nested_type| *name == nested_type.as_codetype().type_label()) + }) { // This 'name' appears as a builtin, so we must insert our type first. Some(pos) => ordered.insert(pos, (name, builtin)), // Otherwise at the end.