Skip to content

Commit

Permalink
ffi: add our own macro for processing exports
Browse files Browse the repository at this point in the history
Including one that will always warn if used with async functions, and
the other one always setting the tokio runtime if used for async stuff.
  • Loading branch information
bnjbvr committed Oct 8, 2024
1 parent 4bcb9b7 commit 736aa03
Show file tree
Hide file tree
Showing 38 changed files with 243 additions and 111 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ matrix-sdk = { path = "crates/matrix-sdk", version = "0.7.0", default-features =
matrix-sdk-base = { path = "crates/matrix-sdk-base", version = "0.7.0" }
matrix-sdk-common = { path = "crates/matrix-sdk-common", version = "0.7.0" }
matrix-sdk-crypto = { path = "crates/matrix-sdk-crypto", version = "0.7.0" }
matrix-sdk-ffi-macros = { path = "testing/matrix-sdk-ffi-macros", version = "0.7.0" }
matrix-sdk-indexeddb = { path = "crates/matrix-sdk-indexeddb", version = "0.7.0", default-features = false }
matrix-sdk-qrcode = { path = "crates/matrix-sdk-qrcode", version = "0.7.0" }
matrix-sdk-sqlite = { path = "crates/matrix-sdk-sqlite", version = "0.7.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions bindings/matrix-sdk-crypto-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ futures-util = { workspace = true }
hmac = "0.12.1"
http = { workspace = true }
matrix-sdk-common = { workspace = true, features = ["uniffi"] }
matrix-sdk-ffi-macros = { workspace = true }
pbkdf2 = "0.12.2"
rand = { workspace = true }
ruma = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl BackupRecoveryKey {
const PBKDF_ROUNDS: i32 = 500_000;
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl BackupRecoveryKey {
/// Create a new random [`BackupRecoveryKey`].
#[allow(clippy::new_without_default)]
Expand Down
6 changes: 3 additions & 3 deletions bindings/matrix-sdk-crypto-ffi/src/dehydrated_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Drop for DehydratedDevices {
}
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl DehydratedDevices {
pub fn create(&self) -> Result<Arc<DehydratedDevice>, DehydrationError> {
let inner = self.runtime.block_on(self.inner.create())?;
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Drop for RehydratedDevice {
}
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl RehydratedDevice {
pub fn receive_events(&self, events: String) -> Result<(), crate::CryptoStoreError> {
let events: Vec<Raw<AnyToDeviceEvent>> = serde_json::from_str(&events)?;
Expand All @@ -133,7 +133,7 @@ impl Drop for DehydratedDevice {
}
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl DehydratedDevice {
pub fn keys_for_upload(
&self,
Expand Down
18 changes: 9 additions & 9 deletions bindings/matrix-sdk-crypto-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl From<anyhow::Error> for MigrationError {
///
/// * `progress_listener` - A callback that can be used to introspect the
/// progress of the migration.
#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
pub fn migrate(
data: MigrationData,
path: String,
Expand Down Expand Up @@ -359,7 +359,7 @@ async fn save_changes(
///
/// * `progress_listener` - A callback that can be used to introspect the
/// progress of the migration.
#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
pub fn migrate_sessions(
data: SessionMigrationData,
path: String,
Expand Down Expand Up @@ -532,7 +532,7 @@ fn collect_sessions(
/// * `passphrase` - The passphrase that should be used to encrypt the data at
/// rest in the Sqlite store. **Warning**, if no passphrase is given, the
/// store and all its data will remain unencrypted.
#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
pub fn migrate_room_settings(
room_settings: HashMap<String, RoomSettings>,
path: String,
Expand All @@ -558,7 +558,7 @@ pub fn migrate_room_settings(
}

/// Callback that will be passed over the FFI to report progress
#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait ProgressListener {
/// The callback that should be called on the Rust side
///
Expand Down Expand Up @@ -794,7 +794,7 @@ pub struct BackupKeys {
backup_version: String,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl BackupKeys {
/// Get the recovery key that we're holding on to.
pub fn recovery_key(&self) -> Arc<BackupRecoveryKey> {
Expand Down Expand Up @@ -891,7 +891,7 @@ fn parse_user_id(user_id: &str) -> Result<OwnedUserId, CryptoStoreError> {
ruma::UserId::parse(user_id).map_err(|e| CryptoStoreError::InvalidUserId(user_id.to_owned(), e))
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
fn version_info() -> VersionInfo {
VersionInfo {
version: matrix_sdk_crypto::VERSION.to_owned(),
Expand All @@ -915,12 +915,12 @@ pub struct VersionInfo {
pub git_description: String,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
fn version() -> String {
matrix_sdk_crypto::VERSION.to_owned()
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
fn vodozemac_version() -> String {
vodozemac::VERSION.to_owned()
}
Expand All @@ -935,7 +935,7 @@ pub struct PkEncryption {
inner: matrix_sdk_crypto::vodozemac::pk_encryption::PkEncryption,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl PkEncryption {
/// Create a new [`PkEncryption`] object from a `Curve25519PublicKey`
/// encoded as Base64.
Expand Down
4 changes: 2 additions & 2 deletions bindings/matrix-sdk-crypto-ffi/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing_subscriber::{fmt::MakeWriter, EnvFilter};

/// Trait that can be used to forward Rust logs over FFI to a language specific
/// logger.
#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait Logger: Send {
/// Called every time the Rust side wants to post a log line.
fn log(&self, log_line: String);
Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct LoggerWrapper {
}

/// Set the logger that should be used to forward Rust logs over FFI.
#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
pub fn set_logger(logger: Box<dyn Logger>) {
let logger = LoggerWrapper { inner: Arc::new(Mutex::new(logger)) };

Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-crypto-ffi/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl From<RustSignatureCheckResult> for SignatureVerification {
}
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl OlmMachine {
/// Create a new `OlmMachine`
///
Expand Down
14 changes: 7 additions & 7 deletions bindings/matrix-sdk-crypto-ffi/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{CryptoStoreError, OutgoingVerificationRequest, SignatureUploadReques

/// Listener that will be passed over the FFI to report changes to a SAS
/// verification.
#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait SasListener: Send {
/// The callback that should be called on the Rust side
///
Expand Down Expand Up @@ -82,7 +82,7 @@ pub struct Verification {
pub(crate) runtime: Handle,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl Verification {
/// Try to represent the `Verification` as an `Sas` verification object,
/// returns `None` if the verification is not a `Sas` verification.
Expand Down Expand Up @@ -112,7 +112,7 @@ pub struct Sas {
pub(crate) runtime: Handle,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl Sas {
/// Get the user id of the other side.
pub fn other_user_id(&self) -> String {
Expand Down Expand Up @@ -276,7 +276,7 @@ impl Sas {

/// Listener that will be passed over the FFI to report changes to a QrCode
/// verification.
#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait QrCodeListener: Send {
/// The callback that should be called on the Rust side
///
Expand Down Expand Up @@ -328,7 +328,7 @@ pub struct QrCode {
pub(crate) runtime: Handle,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl QrCode {
/// Get the user id of the other side.
pub fn other_user_id(&self) -> String {
Expand Down Expand Up @@ -522,7 +522,7 @@ pub struct ConfirmVerificationResult {

/// Listener that will be passed over the FFI to report changes to a
/// verification request.
#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait VerificationRequestListener: Send {
/// The callback that should be called on the Rust side
///
Expand Down Expand Up @@ -562,7 +562,7 @@ pub struct VerificationRequest {
pub(crate) runtime: Handle,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl VerificationRequest {
/// The id of the other user that is participating in this verification
/// request.
Expand Down
1 change: 1 addition & 0 deletions bindings/matrix-sdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ eyeball-im = { workspace = true }
extension-trait = "1.0.1"
futures-util = { workspace = true }
log-panics = { version = "2", features = ["with-backtrace"] }
matrix-sdk-ffi-macros = { workspace = true }
matrix-sdk-ui = { workspace = true, features = ["uniffi"] }
mime = "0.3.16"
once_cell = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions bindings/matrix-sdk-ffi/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct HomeserverLoginDetails {
pub(crate) supports_password_login: bool,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl HomeserverLoginDetails {
/// The URL of the currently configured homeserver.
pub fn url(&self) -> String {
Expand Down Expand Up @@ -62,7 +62,7 @@ pub struct SsoHandler {
pub(crate) url: String,
}

#[uniffi::export(async_runtime = "tokio")]
#[matrix_sdk_ffi_macros::export_async]
impl SsoHandler {
/// Returns the URL for starting SSO authentication. The URL should be
/// opened in a web view. Once the web view succeeds, call `finish` with
Expand Down
18 changes: 9 additions & 9 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,25 @@ impl From<PushFormat> for RumaPushFormat {
}
}

#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait ClientDelegate: Sync + Send {
fn did_receive_auth_error(&self, is_soft_logout: bool);
fn did_refresh_tokens(&self);
}

#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait ClientSessionDelegate: Sync + Send {
fn retrieve_session_from_keychain(&self, user_id: String) -> Result<Session, ClientError>;
fn save_session_in_keychain(&self, session: Session);
}

#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait ProgressWatcher: Send + Sync {
fn transmission_progress(&self, progress: TransmissionProgress);
}

/// A listener to the global (client-wide) error reporter of the send queue.
#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait SendQueueRoomErrorListener: Sync + Send {
/// Called every time the send queue has ran into an error for a given room,
/// which will disable the send queue for that particular room.
Expand Down Expand Up @@ -260,7 +260,7 @@ impl Client {
}
}

#[uniffi::export(async_runtime = "tokio")]
#[matrix_sdk_ffi_macros::export_async]
impl Client {
/// Information about login options for the client's homeserver.
pub async fn homeserver_login_details(&self) -> Arc<HomeserverLoginDetails> {
Expand Down Expand Up @@ -526,7 +526,7 @@ impl Client {
}
}

#[uniffi::export(async_runtime = "tokio")]
#[matrix_sdk_ffi_macros::export_async]
impl Client {
/// The sliding sync version.
pub fn sliding_sync_version(&self) -> SlidingSyncVersion {
Expand Down Expand Up @@ -1092,7 +1092,7 @@ impl Client {
}
}

#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait IgnoredUsersListener: Sync + Send {
fn call(&self, ignored_user_ids: Vec<String>);
}
Expand Down Expand Up @@ -1649,7 +1649,7 @@ impl From<AccountManagementAction> for AccountManagementActionFull {
}
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
fn gen_transaction_id() -> String {
TransactionId::new().to_string()
}
Expand All @@ -1667,7 +1667,7 @@ impl MediaFileHandle {
}
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl MediaFileHandle {
/// Get the media file's path.
pub fn path(&self) -> Result<String, ClientError> {
Expand Down
6 changes: 3 additions & 3 deletions bindings/matrix-sdk-ffi/src/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct QrCodeData {
inner: qrcode::QrCodeData,
}

#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
impl QrCodeData {
/// Attempt to decode a slice of bytes into a [`QrCodeData`] object.
///
Expand Down Expand Up @@ -159,7 +159,7 @@ pub enum QrLoginProgress {
Done,
}

#[uniffi::export(callback_interface)]
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait QrLoginProgressListener: Sync + Send {
fn on_update(&self, state: QrLoginProgress);
}
Expand Down Expand Up @@ -270,7 +270,7 @@ pub struct ClientBuilder {
request_config: Option<RequestConfig>,
}

#[uniffi::export(async_runtime = "tokio")]
#[matrix_sdk_ffi_macros::export_async]
impl ClientBuilder {
#[uniffi::constructor]
pub fn new() -> Arc<Self> {
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct ElementWellKnown {
}

/// Helper function to parse a string into a ElementWellKnown struct
#[uniffi::export]
#[matrix_sdk_ffi_macros::export]
pub fn make_element_well_known(string: String) -> Result<ElementWellKnown, ClientError> {
serde_json::from_str(&string).map_err(ClientError::new)
}
Loading

0 comments on commit 736aa03

Please sign in to comment.