Skip to content

Commit

Permalink
Update docs and tests for HashMap<>/record<> (mozilla#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhammond authored Oct 2, 2023
1 parent 7acfce0 commit 2a8213d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/manual/src/udl/builtin_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The following built-in types can be passed as arguments/returned by Rust methods
| `&T` | `[ByRef] T` | This works for `&str` and `&[T]` |
| `Option<T>` | `T?` | |
| `Vec<T>` | `sequence<T>` | |
| `HashMap<String, T>` | `record<string, T>` | Only string keys are supported |
| `HashMap<K, V>` | `record<K, T>` | |
| `()` | `void` | Empty return |
| `Result<T, E>` | N/A | See [Errors](./errors.md) section |

Expand Down
13 changes: 12 additions & 1 deletion fixtures/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* 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::sync::Arc;
use std::{collections::HashMap, sync::Arc};

mod callback_interface;

Expand Down Expand Up @@ -116,6 +116,17 @@ fn take_two(two: Two) -> String {
two.a
}

#[uniffi::export]
fn make_hashmap(k: i8, v: u64) -> HashMap<i8, u64> {
HashMap::from([(k, v)])
}

// XXX - fails to call this from python - https://github.com/mozilla/uniffi-rs/issues/1774
#[uniffi::export]
fn return_hashmap(h: HashMap<i8, u64>) -> HashMap<i8, u64> {
h
}

#[uniffi::export]
fn take_record_with_bytes(rwb: RecordWithBytes) -> Vec<u8> {
rwb.some_bytes
Expand Down
5 changes: 5 additions & 0 deletions fixtures/proc-macro/tests/bindings/test_proc_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
assert(make_zero().inner == "ZERO")
assert(make_record_with_bytes().some_bytes == bytes([0, 1, 2, 3, 4]))

assert(make_hashmap(1, 2) == {1: 2})
# fails with AttributeError!? - https://github.com/mozilla/uniffi-rs/issues/1774
# d = {1, 2}
# assert(return_hashmap(d) == d)

try:
always_fails()
except BasicError.OsError:
Expand Down
5 changes: 1 addition & 4 deletions uniffi_core/src/ffi_converter_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,7 @@ unsafe impl<UT, T: FfiConverter<UT>> FfiConverter<UT> for Vec<T> {
MetadataBuffer::from_code(metadata::codes::TYPE_VEC).concat(T::TYPE_ID_META);
}

/// Support for associative arrays via the FFI.
/// Note that because of webidl limitations,
/// the key must always be of the String type.
///
/// Support for associative arrays via the FFI - `record<u32, u64>` in UDL.
/// HashMaps are currently always passed by serializing to a buffer.
/// We write a `i32` entries count followed by each entry (string
/// key followed by the value) in turn.
Expand Down

0 comments on commit 2a8213d

Please sign in to comment.