Skip to content

Commit

Permalink
Add more functionality to get_custom_type_aliases (#2088)
Browse files Browse the repository at this point in the history
  • Loading branch information
giarc3 authored May 3, 2024
1 parent a47508d commit 9e61724
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 19 additions & 0 deletions fixtures/ext-types/custom-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

// A trivial guid, declared as `[Custom]` in the UDL.
pub struct Guid(pub String);

Expand Down Expand Up @@ -136,6 +138,23 @@ fn get_nested_ouid(nouid: Option<ANestedOuid>) -> 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<StringWrapper, IntWrapper>);
uniffi::custom_newtype!(MapUsingStringWrapper, HashMap<StringWrapper, IntWrapper>);

#[uniffi::export]
fn get_map_using_string_wrapper(maybe_map: Option<MapUsingStringWrapper>) -> MapUsingStringWrapper {
maybe_map.unwrap_or_else(|| MapUsingStringWrapper(HashMap::new()))
}

// And custom types around other objects.
#[derive(uniffi::Object)]
pub struct InnerObject;
Expand Down
8 changes: 4 additions & 4 deletions uniffi_bindgen/src/bindings/python/gen_python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9e61724

Please sign in to comment.