/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! # Record definitions for a `ComponentInterface`. //! //! This module converts "dictionary" definitions from UDL into [`Record`] structures //! that can be added to a `ComponentInterface`, which are the main way we define structured //! data types for a UniFFI Rust Component. A [`Record`] has a fixed set of named fields, //! each of a specific type. //! //! (The terminology mismatch between "dictionary" and "record" is a historical artifact //! due to this tool being loosely inspired by WebAssembly Interface Types, which used //! the term "record" for this sort of data). //! //! A declaration in the UDL like this: //! //! ``` //! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##" //! # namespace example {}; //! dictionary Example { //! string name; //! u32 value; //! }; //! # "##, "crate_name")?; //! # Ok::<(), anyhow::Error>(()) //! ``` //! //! Will result in a [`Record`] member with two [`Field`]s being added to the resulting //! [`crate::ComponentInterface`]: //! //! ``` //! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##" //! # namespace example {}; //! # dictionary Example { //! # string name; //! # u32 value; //! # }; //! # "##, "crate_name")?; //! let record = ci.get_record_definition("Example").unwrap(); //! assert_eq!(record.name(), "Example"); //! assert_eq!(record.fields()[0].name(), "name"); //! assert_eq!(record.fields()[1].name(), "value"); //! # Ok::<(), anyhow::Error>(()) //! ```
/// Represents a "data class" style object, for passing around complex values. /// /// In the FFI these are represented as a byte buffer, which one side explicitly /// serializes the data into and the other serializes it out of. So I guess they're /// kind of like "pass by clone" values. #[derive(Debug, Clone, Checksum)] pubstruct Record { pub(super) name: String, pub(super) module_path: String, pub(super) remote: bool, pub(super) fields: Vec<Field>, pub(super) constructors: Vec<Constructor>, pub(super) methods: Vec<Method>, pub uniffi_traits: Vec<UniffiTrait>, #[checksum_ignore] pub(super) docstring: Option<String>,
}
impl Record { pubfn name(&self) -> &str {
&self.name
}
pubfn derive_ffi_funcs(&mutself) -> Result<()> { for c inself.constructors.iter_mut() {
c.derive_ffi_func();
} for m inself.methods.iter_mut() {
m.derive_ffi_func()?;
} for ut inself.uniffi_traits.iter_mut() {
ut.derive_ffi_func()?;
}
Ok(())
}
pubfn iter_ffi_function_definitions(&self) -> impl Iterator<Item = &FfiFunction> { self.constructors
.iter()
.map(|f| &f.ffi_func)
.chain(self.methods.iter().map(|f| &f.ffi_func))
.chain( self.uniffi_traits
.iter()
.flat_map(|ut| match ut {
UniffiTrait::Display { fmt: m }
| UniffiTrait::Debug { fmt: m }
| UniffiTrait::Hash { hash: m }
| UniffiTrait::Ord { cmp: m } => vec![m],
UniffiTrait::Eq { eq, ne } => vec![eq, ne],
})
.map(|m| &m.ffi_func),
)
}
}
impl AsType for Record { fn as_type(&self) -> Type { Type::Record {
name: self.name.clone(),
module_path: self.module_path.clone(),
}
}
}
impl TryFrom<uniffi_meta::RecordMetadata> for Record { type Error = anyhow::Error;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.