@@ -1841,18 +1841,18 @@ impl Client {
1841
1841
1842
1842
async fn get_or_load_and_cache_server_info <
1843
1843
Value ,
1844
- MapFunction : Fn ( & ClientServerInfo ) -> Option < Value > ,
1844
+ MapFunction : Fn ( & ClientServerInfo ) -> CachedValue < Value > ,
1845
1845
> (
1846
1846
& self ,
1847
1847
map : MapFunction ,
1848
1848
) -> HttpResult < Value > {
1849
1849
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 ) {
1851
1851
return Ok ( val) ;
1852
1852
}
1853
1853
1854
1854
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) {
1856
1856
return Ok ( val) ;
1857
1857
}
1858
1858
@@ -1864,13 +1864,13 @@ impl Client {
1864
1864
versions. push ( MatrixVersion :: V1_0 ) ;
1865
1865
}
1866
1866
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 ) ;
1870
1870
1871
1871
// SAFETY: all fields were set above, so (assuming the caller doesn't attempt to
1872
1872
// 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 ( ) )
1874
1874
}
1875
1875
1876
1876
/// Get the Matrix versions supported by the homeserver by fetching them
@@ -1954,8 +1954,8 @@ impl Client {
1954
1954
pub async fn reset_server_info ( & self ) -> Result < ( ) > {
1955
1955
// Empty the in-memory caches.
1956
1956
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 ;
1959
1959
1960
1960
// Empty the store cache.
1961
1961
Ok ( self . state_store ( ) . remove_kv_data ( StateStoreDataKey :: ServerInfo ) . await ?)
@@ -2696,17 +2696,38 @@ impl WeakClient {
2696
2696
#[ derive( Clone ) ]
2697
2697
struct ClientServerInfo {
2698
2698
/// The Matrix versions the server supports (known ones only).
2699
- server_versions : Option < Box < [ MatrixVersion ] > > ,
2699
+ server_versions : CachedValue < Box < [ MatrixVersion ] > > ,
2700
2700
2701
2701
/// 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 > > ,
2703
2703
2704
2704
/// 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.
2705
2721
///
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
+ }
2710
2731
}
2711
2732
2712
2733
// The http mocking library is not supported for wasm32
0 commit comments