[fractal] misc: Remove RefCell or OnceCell wrapping WeakRefs



commit d19bb254d5e420db70269b9bc4991f398e66caa2
Author: Kévin Commaille <zecakeh tedomum fr>
Date:   Wed Sep 28 17:32:50 2022 +0200

    misc: Remove RefCell or OnceCell wrapping WeakRefs

 src/application.rs                                       | 12 ++++--------
 src/components/auth_dialog.rs                            | 11 ++++-------
 src/session/account_settings/devices_page/device.rs      |  9 +++------
 src/session/account_settings/devices_page/device_list.rs | 11 ++++-------
 src/session/account_settings/mod.rs                      | 14 ++++----------
 .../security_page/import_export_keys_subpage.rs          | 12 +++---------
 src/session/account_settings/security_page/mod.rs        | 14 +++-----------
 .../user_page/change_password_subpage.rs                 | 12 +++---------
 .../user_page/deactivate_account_subpage.rs              | 10 +++-------
 src/session/account_settings/user_page/mod.rs            | 12 +++---------
 src/session/avatar.rs                                    | 11 ++++-------
 src/session/content/explore/mod.rs                       | 12 +++---------
 src/session/content/explore/public_room_list.rs          | 12 +++---------
 src/session/content/mod.rs                               | 12 +++---------
 src/session/content/room_history/item_row.rs             | 10 +++-------
 src/session/content/verification/session_verification.rs |  9 ++++-----
 src/session/media_viewer.rs                              | 12 +++---------
 src/session/room/event/mod.rs                            | 16 ++++------------
 src/session/room/member_list.rs                          | 11 ++++-------
 src/session/room/mod.rs                                  |  9 +++------
 src/session/room/timeline/mod.rs                         | 15 ++++++---------
 src/session/room_creation/mod.rs                         | 14 +++-----------
 src/session/room_list.rs                                 | 11 ++++-------
 src/session/sidebar/row.rs                               | 11 ++++-------
 src/session/user.rs                                      | 15 +++------------
 src/session/verification/identity_verification.rs        |  8 ++++----
 src/session/verification/verification_list.rs            | 11 ++++-------
 27 files changed, 96 insertions(+), 220 deletions(-)
