Skip to content

Commit 355e5a9

Browse files
authored
config: Add calloop feature to provide a calloop source (#102)
This should be useful for integrating cosmic-config into things using calloop, like cosmic-comp.
1 parent b85c504 commit 355e5a9

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

cosmic-config/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
atomicwrites = "0.4.0"
8+
calloop = { version = "0.10.5", optional = true }
89
dirs = "4.0.0"
910
notify = "5.1.0"
1011
ron = "0.8.0"

cosmic-config/src/calloop.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// TODO If possible, calloop could poll inotify/kqueue without a thread
2+
3+
use calloop::channel;
4+
5+
use crate::{Config, Error};
6+
7+
pub struct ConfigWatchSource {
8+
channel: channel::Channel<(Config, Vec<String>)>,
9+
_watcher: notify::RecommendedWatcher,
10+
}
11+
12+
impl ConfigWatchSource {
13+
pub fn new(config: &Config) -> Result<Self, Error> {
14+
let (sender, channel) = channel::sync_channel(32);
15+
let _watcher = config.watch(move |config, keys| {
16+
let _ = sender.send((config.clone(), keys.to_owned()));
17+
})?;
18+
Ok(Self { channel, _watcher })
19+
}
20+
}
21+
22+
impl calloop::EventSource for ConfigWatchSource {
23+
type Event = (Config, Vec<String>);
24+
type Metadata = ();
25+
type Ret = ();
26+
type Error = calloop::channel::ChannelError;
27+
28+
fn process_events<F>(
29+
&mut self,
30+
readiness: calloop::Readiness,
31+
token: calloop::Token,
32+
mut cb: F,
33+
) -> Result<calloop::PostAction, Self::Error>
34+
where
35+
F: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
36+
{
37+
self.channel
38+
.process_events(readiness, token, |event, ()| match event {
39+
calloop::channel::Event::Msg(msg) => cb(msg, &mut ()),
40+
calloop::channel::Event::Closed => {}
41+
})
42+
}
43+
44+
fn register(
45+
&mut self,
46+
poll: &mut calloop::Poll,
47+
token_factory: &mut calloop::TokenFactory,
48+
) -> Result<(), calloop::Error> {
49+
self.channel.register(poll, token_factory)
50+
}
51+
52+
fn reregister(
53+
&mut self,
54+
poll: &mut calloop::Poll,
55+
token_factory: &mut calloop::TokenFactory,
56+
) -> Result<(), calloop::Error> {
57+
self.channel.reregister(poll, token_factory)
58+
}
59+
60+
fn unregister(&mut self, poll: &mut calloop::Poll) -> Result<(), calloop::Error> {
61+
self.channel.unregister(poll)
62+
}
63+
}

cosmic-config/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use std::{
77
sync::Mutex,
88
};
99

10+
#[cfg(feature = "calloop")]
11+
pub mod calloop;
12+
1013
#[derive(Debug)]
1114
pub enum Error {
1215
AtomicWrites(atomicwrites::Error<std::io::Error>),

0 commit comments

Comments
 (0)