| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
//! The set of all [`Type`]s used in a component interface is represented by a `TypeUniverse`, |
| 6 |
//! which can be used by the bindings generator code to determine what type-related helper |
| 7 |
//! functions to emit for a given component. |
| 8 |
//! |
| 9 |
use anyhow::{Context, Result}; |
| 10 |
use std::{collections::hash_map::Entry, collections::BTreeSet, collections::HashMap}; |
| 11 |
|
| 12 |
pub use uniffi_meta::{AsType, NamespaceMetadata, ObjectImpl, Type, TypeIterator}; |
| 13 |
|
| 14 |
/// The set of all possible types used in a particular component interface. |
| 15 |
/// |
| 16 |
/// Every component API uses a finite number of types, including primitive types, API-defined |
| 17 |
/// types like records and enums, and recursive types such as sequences of the above. Our |
| 18 |
/// component API doesn't support fancy generics so this is a finitely-enumerable set, which |
| 19 |
/// is useful to be able to operate on explicitly. |
| 20 |
/// |
| 21 |
/// You could imagine this struct doing some clever interning of names and so-on in future, |
| 22 |
/// to reduce the overhead of passing around [Type] instances. For now we just do a whole |
| 23 |
/// lot of cloning. |
| 24 |
#[derive(Clone, Debug, Default)] |
| 25 |
pub(crate) struct TypeUniverse { |
| 26 |
/// The unique prefixes that we'll use for namespacing when exposing this component's API. |
| 27 |
pub namespace: NamespaceMetadata, |
| 28 |
pub namespace_docstring: Option<String>, |
| 29 |
|
| 30 |
// Named type definitions (including aliases). |
| 31 |
pub(super) type_definitions: HashMap<String, Type>, |
| 32 |
// All the types in the universe, by canonical type name, in a well-defined order. |
| 33 |
pub(super) all_known_types: BTreeSet<Type>, |
| 34 |
} |
| 35 |
|
| 36 |
impl TypeUniverse { |
| 37 |
pub fn new(namespace: NamespaceMetadata) -> Self { |
| 38 |
Self { |
| 39 |
namespace, |
| 40 |
..Default::default() |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/// Add the definition of a named [Type]. |
| 45 |
fn add_type_definition(&mut self, name: &str, type_: &Type) -> Result<()> { |
| 46 |
match self.type_definitions.entry(name.to_string()) { |
| 47 |
Entry::Occupied(o) => { |
| 48 |
// mismatched types are bad. |
| 49 |
let cur = o.get(); |
| 50 |
anyhow::ensure!( |
| 51 |
type_ == cur, |
| 52 |
"conflicting types:\ncur: {cur:?}\nnew: {type_:?}", |
| 53 |
); |
| 54 |
Ok(()) |
| 55 |
} |
| 56 |
Entry::Vacant(e) => { |
| 57 |
e.insert(type_.clone()); |
| 58 |
Ok(()) |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/// Get the [Type] corresponding to a given name, if any. |
| 64 |
pub(super) fn get_type_definition(&self, name: &str) -> Option<Type> { |
| 65 |
self.type_definitions.get(name).cloned() |
| 66 |
} |
| 67 |
|
| 68 |
/// Add a [Type] to the set of all types seen in the component interface. |
| 69 |
pub fn add_known_type(&mut self, type_: &Type) -> Result<()> { |
| 70 |
// Types are more likely to already be known than not, so avoid unnecessary cloning. |
| 71 |
if !self.all_known_types.contains(type_) { |
| 72 |
self.all_known_types.insert(type_.to_owned()); |
| 73 |
} |
| 74 |
// all sub-types, but we want to skip this type as we just added it above. |
| 75 |
for sub in type_.iter_nested_types() { |
| 76 |
self.add_known_type(sub)?; |
| 77 |
} |
| 78 |
if let Some(name) = type_.name() { |
| 79 |
self.add_type_definition(name, type_) |
| 80 |
.with_context(|| format!("adding named type {name}"))?; |
| 81 |
} |
| 82 |
Ok(()) |
| 83 |
} |
| 84 |
|
| 85 |
/// Add many [`Type`]s... |
| 86 |
pub fn add_known_types(&mut self, types: TypeIterator<'_>) -> Result<()> { |
| 87 |
for t in types { |
| 88 |
self.add_known_type(t) |
| 89 |
.with_context(|| format!("adding type {t:?}"))? |
| 90 |
} |
| 91 |
Ok(()) |
| 92 |
} |
| 93 |
|
| 94 |
pub fn is_external(&self, t: &Type) -> bool { |
| 95 |
t.module_path() |
| 96 |
.map(|p| p != self.namespace.crate_name) |
| 97 |
.unwrap_or(false) |
| 98 |
} |
| 99 |
|
| 100 |
#[cfg(test)] |
| 101 |
/// Check if a [Type] is present |
| 102 |
pub fn contains(&self, type_: &Type) -> bool { |
| 103 |
self.all_known_types.contains(type_) |
| 104 |
} |
| 105 |
|
| 106 |
pub fn iter_local_types(&self) -> impl Iterator<Item = &Type> { |
| 107 |
self.filter_local_types(self.all_known_types.iter()) |
| 108 |
} |
| 109 |
|
| 110 |
pub fn iter_external_types(&self) -> impl Iterator<Item = &Type> { |
| 111 |
self.all_known_types.iter().filter(|t| self.is_external(t)) |
| 112 |
} |
| 113 |
|
| 114 |
// Keep only the local types in an iterator of types. |
| 115 |
pub fn filter_local_types<'a>( |
| 116 |
&'a self, |
| 117 |
types: impl Iterator<Item = &'a Type>, |
| 118 |
) -> impl Iterator<Item = &'a Type> { |
| 119 |
types.filter(|t| !self.is_external(t)) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
#[cfg(test)] |
| 124 |
mod test_type_universe { |
| 125 |
// All the useful functionality of the `TypeUniverse` struct |
| 126 |
// is tested as part of the `TypeFinder` and `TypeResolver` test suites. |
| 127 |
} |