-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding
FfiType::Handle
and use it for Rust futures
FfiType::Handle is a opaque 64-bit handle that's used to pass objects across the FFI. This PR changes the rustfuture code to use it and also renames the type used to represent callback interfaces from `u64` to handle. The plan is to use it for all object types, including interfaces and trait interfaces, but that will be done in future PRs. Accompianing that code is the `HandleAlloc` trait, which has a general system for allocating/using/freeing handles for objects. It's essentially the same system(s) we're currently using, but now the code is all in one place. On the bindings side, switched the code to using the `u64` type rather than having a `UniffiHandle` type alias. I don't think the type alias is very useful since handles are only used internally. Also, I remember running into Swift issues with the type alias and multiple crates. Most of this code was copied from the handles PR (#1823).
- Loading branch information
Showing
22 changed files
with
271 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
// Handle from a UniffiHandleMap | ||
internal typealias UniffiHandle = Long | ||
|
||
// Map handles to objects | ||
// | ||
// This is used pass an opaque 64-bit handle representing a foreign object to the Rust code. | ||
internal class UniffiHandleMap<T: Any> { | ||
private val map = ConcurrentHashMap<UniffiHandle, T>() | ||
private val map = ConcurrentHashMap<Long, T>() | ||
private val counter = java.util.concurrent.atomic.AtomicLong(0) | ||
|
||
val size: Int | ||
get() = map.size | ||
|
||
// Insert a new object into the handle map and get a handle for it | ||
fun insert(obj: T): UniffiHandle { | ||
fun insert(obj: T): Long { | ||
val handle = counter.getAndAdd(1) | ||
map.put(handle, obj) | ||
return handle | ||
} | ||
|
||
// Get an object from the handle map | ||
fun get(handle: UniffiHandle): T { | ||
fun get(handle: Long): T { | ||
return map.get(handle) ?: throw InternalException("UniffiHandleMap.get: Invalid handle") | ||
} | ||
|
||
// Remove an entry from the handlemap and get the Kotlin object back | ||
fun remove(handle: UniffiHandle): T { | ||
fun remove(handle: Long): T { | ||
return map.remove(handle) ?: throw InternalException("UniffiHandleMap: Invalid handle") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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/. */ | ||
|
||
/// Object handle | ||
/// | ||
/// Handles opaque `u64` values used to pass objects across the FFI, both for objects implemented in | ||
/// Rust and ones implemented in the foreign language. | ||
/// | ||
/// Rust handles are generated by leaking a raw pointer | ||
/// Foreign handles are generated with a handle map that only generates odd values. | ||
/// For all currently supported architectures and hopefully any ones we add in the future: | ||
/// * 0 is an invalid value. | ||
/// * The lowest bit will always be set for foreign handles and never set for Rust ones (since the | ||
/// leaked pointer will be aligned). | ||
/// | ||
/// Rust handles are mainly managed is through the [crate::HandleAlloc] trait. | ||
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)] | ||
#[repr(transparent)] | ||
pub struct Handle(u64); | ||
|
||
impl Handle { | ||
pub fn from_pointer<T>(ptr: *const T) -> Self { | ||
Self(ptr as u64) | ||
} | ||
|
||
pub fn as_pointer<T>(&self) -> *const T { | ||
self.0 as *const T | ||
} | ||
|
||
pub fn from_raw(raw: u64) -> Option<Self> { | ||
if raw == 0 { | ||
None | ||
} else { | ||
Some(Self(raw)) | ||
} | ||
} | ||
|
||
pub fn from_raw_unchecked(raw: u64) -> Self { | ||
Self(raw) | ||
} | ||
|
||
pub fn as_raw(&self) -> u64 { | ||
self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.