Browse Source

utils: Simplify TokioDrop API

It is now just a wrapper.
fractal-13
Kévin Commaille 7 months ago
parent
commit
4597519128
No known key found for this signature in database
GPG Key ID: F26F4BE20A08255B
  1. 4
      src/session/model/session.rs
  2. 3
      src/session/view/content/room_history/message_toolbar/mod.rs
  3. 68
      src/utils/mod.rs

4
src/session/model/session.rs

@ -67,7 +67,7 @@ mod imp {
#[properties(wrapper_type = super::Session)]
pub struct Session {
/// The Matrix client for this session.
client: TokioDrop<Client>,
client: OnceCell<TokioDrop<Client>>,
/// The list model of the sidebar.
#[property(get = Self::sidebar_list_model)]
sidebar_list_model: OnceCell<SidebarListModel>,
@ -153,7 +153,7 @@ mod imp {
/// Set the Matrix client for this session.
pub(super) fn set_client(&self, client: Client) {
self.client
.set(client)
.set(TokioDrop::new(client))
.expect("client should be uninitialized");
let obj = self.obj();

3
src/session/view/content/room_history/message_toolbar/mod.rs

@ -813,8 +813,7 @@ mod imp {
}
future::Either::Right((response, _)) => {
// The linux location stream requires a tokio executor when dropped.
let stream_drop = TokioDrop::new();
let _ = stream_drop.set(location_stream);
let _ = TokioDrop::new(location_stream);
if response == gtk::ResponseType::Ok {
break;

68
src/utils/mod.rs

@ -3,8 +3,9 @@
use std::{
borrow::Cow,
cell::{Cell, OnceCell, RefCell},
fmt, fs, io,
io::Write,
fmt, fs,
io::{self, Write},
ops::Deref,
path::{Path, PathBuf},
rc::{Rc, Weak},
sync::{Arc, LazyLock},
@ -384,36 +385,30 @@ impl<T> AsyncAction<T> {
}
}
/// A type that requires the tokio runtime to be running when dropped.
///
/// This is basically usable as a [`OnceCell`].
/// A wrapper that requires the tokio runtime to be running when dropped.
#[derive(Debug, Clone)]
pub struct TokioDrop<T>(OnceCell<T>);
pub struct TokioDrop<T>(Option<T>);
impl<T> TokioDrop<T> {
/// Create a new empty `TokioDrop`;
pub fn new() -> Self {
Self::default()
/// Create a new `TokioDrop` wrapping the given type.
pub fn new(value: T) -> Self {
Self(Some(value))
}
}
/// Gets a reference to the underlying value.
///
/// Returns `None` if the cell is empty.
pub fn get(&self) -> Option<&T> {
self.0.get()
}
impl<T> Deref for TokioDrop<T> {
type Target = T;
/// Sets the contents of this cell to `value`.
///
/// Returns `Ok(())` if the cell was empty and `Err(value)` if it was full.
pub(crate) fn set(&self, value: T) -> Result<(), T> {
self.0.set(value)
fn deref(&self) -> &Self::Target {
self.0
.as_ref()
.expect("TokioDrop should always contain a value")
}
}
impl<T> Default for TokioDrop<T> {
fn default() -> Self {
Self(Default::default())
impl<T> From<T> for TokioDrop<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}
@ -421,35 +416,12 @@ impl<T> Drop for TokioDrop<T> {
fn drop(&mut self) {
let _guard = RUNTIME.enter();
if let Some(inner) = self.0.take() {
drop(inner);
if let Some(value) = self.0.take() {
drop(value);
}
}
}
impl<T: glib::property::Property> glib::property::Property for TokioDrop<T> {
type Value = T::Value;
}
impl<T> glib::property::PropertyGet for TokioDrop<T> {
type Value = T;
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
f(self.get().unwrap())
}
}
impl<T> glib::property::PropertySet for TokioDrop<T> {
type SetValue = T;
fn set(&self, v: Self::SetValue) {
assert!(
self.set(v).is_ok(),
"TokioDrop value was already initialized"
);
}
}
/// The state of a resource that can be loaded.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, glib::Enum)]
#[enum_type(name = "LoadingState")]

Loading…
Cancel
Save