From a9594dafe1073ecf12ad9349e050e5263044ee76 Mon Sep 17 00:00:00 2001 From: Edouard Oger Date: Thu, 13 Aug 2020 13:55:17 -0400 Subject: [PATCH] Implement record type --- examples/rondpoint/src/lib.rs | 6 ++ examples/rondpoint/src/rondpoint.idl | 1 + .../tests/bindings/test_rondpoint.kts | 1 + .../tests/bindings/test_rondpoint.swift | 1 + uniffi/src/lib.rs | 90 +++++++++++++------ .../src/bindings/kotlin/gen_kotlin.rs | 34 +++++++ uniffi_bindgen/src/bindings/kotlin/mod.rs | 1 + .../kotlin/templates/RustBufferHelpers.kt | 30 +++++++ .../src/bindings/python/gen_python.rs | 8 +- .../src/bindings/swift/gen_swift.rs | 1 + .../swift/templates/RustBufferHelper.swift | 39 ++++++-- uniffi_bindgen/src/interface/types.rs | 14 ++- uniffi_bindgen/src/scaffolding.rs | 1 + uniffi_bindgen/src/templates/EnumTemplate.rs | 6 +- .../src/templates/RecordTemplate.rs | 6 +- 15 files changed, 197 insertions(+), 42 deletions(-) diff --git a/examples/rondpoint/src/lib.rs b/examples/rondpoint/src/lib.rs index 9a607e4806..8fc2c02e7c 100644 --- a/examples/rondpoint/src/lib.rs +++ b/examples/rondpoint/src/lib.rs @@ -2,6 +2,8 @@ * 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/. */ +use std::collections::HashMap; + #[derive(Debug, Clone)] pub struct Dictionnaire { un: Enumeration, @@ -36,6 +38,10 @@ fn copie_enumerations(e: Vec) -> Vec { e } +fn copie_carte(e: HashMap) -> HashMap { + e +} + fn copie_dictionnaire(d: Dictionnaire) -> Dictionnaire { d } diff --git a/examples/rondpoint/src/rondpoint.idl b/examples/rondpoint/src/rondpoint.idl index 32d3ce9e0d..4a36cbdeb9 100644 --- a/examples/rondpoint/src/rondpoint.idl +++ b/examples/rondpoint/src/rondpoint.idl @@ -2,6 +2,7 @@ namespace rondpoint { Dictionnaire copie_dictionnaire(Dictionnaire d); Enumeration copie_enumeration(Enumeration e); sequence copie_enumerations(sequence e); + record copie_carte(record c); boolean switcheroo(boolean b); }; diff --git a/examples/rondpoint/tests/bindings/test_rondpoint.kts b/examples/rondpoint/tests/bindings/test_rondpoint.kts index 9e83706b49..8fe9dc712b 100644 --- a/examples/rondpoint/tests/bindings/test_rondpoint.kts +++ b/examples/rondpoint/tests/bindings/test_rondpoint.kts @@ -6,5 +6,6 @@ assert(dico == copyDico) assert(copieEnumeration(Enumeration.DEUX) == Enumeration.DEUX) assert(copieEnumerations(listOf(Enumeration.UN, Enumeration.DEUX)) == listOf(Enumeration.UN, Enumeration.DEUX)) +assert(copieCarte(mapOf("1" to Enumeration.UN, "2" to Enumeration.DEUX)) == mapOf("1" to Enumeration.UN, "2" to Enumeration.DEUX)) assert(switcheroo(false)) diff --git a/examples/rondpoint/tests/bindings/test_rondpoint.swift b/examples/rondpoint/tests/bindings/test_rondpoint.swift index a0f915a3d0..996b2b3da2 100644 --- a/examples/rondpoint/tests/bindings/test_rondpoint.swift +++ b/examples/rondpoint/tests/bindings/test_rondpoint.swift @@ -6,5 +6,6 @@ assert(dico == copyDico) assert(copieEnumeration(e: .deux) == .deux) assert(copieEnumerations(e: [.un, .deux]) == [.un, .deux]) +assert(copieCarte(c: ["1": .un, "2": .deux]) == ["1": .un, "2": .deux]) assert(switcheroo(b: false)) diff --git a/uniffi/src/lib.rs b/uniffi/src/lib.rs index a901fe7849..d4f4c406de 100644 --- a/uniffi/src/lib.rs +++ b/uniffi/src/lib.rs @@ -28,8 +28,7 @@ use anyhow::{bail, Result}; use bytes::buf::{Buf, BufMut}; use ffi_support::ByteBuffer; use paste::paste; -use std::convert::TryFrom; -use std::ffi::CString; +use std::{collections::HashMap, convert::TryFrom, ffi::CString}; // It would be nice if this module was behind a cfg(test) guard, but it // doesn't work between crates so let's hope LLVM tree-shaking works well. @@ -75,9 +74,9 @@ pub unsafe trait ViaFfi: Sized { /// serializing the data for transfer. In theory it could be possible to build a matching /// `#[repr(C)]` struct for a complex data type and pass that instead, but explicit /// serialization is simpler and safer as a starting point. - type Value; + type FfiType; - /// Lower a rust value of the target type, into an FFI value of type Self::Value. + /// Lower a rust value of the target type, into an FFI value of type Self::FfiType. /// /// This trait method is used for sending data from rust to the foreign language code, /// by (hopefully cheaply!) converting it into someting that can be passed over the FFI @@ -85,17 +84,17 @@ pub unsafe trait ViaFfi: Sized { /// /// Note that this method takes an owned `self`; this allows it to transfer ownership /// in turn to the foreign language code, e.g. by boxing the value and passing a pointer. - fn lower(self) -> Self::Value; + fn lower(self) -> Self::FfiType; - /// Lift a rust value of the target type, from an FFI value of type Self::Value. + /// Lift a rust value of the target type, from an FFI value of type Self::FfiType. /// /// This trait method is used for receiving data from the foreign language code in rust, - /// by (hopefully cheaply!) converting it from a low-level FFI value of type Self::Value + /// by (hopefully cheaply!) converting it from a low-level FFI value of type Self::FfiType /// into a high-level rust value of the target type. /// /// Since we cannot statically guarantee that the foreign-language code will send valid - /// values of type Self::Value, this method is fallible. - fn try_lift(v: Self::Value) -> Result; + /// values of type Self::FfiType, this method is fallible. + fn try_lift(v: Self::FfiType) -> Result; /// Write a rust value into a bytebuffer, to send over the FFI in serialized form. /// @@ -163,13 +162,13 @@ macro_rules! impl_via_ffi_for_num_primitive { $( paste! { unsafe impl ViaFfi for $T { - type Value = Self; + type FfiType = Self; - fn lower(self) -> Self::Value { + fn lower(self) -> Self::FfiType { self } - fn try_lift(v: Self::Value) -> Result { + fn try_lift(v: Self::FfiType) -> Result { Ok(v) } @@ -196,9 +195,9 @@ impl_via_ffi_for_num_primitive! { /// Booleans are passed as a `u8` in order to avoid problems with handling /// C-compatible boolean values on JVM-based languages. unsafe impl ViaFfi for bool { - type Value = u8; + type FfiType = u8; - fn lower(self) -> Self::Value { + fn lower(self) -> Self::FfiType { if self { 1 } else { @@ -206,7 +205,7 @@ unsafe impl ViaFfi for bool { } } - fn try_lift(v: Self::Value) -> Result { + fn try_lift(v: Self::FfiType) -> Result { Ok(match v { 0 => false, 1 => true, @@ -234,13 +233,13 @@ unsafe impl ViaFfi for bool { /// `None` option is represented as a null pointer and the `Some` as a valid pointer, /// but that seems more fiddly and less safe in the short term, so it can wait. unsafe impl ViaFfi for Option { - type Value = ffi_support::ByteBuffer; + type FfiType = ffi_support::ByteBuffer; - fn lower(self) -> Self::Value { + fn lower(self) -> Self::FfiType { lower_into_bytebuffer(self) } - fn try_lift(v: Self::Value) -> Result { + fn try_lift(v: Self::FfiType) -> Result { try_lift_from_bytebuffer(v) } @@ -273,20 +272,20 @@ unsafe impl ViaFfi for Option { /// pair but that seems tremendously fiddly and unsafe in the short term. /// Maybe one day... unsafe impl ViaFfi for Vec { - type Value = ffi_support::ByteBuffer; + type FfiType = ffi_support::ByteBuffer; - fn lower(self) -> Self::Value { + fn lower(self) -> Self::FfiType { lower_into_bytebuffer(self) } - fn try_lift(v: Self::Value) -> Result { + fn try_lift(v: Self::FfiType) -> Result { try_lift_from_bytebuffer(v) } fn write(&self, buf: &mut B) { // TODO: would be nice not to panic here :-/ let len = u32::try_from(self.len()).unwrap(); - buf.put_u32(len); // We limit arrays to u32::MAX bytes + buf.put_u32(len); // We limit arrays to u32::MAX items for item in self.iter() { ViaFfi::write(item, buf); } @@ -303,6 +302,47 @@ unsafe impl ViaFfi for Vec { } } +/// Support for associative arrays via the FFI. +/// Note that because of webidl limitations, +/// the key must always be of the String type. +/// +/// HashMaps are currently always passed by serializing to a bytebuffer. +/// We write a `u32` entries count +/// followed by each entry (string key followed by the value) in turn. +unsafe impl ViaFfi for HashMap { + type FfiType = ffi_support::ByteBuffer; + + fn lower(self) -> Self::FfiType { + lower_into_bytebuffer(self) + } + + fn try_lift(v: Self::FfiType) -> Result { + try_lift_from_bytebuffer(v) + } + + fn write(&self, buf: &mut B) { + // TODO: would be nice not to panic here :-/ + let len = u32::try_from(self.len()).unwrap(); + buf.put_u32(len); // We limit HashMaps to u32::MAX entries + for (key, value) in self.iter() { + ViaFfi::write(key, buf); + ViaFfi::write(value, buf); + } + } + + fn try_read(buf: &mut B) -> Result { + check_remaining(buf, 4)?; + let len = buf.get_u32(); + let mut map = HashMap::with_capacity(len as usize); + for _ in 0..len { + let key = String::try_read(buf)?; + let value = ::try_read(buf)?; + map.insert(key, value); + } + Ok(map) + } +} + /// Support for passing Strings via the FFI. /// /// Unlike many other implementations of `ViaFfi`, this passes a pointer rather @@ -317,19 +357,19 @@ unsafe impl ViaFfi for Vec { /// (In practice, we currently do end up copying the data, the copying just happens /// on the foreign language side rather than here in the rust code.) unsafe impl ViaFfi for String { - type Value = *mut std::os::raw::c_char; + type FfiType = *mut std::os::raw::c_char; // This returns a raw pointer to the underlying bytes, so it's very important // that it consume ownership of the String, which is relinquished to the foreign // language code (and can be restored by it passing the pointer back). - fn lower(self) -> Self::Value { + fn lower(self) -> Self::FfiType { ffi_support::rust_string_to_c(self) } // The argument here *must* be a uniquely-owned pointer previously obtained // from `info_ffi_value` above. It will try to panic if you give it an invalid // pointer, but there's no guarantee that it will. - fn try_lift(v: Self::Value) -> Result { + fn try_lift(v: Self::FfiType) -> Result { if v.is_null() { bail!("null pointer passed as String") } diff --git a/uniffi_bindgen/src/bindings/kotlin/gen_kotlin.rs b/uniffi_bindgen/src/bindings/kotlin/gen_kotlin.rs index e3f698f264..7621c24750 100644 --- a/uniffi_bindgen/src/bindings/kotlin/gen_kotlin.rs +++ b/uniffi_bindgen/src/bindings/kotlin/gen_kotlin.rs @@ -61,6 +61,7 @@ mod filters { } Type::Optional(t) => format!("{}?", type_kt(t)?), Type::Sequence(t) => format!("List<{}>", type_kt(t)?), + Type::Map(t) => format!("Map", type_kt(t)?), }) } @@ -124,6 +125,14 @@ mod filters { calculate_write_size(&"v", t)?, write_kt(&"v", &"buf", t)? ), + Type::Map(t) => format!( + "lowerMap({}, {{ k, v -> {} + {} }}, {{ k, v, buf -> {}; {} }})", + nm, + calculate_write_size(&"k", &Type::String)?, + calculate_write_size(&"v", t)?, + write_kt(&"k", &"buf", &Type::String)?, + write_kt(&"v", &"buf", t)? + ), _ => format!("{}.lower()", nm), }) } @@ -151,6 +160,13 @@ mod filters { target, write_kt(&"v", &"buf", t)? ), + Type::Map(t) => format!( + "writeMap({}, {}, {{ k, v, buf -> {}; {} }})", + nm, + target, + write_kt(&"k", &"buf", &Type::String)?, + write_kt(&"v", &"buf", t)? + ), _ => format!("{}.write({})", nm, target), }) } @@ -175,6 +191,12 @@ mod filters { nm, calculate_write_size(&"v", t)? ), + Type::Map(t) => format!( + "calculateWriteSizeMap({}, {{ k, v -> {} + {} }})", + nm, + calculate_write_size(&"k", &Type::String)?, + calculate_write_size(&"v", t)? + ), _ => format!("{}.calculateWriteSize()", nm), }) } @@ -192,6 +214,12 @@ mod filters { Type::Sequence(t) => { format!("liftSequence({}, {{ buf -> {} }})", nm, read_kt(&"buf", t)?) } + Type::Map(t) => format!( + "liftMap({}, {{ buf -> Pair({}, {}) }})", + nm, + read_kt(&"buf", &Type::String)?, + read_kt(&"buf", t)? + ), _ => format!("{}.lift({})", type_kt(type_)?, nm), }) } @@ -209,6 +237,12 @@ mod filters { Type::Sequence(t) => { format!("readSequence({}, {{ buf -> {} }})", nm, read_kt(&"buf", t)?) } + Type::Map(t) => format!( + "readMap({}, {{ buf -> Pair({}, {}) }})", + nm, + read_kt(&"buf", &Type::String)?, + read_kt(&"buf", t)? + ), _ => format!("{}.read({})", type_kt(type_)?, nm), }) } diff --git a/uniffi_bindgen/src/bindings/kotlin/mod.rs b/uniffi_bindgen/src/bindings/kotlin/mod.rs index 81955c8cc5..3b8eb47509 100644 --- a/uniffi_bindgen/src/bindings/kotlin/mod.rs +++ b/uniffi_bindgen/src/bindings/kotlin/mod.rs @@ -66,6 +66,7 @@ pub fn compile_bindings(ci: &ComponentInterface, out_dir: &Path) -> Result<()> { let mut jar_file = PathBuf::from(out_dir); jar_file.push(format!("{}.jar", ci.namespace())); let status = Command::new("kotlinc") + .arg("-Xopt-in=kotlin.RequiresOptIn") .arg("-classpath") .arg(env::var("CLASSPATH").unwrap_or_else(|_| "".to_string())) .arg(&kt_file) diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/RustBufferHelpers.kt b/uniffi_bindgen/src/bindings/kotlin/templates/RustBufferHelpers.kt index 41c69352ce..e91ec024f3 100644 --- a/uniffi_bindgen/src/bindings/kotlin/templates/RustBufferHelpers.kt +++ b/uniffi_bindgen/src/bindings/kotlin/templates/RustBufferHelpers.kt @@ -96,6 +96,21 @@ fun readSequence(buf: ByteBuffer, readItem: (ByteBuffer) -> T): List { } } +fun liftMap(rbuf: RustBuffer.ByValue, readItem: (ByteBuffer) -> Pair): Map { + return liftFromRustBuffer(rbuf) { buf -> readMap(buf, readItem) } +} + +fun readMap(buf: ByteBuffer, readItem: (ByteBuffer) -> Pair): Map { + val len = Int.read(buf) + @OptIn(ExperimentalStdlibApi::class) + return buildMap(len) { + repeat(len) { + val (k, v) = readItem(buf) + put(k, v) + } + } +} + // Helpers for lowering primitive data types into a bytebuffer. // Since we need to allocate buffers from rust, the lowering process needs to be // able to calculate ahead-of-time what the required size of the buffer will be. @@ -244,6 +259,21 @@ fun writeSequence(v: List, buf: ByteBuffer, writeItem: (T, ByteBuffer) -> v.forEach { writeItem(it, buf) } } +fun lowerMap(m: Map, calculateWriteSize: (String, V) -> Int, writeEntry: (String, V, ByteBuffer) -> Unit): RustBuffer.ByValue { + return lowerIntoRustBuffer(m, { m -> calculateWriteSizeMap(m, calculateWriteSize) }, { m, buf -> writeMap(m, buf, writeEntry) }) +} + +fun calculateWriteSizeMap(v: Map, calculateWriteSize: (String, V) -> Int): Int { + var len = v.size.calculateWriteSize() + v.forEach { k, v -> len += calculateWriteSize(k, v) } + return len +} + +fun writeMap(v: Map, buf: ByteBuffer, writeEntry: (String, V, ByteBuffer) -> Unit) { + v.size.write(buf) + v.forEach { k, v -> writeEntry(k, v, buf) } +} + fun lowerOptional(v: T?, calculateWriteSize: (T) -> Int, writeItem: (T, ByteBuffer) -> Unit): RustBuffer.ByValue { return lowerIntoRustBuffer(v, { v -> calculateWriteSizeOptional(v, calculateWriteSize) }, { v, buf -> writeOptional(v, buf, writeItem) }) } diff --git a/uniffi_bindgen/src/bindings/python/gen_python.rs b/uniffi_bindgen/src/bindings/python/gen_python.rs index 564e1c504a..b53b48b52b 100644 --- a/uniffi_bindgen/src/bindings/python/gen_python.rs +++ b/uniffi_bindgen/src/bindings/python/gen_python.rs @@ -97,7 +97,13 @@ mod filters { Type::Enum(type_name) => format!("{} = {}({})", nm, type_name, nm), Type::Record(type_name) => format!("{} = {}._coerce({})", nm, type_name, nm), Type::Optional(t) => format!("(None if {} is None else {})", nm, coerce_py(nm, t)?), - Type::Sequence(t) => format!("({} for x in {})", coerce_py(&"x", t)?, nm), // TODO: name hygiene + Type::Sequence(t) => format!("({} for x in {})", coerce_py(&"x", t)?, nm), // TODO: name hygiene, + Type::Map(t) => format!( + "({}:{} for (k, v) in {}.items())", + coerce_py(&"k", t)?, + coerce_py(&"v", t)?, + nm + ), }) } diff --git a/uniffi_bindgen/src/bindings/swift/gen_swift.rs b/uniffi_bindgen/src/bindings/swift/gen_swift.rs index f70e8ef2a7..39c59ea21d 100644 --- a/uniffi_bindgen/src/bindings/swift/gen_swift.rs +++ b/uniffi_bindgen/src/bindings/swift/gen_swift.rs @@ -96,6 +96,7 @@ mod filters { } Type::Optional(type_) => format!("{}?", type_swift(type_)?), Type::Sequence(type_) => format!("[{}]", type_swift(type_)?), + Type::Map(type_) => format!("[String:{}]", type_swift(type_)?), }) } diff --git a/uniffi_bindgen/src/bindings/swift/templates/RustBufferHelper.swift b/uniffi_bindgen/src/bindings/swift/templates/RustBufferHelper.swift index a0c1faacf7..8d2dd4901b 100644 --- a/uniffi_bindgen/src/bindings/swift/templates/RustBufferHelper.swift +++ b/uniffi_bindgen/src/bindings/swift/templates/RustBufferHelper.swift @@ -130,16 +130,16 @@ protocol Serializable { // Types confirming to `ViaFfi` can be transferred back-and-for over the FFI. // This is analogous to the Rust trait of the same name. protocol ViaFfi: Serializable { - associatedtype Value - static func lift(_ v: Value) throws -> Self - func lower() -> Value + associatedtype FfiType + static func lift(_ v: FfiType) throws -> Self + func lower() -> FfiType } // Types conforming to `Primitive` pass themselves directly over the FFI. protocol Primitive {} extension Primitive { - typealias Value = Self + typealias FfiType = Self static func lift(_ v: Self) throws -> Self { return v @@ -155,7 +155,7 @@ extension Primitive { protocol ViaFfiUsingByteBuffer: Serializable {} extension ViaFfiUsingByteBuffer { - typealias Value = RustBuffer + typealias FfiType = RustBuffer static func lift(_ buf: RustBuffer) throws -> Self { let reader = Reader(data: Data(rustBuffer: buf)) @@ -177,16 +177,16 @@ extension ViaFfiUsingByteBuffer { // Implement our protocols for the built-in types that we use. extension String: ViaFfi { - typealias Value = UnsafeMutablePointer + typealias FfiType = UnsafeMutablePointer - static func lift(_ v: Value) throws -> Self { + static func lift(_ v: FfiType) throws -> Self { defer { {{ ci.ffi_string_free().name() }}(v) } return String(cString: v) } - func lower() -> Value { + func lower() -> FfiType { var rustErr = NativeRustError(code: 0, message: nil) let rustStr = {{ ci.ffi_string_alloc_from().name() }}(self, &rustErr) if rustErr.code != 0 { @@ -209,7 +209,7 @@ extension String: ViaFfi { extension Bool: ViaFfi { - typealias Value = UInt8 + typealias FfiType = UInt8 static func read(from buf: Reader) throws -> Bool { return try self.lift(buf.readInt()) @@ -366,3 +366,24 @@ extension Array: ViaFfiUsingByteBuffer, ViaFfi, Serializable where Element: Seri } } } + +extension Dictionary: ViaFfiUsingByteBuffer, ViaFfi, Serializable where Key == String, Value: Serializable { + static func read(from buf: Reader) throws -> Self { + let len: UInt32 = try buf.readInt() + var dict = [String: Value]() + dict.reserveCapacity(Int(len)) + for _ in 1...len { + dict[try String.read(from: buf)] = try Value.read(from: buf) + } + return dict + } + + func write(into buf: Writer) { + let len = UInt32(self.count) + buf.writeInt(len) + for (key, value) in self { + key.write(into: buf) + value.write(into: buf) + } + } +} diff --git a/uniffi_bindgen/src/interface/types.rs b/uniffi_bindgen/src/interface/types.rs index 5503827a49..6c3a5e3a23 100644 --- a/uniffi_bindgen/src/interface/types.rs +++ b/uniffi_bindgen/src/interface/types.rs @@ -94,6 +94,7 @@ pub enum Type { // Structurally recursive types. Optional(Box), Sequence(Box), + Map(/* String, */ Box), } /// When passing data across the FFI, each `Type` value will be lowered into a corresponding @@ -124,7 +125,9 @@ impl From<&Type> for FFIType { // Errors have their own special type. Type::Error(_) => FFIType::RustError, // Other types are serialized into a bytebuffer and deserialized on the other side. - Type::Record(_) | Type::Optional(_) | Type::Sequence(_) => FFIType::RustBuffer, + Type::Record(_) | Type::Optional(_) | Type::Sequence(_) | Type::Map(_) => { + FFIType::RustBuffer + } } } } @@ -257,6 +260,7 @@ impl TypeResolver for weedle::types::NonAnyType<'_> { weedle::types::NonAnyType::Integer(t) => t.resolve_type_definition(ci), weedle::types::NonAnyType::FloatingPoint(t) => t.resolve_type_definition(ci), weedle::types::NonAnyType::Sequence(t) => t.resolve_type_definition(ci), + weedle::types::NonAnyType::RecordType(t) => t.resolve_type_definition(ci), _ => bail!("no support for type {:?}", self), } } @@ -316,6 +320,14 @@ impl TypeResolver for weedle::types::SequenceType<'_> { } } +impl TypeResolver for weedle::types::RecordType<'_> { + fn resolve_type_definition(&self, ci: &ComponentInterface) -> Result { + Ok(Type::Map(Box::new( + (&self.generics.body.2).resolve_type_definition(ci)?, + ))) + } +} + impl TypeResolver for weedle::common::Identifier<'_> { fn resolve_type_definition(&self, ci: &ComponentInterface) -> Result { match resolve_builtin_type(self) { diff --git a/uniffi_bindgen/src/scaffolding.rs b/uniffi_bindgen/src/scaffolding.rs index cc5d25e4a2..2e50813b00 100644 --- a/uniffi_bindgen/src/scaffolding.rs +++ b/uniffi_bindgen/src/scaffolding.rs @@ -41,6 +41,7 @@ mod filters { } Type::Optional(t) => format!("Option<{}>", type_rs(t)?), Type::Sequence(t) => format!("Vec<{}>", type_rs(t)?), + Type::Map(t) => format!("std::collections::HashMap", type_rs(t)?), }) } diff --git a/uniffi_bindgen/src/templates/EnumTemplate.rs b/uniffi_bindgen/src/templates/EnumTemplate.rs index 284f9f5004..740e5beb14 100644 --- a/uniffi_bindgen/src/templates/EnumTemplate.rs +++ b/uniffi_bindgen/src/templates/EnumTemplate.rs @@ -8,9 +8,9 @@ // of items as declared in the rust code, but no harm will come from it. #} unsafe impl uniffi::ViaFfi for {{ e.name() }} { - type Value = u32; + type FfiType = u32; - fn lower(self) -> Self::Value { + fn lower(self) -> Self::FfiType { match self { // If the provided enum doesn't match the options defined in the IDL then // this match will fail to compile, with a type error to guide the way. @@ -20,7 +20,7 @@ unsafe impl uniffi::ViaFfi for {{ e.name() }} { } } - fn try_lift(v: Self::Value) -> uniffi::deps::anyhow::Result { + fn try_lift(v: Self::FfiType) -> uniffi::deps::anyhow::Result { Ok(match v { {%- for variant in e.variants() %} {{ loop.index }} => {{ e.name() }}::{{ variant }}, diff --git a/uniffi_bindgen/src/templates/RecordTemplate.rs b/uniffi_bindgen/src/templates/RecordTemplate.rs index 68d200811d..6e40e0d5bb 100644 --- a/uniffi_bindgen/src/templates/RecordTemplate.rs +++ b/uniffi_bindgen/src/templates/RecordTemplate.rs @@ -6,13 +6,13 @@ #} unsafe impl uniffi::ViaFfi for {{ rec.name() }} { - type Value = uniffi::deps::ffi_support::ByteBuffer; + type FfiType = uniffi::deps::ffi_support::ByteBuffer; - fn lower(self) -> Self::Value { + fn lower(self) -> Self::FfiType { uniffi::lower_into_bytebuffer(self) } - fn try_lift(v: Self::Value) -> uniffi::deps::anyhow::Result { + fn try_lift(v: Self::FfiType) -> uniffi::deps::anyhow::Result { uniffi::try_lift_from_bytebuffer(v) }