Skip to content

Commit eea3555

Browse files
committed
add missing wasm stubs
This adds stubs for wasm Uniffi does not support cfg attributes like previously used here. If _any_ platform exports it will generate the skeleton for _all_ platforms every time. Using uniffi::method attributes also will not fix this where we have different exports based on platform. Therefor stubs sadly are the only way to handle this as of now. Signed-off-by: MTRNord <MTRNord@users.noreply.github.com>
1 parent be2d039 commit eea3555

File tree

2 files changed

+104
-49
lines changed

2 files changed

+104
-49
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,6 @@ impl Client {
928928
}
929929
}
930930

931-
#[cfg(not(target_family = "wasm"))]
932931
#[matrix_sdk_ffi_macros::export]
933932
impl Client {
934933
/// Retrieves a media file from the media source
@@ -942,22 +941,54 @@ impl Client {
942941
use_cache: bool,
943942
temp_dir: Option<String>,
944943
) -> Result<Arc<MediaFileHandle>, ClientError> {
945-
let source = (*media_source).clone();
946-
let mime_type: mime::Mime = mime_type.parse()?;
944+
#[cfg(not(target_family = "wasm"))]
945+
{
946+
let source = (*media_source).clone();
947+
let mime_type: mime::Mime = mime_type.parse()?;
947948

948-
let handle = self
949-
.inner
950-
.media()
951-
.get_media_file(
952-
&MediaRequestParameters { source: source.media_source, format: MediaFormat::File },
953-
filename,
954-
&mime_type,
955-
use_cache,
956-
temp_dir,
957-
)
958-
.await?;
949+
let handle = self
950+
.inner
951+
.media()
952+
.get_media_file(
953+
&MediaRequestParameters {
954+
source: source.media_source,
955+
format: MediaFormat::File,
956+
},
957+
filename,
958+
&mime_type,
959+
use_cache,
960+
temp_dir,
961+
)
962+
.await?;
963+
964+
return Ok(Arc::new(MediaFileHandle::new(handle)));
965+
}
959966

960-
Ok(Arc::new(MediaFileHandle::new(handle)))
967+
#[cfg(target_family = "wasm")]
968+
return Err(ClientError::Generic {
969+
msg: "get_media_file is not supported on wasm platforms".to_owned(),
970+
details: None,
971+
});
972+
}
973+
974+
pub async fn set_display_name(&self, name: String) -> Result<(), ClientError> {
975+
#[cfg(not(target_family = "wasm"))]
976+
{
977+
self.inner
978+
.account()
979+
.set_display_name(Some(name.as_str()))
980+
.await
981+
.context("Unable to set display name")?;
982+
return Ok(());
983+
}
984+
985+
#[cfg(target_family = "wasm")]
986+
{
987+
Err(ClientError::Generic {
988+
msg: "set_display_name is not supported on wasm platforms".to_owned(),
989+
details: None,
990+
})
991+
}
961992
}
962993
}
963994

@@ -1093,15 +1124,6 @@ impl Client {
10931124
Ok(display_name)
10941125
}
10951126

