Skip to content

Commit cf37946

Browse files
committed
make crossbeam-channels optional
1 parent cbcc23e commit cf37946

8 files changed

Lines changed: 93 additions & 27 deletions

File tree

.github/workflows/main.yml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,32 @@ jobs:
3737
rustup toolchain install ${{ matrix.version }} --no-self-update
3838
rustup override set ${{ matrix.version }}
3939
40-
- name: check build
40+
- name: check build serde,macos_kqueue for examples
4141
if: matrix.version != '1.56.0' && matrix.os == 'macos-latest'
42-
run: cargo check --features=serde,macos_kqueue --examples
42+
run: cargo check -p notify --features=serde,macos_kqueue --examples
4343

44-
- name: check build
44+
- name: check build serde,macos_kqueue
4545
if: matrix.version == '1.56.0' && matrix.os == 'macos-latest'
46-
run: cargo check --features=serde,macos_kqueue
46+
run: cargo check -p notify --features=serde,macos_kqueue
4747

48-
- name: check build
48+
- name: check build serde for examples
4949
if: matrix.version != '1.56.0' && matrix.os != 'macos-latest'
50-
run: cargo check --features=serde --examples
50+
run: cargo check -p notify --features=serde --examples
5151

52-
- name: check build
52+
- name: check build serde
5353
if: matrix.version == '1.56.0' && matrix.os != 'macos-latest'
5454
run: cargo check --features=serde
5555

56+
- name: check build without crossbeam/default features
57+
if: matrix.version == 'stable'
58+
run: cargo check -p notify --no-default-features --features=macos_fsevent
59+
# -p notify required for feature selection!
60+
61+
- name: check build without crossbeam/default features on macos with kqueue
62+
if: matrix.version == 'stable' && matrix.os == 'macos-latest'
63+
run: cargo check -p notify --no-default-features --features=macos_kqueue
64+
# -p notify required for feature selection!
65+
5666
- name: check build examples
5767
if: matrix.version == 'stable'
5868
run: cargo check --package examples --examples
@@ -90,6 +100,9 @@ jobs:
90100
rustc --version && cargo --version
91101
cargo build --target ${{ matrix.target }}
92102
103+
- name: check build without crossbeam/default features
104+
run: cargo build -p notify --no-default-features --target ${{ matrix.target }}
105+
93106
audit:
94107
runs-on: ubuntu-latest
95108

notify/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ edition = "2021"
1919

2020
[dependencies]
2121
bitflags = "1.0.4"
22-
crossbeam-channel = "0.5.0"
22+
crossbeam-channel = { version = "0.5.0", optional = true }
2323
filetime = "0.2.6"
2424
libc = "0.2.4"
2525
serde = { version = "1.0.89", features = ["derive"], optional = true }
@@ -47,7 +47,7 @@ tempfile = "3.2.0"
4747
nix = "0.23.1"
4848

4949
[features]
50-
default = ["macos_fsevent"]
50+
default = ["macos_fsevent","crossbeam-channel"]
5151
timing_tests = []
5252
manual_tests = []
5353
macos_kqueue = ["kqueue", "mio"]

notify/src/error.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,30 @@ impl From<io::Error> for Error {
131131
}
132132
}
133133

134+
#[cfg(feature = "crossbeam-channel")]
134135
impl<T> From<crossbeam_channel::SendError<T>> for Error {
135136
fn from(err: crossbeam_channel::SendError<T>) -> Self {
136137
Error::generic(&format!("internal channel disconnect: {:?}", err))
137138
}
138139
}
139-
140+
#[cfg(not(feature = "crossbeam-channel"))]
141+
impl<T> From<std::sync::mpsc::SendError<T>> for Error {
142+
fn from(err: std::sync::mpsc::SendError<T>) -> Self {
143+
Error::generic(&format!("internal channel disconnect: {:?}", err))
144+
}
145+
}
146+
#[cfg(feature = "crossbeam-channel")]
140147
impl From<crossbeam_channel::RecvError> for Error {
141148
fn from(err: crossbeam_channel::RecvError) -> Self {
142149
Error::generic(&format!("internal channel disconnect: {:?}", err))
143150
}
144151
}
152+
#[cfg(not(feature = "crossbeam-channel"))]
153+
impl From<std::sync::mpsc::RecvError> for Error {
154+
fn from(err: std::sync::mpsc::RecvError) -> Self {
155+
Error::generic(&format!("internal channel disconnect: {:?}", err))
156+
}
157+
}
145158

