Skip to content

Commit 675963e

Browse files
pixlwavepoljar
authored andcommitted
chore: Make the RTC foci crash fix type-safe.
1 parent d30dae3 commit 675963e

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

crates/matrix-sdk/src/client/builder/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ use crate::encryption::EncryptionSettings;
4040
use crate::http_client::HttpSettings;
4141
use crate::{
4242
authentication::{oauth::OAuthCtx, AuthCtx},
43-
client::ClientServerInfo,
43+
client::{
44+
CachedValue::{Cached, NotSet},
45+
ClientServerInfo,
46+
},
4447
config::RequestConfig,
4548
error::RumaApiError,
4649
http_client::HttpClient,
@@ -561,9 +564,12 @@ impl ClientBuilder {
561564
let send_queue = Arc::new(SendQueueData::new(true));
562565

563566
let server_info = ClientServerInfo {
564-
server_versions: self.server_versions,
565-
unstable_features: None,
566-
well_known: Some(well_known.map(Into::into)),
567+
server_versions: match self.server_versions {
568+
Some(versions) => Cached(versions),
569+
None => NotSet,
570+
},
571+
unstable_features: NotSet,
572+
well_known: Cached(well_known.map(Into::into)),
567573
};
568574

569575
let event_cache = OnceCell::new();

crates/matrix-sdk/src/client/mod.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,18 +1841,18 @@ impl Client {
18411841

18421842
async fn get_or_load_and_cache_server_info<
18431843
Value,
1844-
MapFunction: Fn(&ClientServerInfo) -> Option<Value>,
1844+
MapFunction: Fn(&ClientServerInfo) -> CachedValue<Value>,
18451845
>(
18461846
&self,
18471847
map: MapFunction,
18481848
) -> HttpResult<Value> {
18491849
let server_info = &self.inner.caches.server_info;
1850-
if let Some(val) = map(&*server_info.read().await) {
1850+
if let CachedValue::Cached(val) = map(&*server_info.read().await) {
18511851
return Ok(val);
18521852
}
18531853

18541854
let mut guarded_server_info = server_info.write().await;
1855-
if let Some(val) = map(&guarded_server_info) {
1855+
if let CachedValue::Cached(val) = map(&guarded_server_info) {
18561856
return Ok(val);
18571857
}
18581858

@@ -1864,13 +1864,13 @@ impl Client {
18641864
versions.push(MatrixVersion::V1_0);
18651865
}
18661866

1867-
guarded_server_info.server_versions = Some(versions.into());
1868-
guarded_server_info.unstable_features = Some(server_info.unstable_features);
1869-
guarded_server_info.well_known = Some(server_info.well_known);
1867+
guarded_server_info.server_versions = CachedValue::Cached(versions.into());
1868+
guarded_server_info.unstable_features = CachedValue::Cached(server_info.unstable_features);
1869+
guarded_server_info.well_known = CachedValue::Cached(server_info.well_known);
18701870

18711871
// SAFETY: all fields were set above, so (assuming the caller doesn't attempt to
18721872
// fetch an optional property), the function will always return some.
1873-
Ok(map(&guarded_server_info).unwrap())
1873+
Ok(map(&guarded_server_info).unwrap_cached_value())
18741874
}
18751875

18761876
/// Get the Matrix versions supported by the homeserver by fetching them
@@ -1954,8 +1954,8 @@ impl Client {
19541954
pub async fn reset_server_info(&self) -> Result<()> {
19551955
// Empty the in-memory caches.
19561956
let mut guard = self.inner.caches.server_info.write().await;
1957-
guard.server_versions = None;
1958-
guard.unstable_features = None;
1957+
guard.server_versions = CachedValue::NotSet;
1958+
guard.unstable_features = CachedValue::NotSet;
19591959

19601960
// Empty the store cache.
19611961
Ok(self.state_store().remove_kv_data(StateStoreDataKey::ServerInfo).await?)
@@ -2696,17 +2696,38 @@ impl WeakClient {
26962696
#[derive(Clone)]
26972697
struct ClientServerInfo {
26982698
/// The Matrix versions the server supports (known ones only).
2699-
server_versions: Option<Box<[MatrixVersion]>>,
2699+
server_versions: CachedValue<Box<[MatrixVersion]>>,
27002700

27012701
/// The unstable features and their on/off state on the server.
2702-
unstable_features: Option<BTreeMap<String, bool>>,
2702+
unstable_features: CachedValue<BTreeMap<String, bool>>,
27032703

27042704
/// The server's well-known file, if any.
2705+
well_known: CachedValue<Option<WellKnownResponse>>,
2706+
}
2707+
2708+
/// A cached value that can either be set or not set, used to avoid confusion
2709+
/// between a value that is set to `None` (because it doesn't exist) and a value
2710+
/// that has not been cached yet.
2711+
#[derive(Clone)]
2712+
enum CachedValue<Value> {
2713+
/// A value has been cached.
2714+
Cached(Value),
2715+
/// Nothing has been cached yet.
2716+
NotSet,
2717+
}
2718+
2719+
impl<Value> CachedValue<Value> {
2720+
/// Unwraps the cached value, returning it if it exists.
27052721
///
2706-
/// Note: The outer `Option` represents whether a value has been set or
2707-
/// not, and the inner `Option` represents whether the server has a
2708-
/// well-known file or not.
2709-
well_known: Option<Option<WellKnownResponse>>,
2722+
/// # Panics
2723+
///
2724+
/// If the cached value is not set, this will panic.
2725+
fn unwrap_cached_value(self) -> Value {
2726+
match self {
2727+
CachedValue::Cached(value) => value,
2728+
CachedValue::NotSet => panic!("Tried to unwrap a cached value that wasn't set"),
2729+
}
2730+
}
27102731
}
27112732

27122733
// The http mocking library is not supported for wasm32

0 commit comments

Comments
 (0)