1096-
pub async fn set_display_name(&self, name: String) -> Result<(), ClientError> {
1097-
self.inner
1098-
.account()
1099-
.set_display_name(Some(name.as_str()))
1100-
.await
1101-
.context("Unable to set display name")?;
1102-
Ok(())
1103-
}
1104-
11051127
pub async fn upload_avatar(&self, mime_type: String, data: Vec<u8>) -> Result<(), ClientError> {
11061128
let mime: Mime = mime_type.parse()?;
11071129
self.inner.account().upload_avatar(&mime, data).await?;
@@ -2453,25 +2475,25 @@ fn gen_transaction_id() -> String {
24532475

24542476
/// A file handle that takes ownership of a media file on disk. When the handle
24552477
/// is dropped, the file will be removed from the disk.
2456-
#[cfg(not(target_family = "wasm"))]
24572478
#[derive(uniffi::Object)]
24582479
pub struct MediaFileHandle {
2480+
#[cfg(not(target_family = "wasm"))]
24592481
inner: std::sync::RwLock<Option<SdkMediaFileHandle>>,
24602482
}
24612483

2462-
#[cfg(not(target_family = "wasm"))]
24632484
impl MediaFileHandle {
2485+
#[cfg(not(target_family = "wasm"))]
24642486
fn new(handle: SdkMediaFileHandle) -> Self {
24652487
Self { inner: std::sync::RwLock::new(Some(handle)) }
24662488
}
24672489
}
24682490

2469-
#[cfg(not(target_family = "wasm"))]
24702491
#[matrix_sdk_ffi_macros::export]
24712492
impl MediaFileHandle {
24722493
/// Get the media file's path.
24732494
pub fn path(&self) -> Result<String, ClientError> {
2474-
Ok(self
2495+
#[cfg(not(target_family = "wasm"))]
2496+
return Ok(self
24752497
.inner
24762498
.read()
24772499
.unwrap()
@@ -2480,24 +2502,37 @@ impl MediaFileHandle {
24802502
.path()
24812503
.to_str()
24822504
.unwrap()
2483-
.to_owned())
2505+
.to_owned());
2506+
#[cfg(target_family = "wasm")]
2507+
return Err(ClientError::Generic {
2508+
msg: "MediaFileHandle.path() is not supported on WASM targets".to_string(),
2509+
details: None,
2510+
});
24842511
}
24852512

24862513
pub fn persist(&self, path: String) -> Result<bool, ClientError> {
2487-
let mut guard = self.inner.write().unwrap();
2488-
Ok(
2489-
match guard
2490-
.take()
2491-
.context("MediaFileHandle was already persisted")?
2492-
.persist(path.as_ref())
2493-
{
2494-
Ok(_) => true,
2495-
Err(e) => {
2496-
*guard = Some(e.file);
2497-
false
2498-
}
2499-
},
2500-
)
2514+
#[cfg(not(target_family = "wasm"))]
2515+
{
2516+
let mut guard = self.inner.write().unwrap();
2517+
return Ok(
2518+
match guard
2519+
.take()
2520+
.context("MediaFileHandle was already persisted")?
2521+
.persist(path.as_ref())
2522+
{
2523+
Ok(_) => true,
2524+
Err(e) => {
2525+
*guard = Some(e.file);
2526+
false
2527+
}
2528+
},
2529+
);
2530+
}
2531+
#[cfg(target_family = "wasm")]
2532+
return Err(ClientError::Generic {
2533+
msg: "MediaFileHandle.persist() is not supported on WASM targets".to_string(),
2534+
details: None,
2535+
});
25012536
}
25022537
}
25032538

bindings/matrix-sdk-ffi/src/client_builder.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,20 @@ impl ClientBuilder {
152152
system_is_memory_constrained: false,
153153
username: None,
154154
homeserver_cfg: None,
155+
#[cfg(not(target_family = "wasm"))]
155156
user_agent: None,
156157
sliding_sync_version_builder: SlidingSyncVersionBuilder::None,
158+
#[cfg(not(target_family = "wasm"))]
157159
proxy: None,
160+
#[cfg(not(target_family = "wasm"))]
158161
disable_ssl_verification: false,
159162
disable_automatic_token_refresh: false,
160163
cross_process_store_locks_holder_name: None,
161164
enable_oidc_refresh_lock: false,
162165
session_delegate: None,
166+
#[cfg(not(target_family = "wasm"))]
163167
additional_root_certificates: Default::default(),
168+
#[cfg(not(target_family = "wasm"))]
164169
disable_built_in_root_certificates: false,
165170
encryption_settings: EncryptionSettings {
166171
auto_enable_cross_signing: false,
@@ -559,18 +564,23 @@ impl ClientBuilder {
559564
}
560565
}
561566

562-
#[cfg(not(target_family = "wasm"))]
563567
#[matrix_sdk_ffi_macros::export]
564568
impl ClientBuilder {
565569
pub fn proxy(self: Arc<Self>, url: String) -> Arc<Self> {
566570
let mut builder = unwrap_or_clone_arc(self);
567-
builder.proxy = Some(url);
571+
#[cfg(not(target_family = "wasm"))]
572+
{
573+
builder.proxy = Some(url);
574+
}
568575
Arc::new(builder)
569576
}
570577

571578
pub fn disable_ssl_verification(self: Arc<Self>) -> Arc<Self> {
572579
let mut builder = unwrap_or_clone_arc(self);
573-
builder.disable_ssl_verification = true;
580+
#[cfg(not(target_family = "wasm"))]
581+
{
582+
builder.disable_ssl_verification = true;
583+
}
574584
Arc::new(builder)
575585
}
576586

@@ -579,7 +589,11 @@ impl ClientBuilder {
579589
certificates: Vec<CertificateBytes>,
580590
) -> Arc<Self> {
581591
let mut builder = unwrap_or_clone_arc(self);
582-
builder.additional_root_certificates = certificates;
592+
593+
#[cfg(not(target_family = "wasm"))]
594+
{
595+
builder.additional_root_certificates = certificates;
596+
}
583597

584598
Arc::new(builder)
585599
}
@@ -589,13 +603,19 @@ impl ClientBuilder {
589603
/// [`add_root_certificates`][ClientBuilder::add_root_certificates].
590604
pub fn disable_built_in_root_certificates(self: Arc<Self>) -> Arc<Self> {
591605
let mut builder = unwrap_or_clone_arc(self);
592-
builder.disable_built_in_root_certificates = true;
606+
#[cfg(not(target_family = "wasm"))]
607+
{
608+
builder.disable_built_in_root_certificates = true;
609+
}
593610
Arc::new(builder)
594611
}
595612

596613
pub fn user_agent(self: Arc<Self>, user_agent: String) -> Arc<Self> {
597614
let mut builder = unwrap_or_clone_arc(self);
598-
builder.user_agent = Some(user_agent);
615+
#[cfg(not(target_family = "wasm"))]
616+
{
617+
builder.user_agent = Some(user_agent);
618+
}
599619
Arc::new(builder)
600620
}
601621
}

0 commit comments

Comments
 (0)