146159
impl<T> From<std::sync::PoisonError<T>> for Error {
147160
fn from(err: std::sync::PoisonError<T>) -> Self {

notify/src/fsevent.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#![allow(non_upper_case_globals, dead_code)]
1616

1717
use crate::event::*;
18-
use crate::{Config, Error, EventHandler, RecursiveMode, Result, Watcher};
19-
use crossbeam_channel::{unbounded, Sender};
18+
use crate::{Config, Error, EventHandler, RecursiveMode, Result, Watcher, unbounded, Sender};
2019
use fsevent_sys as fs;
2120
use fsevent_sys::core_foundation as cf;
2221
use std::collections::HashMap;

notify/src/inotify.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use super::event::*;
88
use super::{Config, Error, ErrorKind, EventHandler, RecursiveMode, Result, Watcher};
9-
use crossbeam_channel::{bounded, unbounded, Sender};
9+
use crate::{unbounded, bounded, Sender, BoundSender, Receiver};
1010
use inotify as inotify_sys;
1111
use inotify_sys::{EventMask, Inotify, WatchDescriptor, WatchMask};
1212
use std::collections::HashMap;
@@ -32,8 +32,8 @@ struct EventLoop {
3232
running: bool,
3333
poll: mio::Poll,
3434
event_loop_waker: Arc<mio::Waker>,
35-
event_loop_tx: crossbeam_channel::Sender<EventLoopMsg>,
36-
event_loop_rx: crossbeam_channel::Receiver<EventLoopMsg>,
35+
event_loop_tx: Sender<EventLoopMsg>,
36+
event_loop_rx: Receiver<EventLoopMsg>,
3737
inotify: Option<Inotify>,
3838
event_handler: Box<dyn EventHandler>,
3939
watches: HashMap<PathBuf, (WatchDescriptor, WatchMask, bool)>,
@@ -44,7 +44,7 @@ struct EventLoop {
4444
/// Watcher implementation based on inotify
4545
#[derive(Debug)]
4646
pub struct INotifyWatcher {
47-
channel: crossbeam_channel::Sender<EventLoopMsg>,
47+
channel: Sender<EventLoopMsg>,
4848
waker: Arc<mio::Waker>,
4949
}
5050

@@ -53,7 +53,7 @@ enum EventLoopMsg {
5353
RemoveWatch(PathBuf, Sender<Result<()>>),
5454
Shutdown,
5555
RenameTimeout(usize),
56-
Configure(Config, Sender<Result<bool>>),
56+
Configure(Config, BoundSender<Result<bool>>),
5757
}
5858

5959
#[inline]
@@ -101,7 +101,7 @@ fn remove_watch_by_event(
101101

102102
impl EventLoop {
103103
pub fn new(inotify: Inotify, event_handler: Box<dyn EventHandler>) -> Result<Self> {
104-
let (event_loop_tx, event_loop_rx) = crossbeam_channel::unbounded::<EventLoopMsg>();
104+
let (event_loop_tx, event_loop_rx) = unbounded::<EventLoopMsg>();
105105
let poll = mio::Poll::new()?;
106106

107107
let event_loop_waker = Arc::new(mio::Waker::new(poll.registry(), MESSAGE)?);
@@ -204,7 +204,7 @@ impl EventLoop {
204204
}
205205
}
206206

207-
fn configure_raw_mode(&mut self, _config: Config, tx: Sender<Result<bool>>) {
207+
fn configure_raw_mode(&mut self, _config: Config, tx: BoundSender<Result<bool>>) {
208208
tx.send(Ok(false))
209209
.expect("configuration channel disconnected");
210210
}

notify/src/kqueue.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use super::event::*;
88
use super::{Error, EventHandler, RecursiveMode, Result, Watcher};
9-
use crossbeam_channel::{unbounded, Sender};
9+
use crate::{unbounded, Sender, Receiver};
1010
use kqueue::{EventData, EventFilter, FilterFlag, Ident};
1111
use std::collections::HashMap;
1212
use std::env;
@@ -29,8 +29,8 @@ struct EventLoop {
2929
running: bool,
3030
poll: mio::Poll,
3131
event_loop_waker: Arc<mio::Waker>,
32-
event_loop_tx: crossbeam_channel::Sender<EventLoopMsg>,
33-
event_loop_rx: crossbeam_channel::Receiver<EventLoopMsg>,
32+
event_loop_tx: Sender<EventLoopMsg>,
33+
event_loop_rx: Receiver<EventLoopMsg>,
3434
kqueue: kqueue::Watcher,
3535
event_handler: Box<dyn EventHandler>,
3636
watches: HashMap<PathBuf, bool>,
@@ -39,7 +39,7 @@ struct EventLoop {
3939
/// Watcher implementation based on inotify
4040
#[derive(Debug)]
4141
pub struct KqueueWatcher {
42-
channel: crossbeam_channel::Sender<EventLoopMsg>,
42+
channel: Sender<EventLoopMsg>,
4343
waker: Arc<mio::Waker>,
4444
}
4545

@@ -51,7 +51,7 @@ enum EventLoopMsg {
5151

5252
impl EventLoop {
5353
pub fn new(kqueue: kqueue::Watcher, event_handler: Box<dyn EventHandler>) -> Result<Self> {
54-
let (event_loop_tx, event_loop_rx) = crossbeam_channel::unbounded::<EventLoopMsg>();
54+
let (event_loop_tx, event_loop_rx) = unbounded::<EventLoopMsg>();
5555
let poll = mio::Poll::new()?;
5656

5757
let event_loop_waker = Arc::new(mio::Waker::new(poll.registry(), MESSAGE)?);

notify/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,46 @@ pub use error::{Error, ErrorKind, Result};
105105
pub use event::{Event, EventKind};
106106
use std::path::Path;
107107

108+
#[allow(dead_code)]
109+
#[cfg(feature = "crossbeam-channel")]
110+
pub(crate) type Receiver<T> = crossbeam_channel::Receiver<T>;
111+
#[allow(dead_code)]
112+
#[cfg(not(feature = "crossbeam-channel"))]
113+
pub(crate) type Receiver<T> = std::sync::mpsc::Receiver<T>;
114+
115+
#[allow(dead_code)]
116+
#[cfg(feature = "crossbeam-channel")]
117+
pub(crate) type Sender<T> = crossbeam_channel::Sender<T>;
118+
#[allow(dead_code)]
119+
#[cfg(not(feature = "crossbeam-channel"))]
120+
pub(crate) type Sender<T> = std::sync::mpsc::Sender<T>;
121+
122+
// std limitation
123+
#[allow(dead_code)]
124+
#[cfg(feature = "crossbeam-channel")]
125+
pub(crate) type BoundSender<T> = crossbeam_channel::Sender<T>;
126+
#[allow(dead_code)]
127+
#[cfg(not(feature = "crossbeam-channel"))]
128+
pub(crate) type BoundSender<T> = std::sync::mpsc::SyncSender<T>;
129+
130+
#[allow(dead_code)]
131+
#[inline]
132+
pub(crate) fn unbounded<T>() -> (Sender<T>, Receiver<T>) {
133+
#[cfg(feature = "crossbeam-channel")]
134+
return crossbeam_channel::unbounded();
135+
#[cfg(not(feature = "crossbeam-channel"))]
136+
return std::sync::mpsc::channel();
137+
}
138+
139+
#[allow(dead_code)]
140+
#[inline]
141+
pub(crate) fn bounded<T>(cap: usize) -> (BoundSender<T>, Receiver<T>) {
142+
#[cfg(feature = "crossbeam-channel")]
143+
return crossbeam_channel::bounded(cap);
144+
#[cfg(not(feature = "crossbeam-channel"))]
145+
return std::sync::mpsc::sync_channel(cap);
146+
}
147+
108148
#[cfg(all(target_os = "macos", not(feature = "macos_kqueue")))]
109149
pub use crate::fsevent::FsEventWatcher;
110150
#[cfg(target_os = "linux")]
@@ -176,6 +216,7 @@ where
176216
}
177217
}
178218

219+
#[cfg(feature = "crossbeam-channel")]
179220
impl EventHandler for crossbeam_channel::Sender<Result<Event>> {
180221
fn handle_event(&mut self, event: Result<Event>) {
181222
let _ = self.send(event);

notify/src/windows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use winapi::um::winnt::{self, FILE_NOTIFY_INFORMATION, HANDLE};
1717

1818
use crate::{event::*, WatcherKind};
1919
use crate::{Config, Error, EventHandler, RecursiveMode, Result, Watcher};
20-
use crossbeam_channel::{bounded, unbounded, Receiver, Sender};
20+
use crate::{unbounded, bounded, Sender, Receiver, BoundSender};
2121
use std::collections::HashMap;
2222
use std::env;
2323
use std::ffi::OsString;
@@ -51,7 +51,7 @@ enum Action {
5151
Watch(PathBuf, RecursiveMode),
5252
Unwatch(PathBuf),
5353
Stop,
54-
Configure(Config, Sender<Result<bool>>),
54+
Configure(Config, BoundSender<Result<bool>>),
5555
}
5656

5757
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@@ -228,7 +228,7 @@ impl ReadDirectoryChangesServer {
228228
}
229229
}
230230

231-
fn configure_raw_mode(&mut self, _config: Config, tx: Sender<Result<bool>>) {
231+
fn configure_raw_mode(&mut self, _config: Config, tx: BoundSender<Result<bool>>) {
232232
tx.send(Ok(false))
233233
.expect("configuration channel disconnect");
234234
}

0 commit comments

Comments
 (0)