---
diff --git a/src/application.rs b/src/application.rs
index 038d6278c..b6c7c6c1e 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -9,13 +9,12 @@ use crate::{config, Window};
 
 mod imp {
     use adw::subclass::prelude::AdwApplicationImpl;
-    use once_cell::unsync::OnceCell;
 
     use super::*;
 
     #[derive(Debug)]
     pub struct Application {
-        pub window: OnceCell<WeakRef<Window>>,
+        pub window: WeakRef<Window>,
         pub settings: Settings,
     }
 
@@ -41,17 +40,14 @@ mod imp {
         fn activate(&self, app: &Self::Type) {
             debug!("GtkApplication<Application>::activate");
 
-            if let Some(window) = self.window.get() {
-                let window = window.upgrade().unwrap();
+            if let Some(window) = self.window.upgrade() {
                 window.show();
                 window.present();
                 return;
             }
 
             let window = Window::new(app);
-            self.window
-                .set(window.downgrade())
-                .expect("Window already set.");
+            self.window.set(Some(&window));
 
             app.setup_gactions();
             app.setup_accels();
@@ -100,7 +96,7 @@ impl Application {
     }
 
     fn get_main_window(&self) -> Window {
-        self.imp().window.get().unwrap().upgrade().unwrap()
+        self.imp().window.upgrade().unwrap()
     }
 
     pub fn settings(&self) -> Settings {
diff --git a/src/components/auth_dialog.rs b/src/components/auth_dialog.rs
index 71cbe057c..e5f84f5fc 100644
--- a/src/components/auth_dialog.rs
+++ b/src/components/auth_dialog.rs
@@ -85,14 +85,14 @@ mod imp {
         subclass::{InitializingObject, Signal},
         SignalHandlerId,
     };
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/Fractal/components-auth-dialog.ui")]
     pub struct AuthDialog {
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub stack: TemplateChild<gtk::Stack>,
         #[template_child]
@@ -155,10 +155,7 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "session" => self
-                    .session
-                    .set(value.get::<Session>().unwrap().downgrade())
-                    .unwrap(),
+                "session" => self.session.set(value.get::<Session>().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -222,7 +219,7 @@ impl AuthDialog {
     }
 
     pub fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
     /// Authenticates the user to the server via an authentication flow.
diff --git a/src/session/account_settings/devices_page/device.rs 
b/src/session/account_settings/devices_page/device.rs
index f426c8165..a2d90b6dd 100644
--- a/src/session/account_settings/devices_page/device.rs
+++ b/src/session/account_settings/devices_page/device.rs
@@ -22,7 +22,7 @@ mod imp {
     pub struct Device {
         pub device: OnceCell<MatrixDevice>,
         pub crypto_device: OnceCell<CryptoDevice>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
     }
 
     #[glib::object_subclass]
@@ -91,10 +91,7 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "session" => self
-                    .session
-                    .set(value.get::<Session>().unwrap().downgrade())
-                    .unwrap(),
+                "session" => self.session.set(value.get().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -133,7 +130,7 @@ impl Device {
     }
 
     pub fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
     fn set_matrix_device(&self, device: MatrixDevice, crypto_device: Option<CryptoDevice>) {
diff --git a/src/session/account_settings/devices_page/device_list.rs 
b/src/session/account_settings/devices_page/device_list.rs
index 003117a7c..9b28bba4b 100644
--- a/src/session/account_settings/devices_page/device_list.rs
+++ b/src/session/account_settings/devices_page/device_list.rs
@@ -13,14 +13,14 @@ mod imp {
     use std::cell::{Cell, RefCell};
 
     use glib::object::WeakRef;
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
     #[derive(Debug, Default)]
     pub struct DeviceList {
         pub list: RefCell<Vec<DeviceItem>>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         pub current_device: RefCell<Option<DeviceItem>>,
         pub loading: Cell<bool>,
     }
@@ -64,10 +64,7 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "session" => self
-                    .session
-                    .set(value.get::<Session>().unwrap().downgrade())
-                    .unwrap(),
+                "session" => self.session.set(value.get().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -115,7 +112,7 @@ impl DeviceList {
     }
 
     pub fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
     fn set_loading(&self, loading: bool) {
diff --git a/src/session/account_settings/mod.rs b/src/session/account_settings/mod.rs
index 7d97a9110..c89aad727 100644
--- a/src/session/account_settings/mod.rs
+++ b/src/session/account_settings/mod.rs
@@ -24,7 +24,7 @@ mod imp {
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/Fractal/account-settings.ui")]
     pub struct AccountSettings {
-        pub session: RefCell<Option<WeakRef<Session>>>,
+        pub session: WeakRef<Session>,
         pub session_handler: RefCell<Option<glib::SignalHandlerId>>,
     }
 
@@ -98,7 +98,7 @@ mod imp {
         }
 
         fn dispose(&self, _obj: &Self::Type) {
-            if let Some(session) = self.session.take().and_then(|session| session.upgrade()) {
+            if let Some(session) = self.session.upgrade() {
                 if let Some(handler) = self.session_handler.take() {
                     session.disconnect(handler);
                 }
@@ -125,11 +125,7 @@ impl AccountSettings {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .borrow()
-            .clone()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn set_session(&self, session: Option<Session>) {
@@ -155,9 +151,7 @@ impl AccountSettings {
                 )));
         }
 
-        self.imp()
-            .session
-            .replace(session.map(|session| session.downgrade()));
+        self.imp().session.set(session.as_ref());
         self.notify("session");
     }
 }
diff --git a/src/session/account_settings/security_page/import_export_keys_subpage.rs 
b/src/session/account_settings/security_page/import_export_keys_subpage.rs
index 175a2face..9ed93ea4b 100644
--- a/src/session/account_settings/security_page/import_export_keys_subpage.rs
+++ b/src/session/account_settings/security_page/import_export_keys_subpage.rs
@@ -33,14 +33,13 @@ mod imp {
     use std::cell::{Cell, RefCell};
 
     use glib::{subclass::InitializingObject, WeakRef};
-    use once_cell::unsync::OnceCell;
 
     use super::*;
 
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/Fractal/account-settings-import-export-keys-subpage.ui")]
     pub struct ImportExportKeysSubpage {
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub title: TemplateChild<gtk::Label>,
         #[template_child]
@@ -181,16 +180,11 @@ impl ImportExportKeysSubpage {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .get()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn set_session(&self, session: Option<Session>) {
-        if let Some(session) = session {
-            self.imp().session.set(session.downgrade()).unwrap();
-        }
+        self.imp().session.set(session.as_ref());
     }
 
     pub fn file_path(&self) -> Option<gio::File> {
diff --git a/src/session/account_settings/security_page/mod.rs 
b/src/session/account_settings/security_page/mod.rs
index 7154e7ca7..454a46aab 100644
--- a/src/session/account_settings/security_page/mod.rs
+++ b/src/session/account_settings/security_page/mod.rs
@@ -7,8 +7,6 @@ mod import_export_keys_subpage;
 use import_export_keys_subpage::{ImportExportKeysSubpage, KeysSubpageMode};
 
 mod imp {
-    use std::cell::RefCell;
-
     use glib::{subclass::InitializingObject, WeakRef};
 
     use super::*;
@@ -16,7 +14,7 @@ mod imp {
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/Fractal/account-settings-security-page.ui")]
     pub struct SecurityPage {
-        pub session: RefCell<Option<WeakRef<Session>>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub import_export_keys_subpage: TemplateChild<ImportExportKeysSubpage>,
     }
@@ -93,11 +91,7 @@ impl SecurityPage {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .borrow()
-            .clone()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn set_session(&self, session: Option<Session>) {
@@ -105,9 +99,7 @@ impl SecurityPage {
             return;
         }
 
-        self.imp()
-            .session
-            .replace(session.map(|session| session.downgrade()));
+        self.imp().session.set(session.as_ref());
         self.notify("session");
     }
 
diff --git a/src/session/account_settings/user_page/change_password_subpage.rs 
b/src/session/account_settings/user_page/change_password_subpage.rs
index 157d04167..dd2ff237e 100644
--- a/src/session/account_settings/user_page/change_password_subpage.rs
+++ b/src/session/account_settings/user_page/change_password_subpage.rs
@@ -25,14 +25,13 @@ use crate::{
 
 mod imp {
     use glib::{subclass::InitializingObject, WeakRef};
-    use once_cell::unsync::OnceCell;
 
     use super::*;
 
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/Fractal/account-settings-change-password-subpage.ui")]
     pub struct ChangePasswordSubpage {
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub password: TemplateChild<PasswordEntryRow>,
         #[template_child]
@@ -177,16 +176,11 @@ impl ChangePasswordSubpage {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .get()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn set_session(&self, session: Option<Session>) {
-        if let Some(session) = session {
-            self.imp().session.set(session.downgrade()).unwrap();
-        }
+        self.imp().session.set(session.as_ref());
     }
 
     fn validate_password(&self) {
diff --git a/src/session/account_settings/user_page/deactivate_account_subpage.rs 
b/src/session/account_settings/user_page/deactivate_account_subpage.rs
index 457d0a467..3cad5ccc8 100644
--- a/src/session/account_settings/user_page/deactivate_account_subpage.rs
+++ b/src/session/account_settings/user_page/deactivate_account_subpage.rs
@@ -15,14 +15,13 @@ use crate::{
 
 mod imp {
     use glib::{subclass::InitializingObject, WeakRef};
-    use once_cell::unsync::OnceCell;
 
     use super::*;
 
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/Fractal/account-settings-deactivate-account-subpage.ui")]
     pub struct DeactivateAccountSubpage {
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub confirmation: TemplateChild<adw::EntryRow>,
         #[template_child]
@@ -123,16 +122,13 @@ impl DeactivateAccountSubpage {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .get()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn set_session(&self, session: Option<Session>) {
         if let Some(session) = session {
             let priv_ = self.imp();
-            priv_.session.set(session.downgrade()).unwrap();
+            priv_.session.set(Some(&session));
             priv_.confirmation.set_title(&self.user_id());
         }
     }
diff --git a/src/session/account_settings/user_page/mod.rs b/src/session/account_settings/user_page/mod.rs
index 6794b0a0e..4cf7a7f86 100644
--- a/src/session/account_settings/user_page/mod.rs
+++ b/src/session/account_settings/user_page/mod.rs
@@ -33,7 +33,7 @@ mod imp {
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/Fractal/account-settings-user-page.ui")]
     pub struct UserPage {
-        pub session: RefCell<Option<WeakRef<Session>>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub avatar: TemplateChild<EditableAvatar>,
         #[template_child]
@@ -141,20 +141,14 @@ impl UserPage {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .borrow()
-            .clone()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn set_session(&self, session: Option<Session>) {
         if self.session() == session {
             return;
         }
-        self.imp()
-            .session
-            .replace(session.map(|session| session.downgrade()));
+        self.imp().session.set(session.as_ref());
         self.notify("session");
 
         self.user().avatar().connect_notify_local(
diff --git a/src/session/avatar.rs b/src/session/avatar.rs
index 8df52fbc2..336231758 100644
--- a/src/session/avatar.rs
+++ b/src/session/avatar.rs
@@ -19,7 +19,7 @@ mod imp {
     use std::cell::{Cell, RefCell};
 
     use glib::object::WeakRef;
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
@@ -29,7 +29,7 @@ mod imp {
         pub needed_size: Cell<u32>,
         pub url: RefCell<Option<OwnedMxcUri>>,
         pub display_name: RefCell<Option<String>>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
     }
 
     #[glib::object_subclass]
@@ -95,10 +95,7 @@ mod imp {
             match pspec.name() {
                 "needed-size" => obj.set_needed_size(value.get().unwrap()),
                 "url" => obj.set_url(value.get::<Option<&str>>().unwrap().map(Into::into)),
-                "session" => self
-                    .session
-                    .set(value.get::<Session>().unwrap().downgrade())
-                    .unwrap(),
+                "session" => self.session.set(value.get::<Session>().ok().as_ref()),
                 "display-name" => obj.set_display_name(value.get().unwrap()),
                 _ => unimplemented!(),
             }
@@ -137,7 +134,7 @@ impl Avatar {
     }
 
     fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
     pub fn image(&self) -> Option<gdk::Paintable> {
diff --git a/src/session/content/explore/mod.rs b/src/session/content/explore/mod.rs
index 34d802b2f..4b94dc16d 100644
--- a/src/session/content/explore/mod.rs
+++ b/src/session/content/explore/mod.rs
@@ -24,7 +24,7 @@ mod imp {
     #[template(resource = "/org/gnome/Fractal/content-explore.ui")]
     pub struct Explore {
         pub compact: Cell<bool>,
-        pub session: RefCell<Option<WeakRef<Session>>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub stack: TemplateChild<gtk::Stack>,
         #[template_child]
@@ -146,11 +146,7 @@ impl Explore {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .borrow()
-            .as_ref()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn init(&self) {
@@ -192,9 +188,7 @@ impl Explore {
             priv_.public_room_list.replace(Some(public_room_list));
         }
 
-        priv_
-            .session
-            .replace(session.map(|session| session.downgrade()));
+        priv_.session.set(session.as_ref());
         self.notify("session");
     }
 
diff --git a/src/session/content/explore/public_room_list.rs b/src/session/content/explore/public_room_list.rs
index 3824d31ea..987c2fe10 100644
--- a/src/session/content/explore/public_room_list.rs
+++ b/src/session/content/explore/public_room_list.rs
@@ -34,7 +34,7 @@ mod imp {
         pub loading: Cell<bool>,
         pub request_sent: Cell<bool>,
         pub total_room_count_estimate: Cell<Option<u64>>,
-        pub session: RefCell<Option<WeakRef<Session>>>,
+        pub session: WeakRef<Session>,
     }
 
     #[glib::object_subclass]
@@ -134,11 +134,7 @@ impl PublicRoomList {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .borrow()
-            .as_ref()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn set_session(&self, session: Option<Session>) {
@@ -146,9 +142,7 @@ impl PublicRoomList {
             return;
         }
 
-        self.imp()
-            .session
-            .replace(session.map(|session| session.downgrade()));
+        self.imp().session.set(session.as_ref());
         self.notify("session");
     }
 
diff --git a/src/session/content/mod.rs b/src/session/content/mod.rs
index f01464f84..fdaff61b1 100644
--- a/src/session/content/mod.rs
+++ b/src/session/content/mod.rs
@@ -31,7 +31,7 @@ mod imp {
     #[template(resource = "/org/gnome/Fractal/content.ui")]
     pub struct Content {
         pub compact: Cell<bool>,
-        pub session: RefCell<Option<WeakRef<Session>>>,
+        pub session: WeakRef<Session>,
         pub item: RefCell<Option<glib::Object>>,
         pub signal_handler: RefCell<Option<SignalHandlerId>>,
         #[template_child]
@@ -171,11 +171,7 @@ impl Content {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .borrow()
-            .as_ref()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     pub fn set_session(&self, session: Option<Session>) {
@@ -183,9 +179,7 @@ impl Content {
             return;
         }
 
-        self.imp()
-            .session
-            .replace(session.map(|session| session.downgrade()));
+        self.imp().session.set(session.as_ref());
         self.notify("session");
     }
 
diff --git a/src/session/content/room_history/item_row.rs b/src/session/content/room_history/item_row.rs
index 1511cf5cb..3ef51db8e 100644
--- a/src/session/content/room_history/item_row.rs
+++ b/src/session/content/room_history/item_row.rs
@@ -19,13 +19,12 @@ mod imp {
     use std::{cell::RefCell, rc::Rc};
 
     use glib::{signal::SignalHandlerId, WeakRef};
-    use once_cell::unsync::OnceCell;
 
     use super::*;
 
     #[derive(Debug, Default)]
     pub struct ItemRow {
-        pub room_history: OnceCell<WeakRef<RoomHistory>>,
+        pub room_history: WeakRef<RoomHistory>,
         pub item: RefCell<Option<TimelineItem>>,
         pub action_group: RefCell<Option<gio::SimpleActionGroup>>,
         pub notify_handler: RefCell<Option<SignalHandlerId>>,
@@ -75,10 +74,7 @@ mod imp {
         ) {
             match pspec.name() {
                 "item" => obj.set_item(value.get().unwrap()),
-                "room-history" => self
-                    .room_history
-                    .set(value.get::<RoomHistory>().unwrap().downgrade())
-                    .unwrap(),
+                "room-history" => self.room_history.set(value.get().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -179,7 +175,7 @@ impl ItemRow {
     }
 
     pub fn room_history(&self) -> RoomHistory {
-        self.imp().room_history.get().unwrap().upgrade().unwrap()
+        self.imp().room_history.upgrade().unwrap()
     }
 
     pub fn action_group(&self) -> Option<gio::SimpleActionGroup> {
diff --git a/src/session/content/verification/session_verification.rs 
b/src/session/content/verification/session_verification.rs
index 9f9ef4ff9..313101016 100644
--- a/src/session/content/verification/session_verification.rs
+++ b/src/session/content/verification/session_verification.rs
@@ -14,7 +14,6 @@ mod imp {
     use std::cell::RefCell;
 
     use glib::{subclass::InitializingObject, SignalHandlerId, WeakRef};
-    use once_cell::unsync::OnceCell;
 
     use super::*;
 
@@ -22,7 +21,7 @@ mod imp {
     #[template(resource = "/org/gnome/Fractal/session-verification.ui")]
     pub struct SessionVerification {
         pub request: RefCell<Option<IdentityVerification>>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub bootstrap_button: TemplateChild<SpinnerButton>,
         #[template_child]
@@ -143,11 +142,11 @@ impl SessionVerification {
 
     /// The current `Session`.
     pub fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
-    fn set_session(&self, session: Session) {
-        self.imp().session.set(session.downgrade()).unwrap()
+    fn set_session(&self, session: Option<Session>) {
+        self.imp().session.set(session.as_ref())
     }
 
     fn request(&self) -> Option<IdentityVerification> {
diff --git a/src/session/media_viewer.rs b/src/session/media_viewer.rs
index e0bf4804b..847c88df2 100644
--- a/src/session/media_viewer.rs
+++ b/src/session/media_viewer.rs
@@ -24,7 +24,7 @@ mod imp {
     #[template(resource = "/org/gnome/Fractal/media-viewer.ui")]
     pub struct MediaViewer {
         pub fullscreened: Cell<bool>,
-        pub event: RefCell<Option<WeakRef<SupportedEvent>>>,
+        pub event: WeakRef<SupportedEvent>,
         pub body: RefCell<Option<String>>,
         #[template_child]
         pub flap: TemplateChild<adw::Flap>,
@@ -154,11 +154,7 @@ impl MediaViewer {
     }
 
     pub fn event(&self) -> Option<SupportedEvent> {
-        self.imp()
-            .event
-            .borrow()
-            .as_ref()
-            .and_then(|event| event.upgrade())
+        self.imp().event.upgrade()
     }
 
     pub fn set_event(&self, event: Option<SupportedEvent>) {
@@ -166,9 +162,7 @@ impl MediaViewer {
             return;
         }
 
-        self.imp()
-            .event
-            .replace(event.map(|event| event.downgrade()));
+        self.imp().event.set(event.as_ref());
         self.build();
         self.notify("event");
     }
diff --git a/src/session/room/event/mod.rs b/src/session/room/event/mod.rs
index 507f3bb56..21c6ec346 100644
--- a/src/session/room/event/mod.rs
+++ b/src/session/room/event/mod.rs
@@ -24,7 +24,7 @@ mod imp {
     use std::cell::RefCell;
 
     use glib::{object::WeakRef, Class};
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
@@ -70,7 +70,7 @@ mod imp {
         pub pure_event: RefCell<Option<SyncTimelineEvent>>,
 
         /// The room containing this `Event`.
-        pub room: OnceCell<WeakRef<Room>>,
+        pub room: WeakRef<Room>,
     }
 
     #[glib::object_subclass]
@@ -133,9 +133,7 @@ mod imp {
                     obj.set_pure_event(event.0);
                 }
                 "room" => {
-                    self.room
-                        .set(value.get::<Room>().unwrap().downgrade())
-                        .unwrap();
+                    self.room.set(value.get().ok().as_ref());
                 }
                 _ => unimplemented!(),
             }
@@ -248,13 +246,7 @@ pub trait EventExt: 'static {
 
 impl<O: IsA<Event>> EventExt for O {
     fn room(&self) -> Room {
-        self.upcast_ref()
-            .imp()
-            .room
-            .get()
-            .unwrap()
-            .upgrade()
-            .unwrap()
+        self.upcast_ref().imp().room.upgrade().unwrap()
     }
 
     fn pure_event(&self) -> SyncTimelineEvent {
diff --git a/src/session/room/member_list.rs b/src/session/room/member_list.rs
index 23668fbab..7f03da70b 100644
--- a/src/session/room/member_list.rs
+++ b/src/session/room/member_list.rs
@@ -11,14 +11,14 @@ mod imp {
     use std::cell::RefCell;
 
     use glib::object::WeakRef;
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
     #[derive(Debug, Default)]
     pub struct MemberList {
         pub members: RefCell<IndexMap<OwnedUserId, Member>>,
-        pub room: OnceCell<WeakRef<Room>>,
+        pub room: WeakRef<Room>,
     }
 
     #[glib::object_subclass]
@@ -51,10 +51,7 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "room" => self
-                    .room
-                    .set(value.get::<Room>().unwrap().downgrade())
-                    .unwrap(),
+                "room" => self.room.set(value.get().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -98,7 +95,7 @@ impl MemberList {
     }
 
     pub fn room(&self) -> Room {
-        self.imp().room.get().unwrap().upgrade().unwrap()
+        self.imp().room.upgrade().unwrap()
     }
 
     /// Updates members with the given RoomMember values.
diff --git a/src/session/room/mod.rs b/src/session/room/mod.rs
index 8af3c94cb..e5256d5bd 100644
--- a/src/session/room/mod.rs
+++ b/src/session/room/mod.rs
@@ -82,7 +82,7 @@ mod imp {
     pub struct Room {
         pub room_id: OnceCell<OwnedRoomId>,
         pub matrix_room: RefCell<Option<MatrixRoom>>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         pub name: RefCell<Option<String>>,
         pub avatar: OnceCell<Avatar>,
         pub category: Cell<RoomType>,
@@ -259,10 +259,7 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "session" => self
-                    .session
-                    .set(value.get::<Session>().unwrap().downgrade())
-                    .unwrap(),
+                "session" => self.session.set(value.get().ok().as_ref()),
                 "display-name" => {
                     let room_name = value.get().unwrap();
                     obj.store_room_name(room_name)
@@ -380,7 +377,7 @@ impl Room {
     }
 
     pub fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
     pub fn room_id(&self) -> &RoomId {
diff --git a/src/session/room/timeline/mod.rs b/src/session/room/timeline/mod.rs
index c57414c00..bde0ee7a0 100644
--- a/src/session/room/timeline/mod.rs
+++ b/src/session/room/timeline/mod.rs
@@ -51,13 +51,13 @@ mod imp {
     use std::cell::{Cell, RefCell};
 
     use glib::object::WeakRef;
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
     #[derive(Debug, Default)]
     pub struct Timeline {
-        pub room: OnceCell<WeakRef<Room>>,
+        pub room: WeakRef<Room>,
         /// A store to keep track of related events that aren't known
         pub relates_to_events: RefCell<HashMap<OwnedEventId, Vec<OwnedEventId>>>,
         /// All events shown in the room history
@@ -121,10 +121,7 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "room" => {
-                    let room = value.get::<Room>().unwrap();
-                    obj.set_room(room);
-                }
+                "room" => obj.set_room(value.get().unwrap()),
                 _ => unimplemented!(),
             }
         }
@@ -837,12 +834,12 @@ impl Timeline {
         self.items_changed(0, 0, added as u32);
     }
 
-    fn set_room(&self, room: Room) {
-        self.imp().room.set(room.downgrade()).unwrap();
+    fn set_room(&self, room: Option<Room>) {
+        self.imp().room.set(room.as_ref());
     }
 
     pub fn room(&self) -> Room {
-        self.imp().room.get().unwrap().upgrade().unwrap()
+        self.imp().room.upgrade().unwrap()
     }
 
     fn set_state(&self, state: TimelineState) {
diff --git a/src/session/room_creation/mod.rs b/src/session/room_creation/mod.rs
index dc6e768a1..88f9bf9a5 100644
--- a/src/session/room_creation/mod.rs
+++ b/src/session/room_creation/mod.rs
@@ -26,8 +26,6 @@ use crate::{
 const MAX_BYTES: usize = 255;
 
 mod imp {
-    use std::cell::RefCell;
-
     use glib::{object::WeakRef, subclass::InitializingObject};
 
     use super::*;
@@ -35,7 +33,7 @@ mod imp {
     #[derive(Debug, Default, CompositeTemplate)]
     #[template(resource = "/org/gnome/Fractal/room-creation.ui")]
     pub struct RoomCreation {
-        pub session: RefCell<Option<WeakRef<Session>>>,
+        pub session: WeakRef<Session>,
         #[template_child]
         pub content: TemplateChild<gtk::ListBox>,
         #[template_child]
@@ -164,11 +162,7 @@ impl RoomCreation {
     }
 
     pub fn session(&self) -> Option<Session> {
-        self.imp()
-            .session
-            .borrow()
-            .as_ref()
-            .and_then(|session| session.upgrade())
+        self.imp().session.upgrade()
     }
 
     fn set_session(&self, session: Option<Session>) {
@@ -184,9 +178,7 @@ impl RoomCreation {
                 .set_label(&[":", user.user_id().server_name().as_str()].concat());
         }
 
-        priv_
-            .session
-            .replace(session.map(|session| session.downgrade()));
+        priv_.session.set(session.as_ref());
         self.notify("session");
     }
 
diff --git a/src/session/room_list.rs b/src/session/room_list.rs
index b6cc065c2..2c54fbdcc 100644
--- a/src/session/room_list.rs
+++ b/src/session/room_list.rs
@@ -18,7 +18,7 @@ mod imp {
     use std::cell::RefCell;
 
     use glib::{object::WeakRef, subclass::Signal};
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
@@ -26,7 +26,7 @@ mod imp {
     pub struct RoomList {
         pub list: RefCell<IndexMap<OwnedRoomId, Room>>,
         pub pending_rooms: RefCell<HashSet<OwnedRoomOrAliasId>>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
     }
 
     #[glib::object_subclass]
@@ -59,10 +59,7 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "session" => self
-                    .session
-                    .set(value.get::<Session>().unwrap().downgrade())
-                    .unwrap(),
+                "session" => self.session.set(value.get().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -122,7 +119,7 @@ impl RoomList {
     }
 
     pub fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
     pub fn is_pending_room(&self, identifier: &RoomOrAliasId) -> bool {
diff --git a/src/session/sidebar/row.rs b/src/session/sidebar/row.rs
index 569b9a549..0c375ba27 100644
--- a/src/session/sidebar/row.rs
+++ b/src/session/sidebar/row.rs
@@ -16,13 +16,13 @@ mod imp {
     use std::cell::RefCell;
 
     use glib::WeakRef;
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
     #[derive(Debug, Default)]
     pub struct Row {
-        pub sidebar: OnceCell<WeakRef<Sidebar>>,
+        pub sidebar: WeakRef<Sidebar>,
         pub list_row: RefCell<Option<gtk::TreeListRow>>,
         pub bindings: RefCell<Vec<glib::Binding>>,
     }
@@ -78,10 +78,7 @@ mod imp {
         ) {
             match pspec.name() {
                 "list-row" => obj.set_list_row(value.get().unwrap()),
-                "sidebar" => self
-                    .sidebar
-                    .set(value.get::<Sidebar>().unwrap().downgrade())
-                    .unwrap(),
+                "sidebar" => self.sidebar.set(value.get().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -133,7 +130,7 @@ impl Row {
     }
 
     pub fn sidebar(&self) -> Sidebar {
-        self.imp().sidebar.get().unwrap().upgrade().unwrap()
+        self.imp().sidebar.upgrade().unwrap()
     }
 
     pub fn item(&self) -> Option<SidebarItem> {
diff --git a/src/session/user.rs b/src/session/user.rs
index 5c6db51f8..26a6c2918 100644
--- a/src/session/user.rs
+++ b/src/session/user.rs
@@ -38,7 +38,7 @@ mod imp {
     pub struct User {
         pub user_id: OnceCell<OwnedUserId>,
         pub display_name: RefCell<Option<String>>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         pub avatar: OnceCell<Avatar>,
         pub is_verified: Cell<bool>,
     }
@@ -118,10 +118,7 @@ mod imp {
                 "display-name" => {
                     obj.set_display_name(value.get::<Option<String>>().unwrap());
                 }
-                "session" => self
-                    .session
-                    .set(value.get::<Session>().unwrap().downgrade())
-                    .unwrap(),
+                "session" => self.session.set(value.get().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -214,13 +211,7 @@ impl User {
 
 pub trait UserExt: IsA<User> {
     fn session(&self) -> Session {
-        self.upcast_ref()
-            .imp()
-            .session
-            .get()
-            .unwrap()
-            .upgrade()
-            .unwrap()
+        self.upcast_ref().imp().session.upgrade().unwrap()
     }
 
     fn user_id(&self) -> OwnedUserId {
diff --git a/src/session/verification/identity_verification.rs 
b/src/session/verification/identity_verification.rs
index 023a15111..2e4020f35 100644
--- a/src/session/verification/identity_verification.rs
+++ b/src/session/verification/identity_verification.rs
@@ -171,7 +171,7 @@ mod imp {
     #[derive(Default)]
     pub struct IdentityVerification {
         pub user: OnceCell<User>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
         pub state: Cell<State>,
         pub mode: OnceCell<Mode>,
         pub supported_methods: Cell<SupportedMethods>,
@@ -524,11 +524,11 @@ impl IdentityVerification {
 
     /// The current `Session`.
     pub fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
-    fn set_session(&self, session: Session) {
-        self.imp().session.set(session.downgrade()).unwrap()
+    fn set_session(&self, session: Option<Session>) {
+        self.imp().session.set(session.as_ref())
     }
 
     fn setup_timeout(&self) {
diff --git a/src/session/verification/verification_list.rs b/src/session/verification/verification_list.rs
index 4d6018801..f50b748c9 100644
--- a/src/session/verification/verification_list.rs
+++ b/src/session/verification/verification_list.rs
@@ -50,14 +50,14 @@ mod imp {
 
     use glib::object::WeakRef;
     use indexmap::IndexMap;
-    use once_cell::{sync::Lazy, unsync::OnceCell};
+    use once_cell::sync::Lazy;
 
     use super::*;
 
     #[derive(Debug, Default)]
     pub struct VerificationList {
         pub list: RefCell<IndexMap<FlowId, IdentityVerification>>,
-        pub session: OnceCell<WeakRef<Session>>,
+        pub session: WeakRef<Session>,
     }
 
     #[glib::object_subclass]
@@ -90,10 +90,7 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "session" => self
-                    .session
-                    .set(value.get::<Session>().unwrap().downgrade())
-                    .unwrap(),
+                "session" => self.session.set(value.get().ok().as_ref()),
                 _ => unimplemented!(),
             }
         }
@@ -133,7 +130,7 @@ impl VerificationList {
     }
 
     pub fn session(&self) -> Session {
-        self.imp().session.get().unwrap().upgrade().unwrap()
+        self.imp().session.upgrade().unwrap()
     }
 
     pub fn handle_response_to_device(&self, to_device: ToDevice) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]