Commit c18143d14a
Unsigned
Layout: unified · split
src/config.rs +2 −27
| @@ -14,7 +14,7 @@ | |||
| 14 | //! The config options created here will be alive as long as the plugin is | 14 | //! The config options created here will be alive as long as the plugin is |
| 15 | //! loaded so they don't need to be freed manually. The drop implementation of | 15 | //! loaded so they don't need to be freed manually. The drop implementation of |
| 16 | //! the section will do so. | 16 | //! the section will do so. |
| 17 | use crate::{MatrixServer, Servers, ServersHandle}; | 17 | use crate::{MatrixServer, Servers}; |
| 18 | use weechat::config::{ | 18 | use weechat::config::{ |
| 19 | Conf, ConfigSection, ConfigSectionSettings, OptionChanged, | 19 | Conf, ConfigSection, ConfigSectionSettings, OptionChanged, |
| 20 | SectionReadCallback, | 20 | SectionReadCallback, |
| @@ -22,7 +22,7 @@ use weechat::config::{ | |||
| 22 | use weechat::Weechat; | 22 | use weechat::Weechat; |
| 23 | 23 | ||
| 24 | use std::cell::{Ref, RefCell, RefMut}; | 24 | use std::cell::{Ref, RefCell, RefMut}; |
| 25 | use std::rc::{Rc, Weak}; | 25 | use std::rc::Rc; |
| 26 | 26 | ||
| 27 | #[derive(Clone)] | 27 | #[derive(Clone)] |
| 28 | pub struct Config { | 28 | pub struct Config { |
| @@ -30,12 +30,6 @@ pub struct Config { | |||
| 30 | servers: Servers, | 30 | servers: Servers, |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | #[derive(Clone, Default)] | ||
| 34 | pub struct ConfigHandle { | ||
| 35 | inner: Weak<RefCell<weechat::config::Config>>, | ||
| 36 | servers: ServersHandle, | ||
| 37 | } | ||
| 38 | |||
| 39 | impl SectionReadCallback for Config { | 33 | impl SectionReadCallback for Config { |
| 40 | fn callback( | 34 | fn callback( |
| 41 | &mut self, | 35 | &mut self, |
| @@ -79,18 +73,6 @@ impl SectionReadCallback for Config { | |||
| 79 | } | 73 | } |
| 80 | } | 74 | } |
| 81 | 75 | ||
| 82 | impl ConfigHandle { | ||
| 83 | pub fn upgrade(&self) -> Config { | ||
| 84 | Config { | ||
| 85 | inner: self | ||
| 86 | .inner | ||
| 87 | .upgrade() | ||
| 88 | .expect("Config got deleted before plugin unload"), | ||
| 89 | servers: self.servers.upgrade(), | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | impl Config { | 76 | impl Config { |
| 95 | pub fn new(_weechat: &Weechat, servers: &Servers) -> Config { | 77 | pub fn new(_weechat: &Weechat, servers: &Servers) -> Config { |
| 96 | let config = Weechat::config_new("matrix-rust") | 78 | let config = Weechat::config_new("matrix-rust") |
| @@ -131,11 +113,4 @@ impl Config { | |||
| 131 | pub fn borrow_mut(&self) -> RefMut<'_, weechat::config::Config> { | 113 | pub fn borrow_mut(&self) -> RefMut<'_, weechat::config::Config> { |
| 132 | self.inner.borrow_mut() | 114 | self.inner.borrow_mut() |
| 133 | } | 115 | } |
| 134 | |||
| 135 | pub fn clone_weak(&self) -> ConfigHandle { | ||
| 136 | ConfigHandle { | ||
| 137 | inner: Rc::downgrade(&self.inner), | ||
| 138 | servers: self.servers.clone_weak(), | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | 116 | } |
src/lib.rs +1 −16
| @@ -8,11 +8,9 @@ mod server; | |||
| 8 | 8 | ||
| 9 | use std::cell::{Ref, RefCell, RefMut}; | 9 | use std::cell::{Ref, RefCell, RefMut}; |
| 10 | use std::collections::HashMap; | 10 | use std::collections::HashMap; |
| 11 | use std::rc::{Rc, Weak}; | 11 | use std::rc::Rc; |
| 12 | use tracing_subscriber; | 12 | use tracing_subscriber; |
| 13 | 13 | ||
| 14 | use tokio::sync::mpsc::unbounded_channel; | ||
| 15 | |||
| 16 | use weechat::{weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin}; | 14 | use weechat::{weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin}; |
| 17 | 15 | ||
| 18 | use crate::commands::Commands; | 16 | use crate::commands::Commands; |
| @@ -24,15 +22,6 @@ const PLUGIN_NAME: &str = "matrix"; | |||
| 24 | #[derive(Clone)] | 22 | #[derive(Clone)] |
| 25 | pub struct Servers(Rc<RefCell<HashMap<String, MatrixServer>>>); | 23 | pub struct Servers(Rc<RefCell<HashMap<String, MatrixServer>>>); |
| 26 | 24 | ||
| 27 | #[derive(Clone, Default)] | ||
| 28 | pub struct ServersHandle(Weak<RefCell<HashMap<String, MatrixServer>>>); | ||
| 29 | |||
| 30 | impl ServersHandle { | ||
| 31 | fn upgrade(&self) -> Servers { | ||
| 32 | Servers(self.0.upgrade().expect("Severs are dropped?")) | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | impl Servers { | 25 | impl Servers { |
| 37 | fn new() -> Self { | 26 | fn new() -> Self { |
| 38 | Servers(Rc::new(RefCell::new(HashMap::new()))) | 27 | Servers(Rc::new(RefCell::new(HashMap::new()))) |
| @@ -45,10 +34,6 @@ impl Servers { | |||
| 45 | fn borrow_mut(&self) -> RefMut<'_, HashMap<String, MatrixServer>> { | 34 | fn borrow_mut(&self) -> RefMut<'_, HashMap<String, MatrixServer>> { |
| 46 | self.0.borrow_mut() | 35 | self.0.borrow_mut() |
| 47 | } | 36 | } |
| 48 | |||
| 49 | fn clone_weak(&self) -> ServersHandle { | ||
| 50 | ServersHandle(Rc::downgrade(&self.0)) | ||
| 51 | } | ||
| 52 | } | 37 | } |
| 53 | 38 | ||
| 54 | struct Matrix { | 39 | struct Matrix { |