From cf379465bb62a00002bffdc4c2428acdadd45791 Mon Sep 17 00:00:00 2001 From: Aron Heinecke Date: Mon, 8 Aug 2022 22:15:40 +0200 Subject: [PATCH] make crossbeam-channels optional --- .github/workflows/main.yml | 27 ++++++++++++++++++------- notify/Cargo.toml | 4 ++-- notify/src/error.rs | 15 +++++++++++++- notify/src/fsevent.rs | 3 +-- notify/src/inotify.rs | 14 ++++++------- notify/src/kqueue.rs | 10 +++++----- notify/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++ notify/src/windows.rs | 6 +++--- 8 files changed, 93 insertions(+), 27 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 319beea4..88b643ce 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -37,22 +37,32 @@ jobs: rustup toolchain install ${{ matrix.version }} --no-self-update rustup override set ${{ matrix.version }} - - name: check build + - name: check build serde,macos_kqueue for examples if: matrix.version != '1.56.0' && matrix.os == 'macos-latest' - run: cargo check --features=serde,macos_kqueue --examples + run: cargo check -p notify --features=serde,macos_kqueue --examples - - name: check build + - name: check build serde,macos_kqueue if: matrix.version == '1.56.0' && matrix.os == 'macos-latest' - run: cargo check --features=serde,macos_kqueue + run: cargo check -p notify --features=serde,macos_kqueue - - name: check build + - name: check build serde for examples if: matrix.version != '1.56.0' && matrix.os != 'macos-latest' - run: cargo check --features=serde --examples + run: cargo check -p notify --features=serde --examples - - name: check build + - name: check build serde if: matrix.version == '1.56.0' && matrix.os != 'macos-latest' run: cargo check --features=serde + - name: check build without crossbeam/default features + if: matrix.version == 'stable' + run: cargo check -p notify --no-default-features --features=macos_fsevent + # -p notify required for feature selection! + + - name: check build without crossbeam/default features on macos with kqueue + if: matrix.version == 'stable' && matrix.os == 'macos-latest' + run: cargo check -p notify --no-default-features --features=macos_kqueue + # -p notify required for feature selection! + - name: check build examples if: matrix.version == 'stable' run: cargo check --package examples --examples @@ -90,6 +100,9 @@ jobs: rustc --version && cargo --version cargo build --target ${{ matrix.target }} + - name: check build without crossbeam/default features + run: cargo build -p notify --no-default-features --target ${{ matrix.target }} + audit: runs-on: ubuntu-latest diff --git a/notify/Cargo.toml b/notify/Cargo.toml index ddd9dce4..3b714fac 100644 --- a/notify/Cargo.toml +++ b/notify/Cargo.toml @@ -19,7 +19,7 @@ edition = "2021" [dependencies] bitflags = "1.0.4" -crossbeam-channel = "0.5.0" +crossbeam-channel = { version = "0.5.0", optional = true } filetime = "0.2.6" libc = "0.2.4" serde = { version = "1.0.89", features = ["derive"], optional = true } @@ -47,7 +47,7 @@ tempfile = "3.2.0" nix = "0.23.1" [features] -default = ["macos_fsevent"] +default = ["macos_fsevent","crossbeam-channel"] timing_tests = [] manual_tests = [] macos_kqueue = ["kqueue", "mio"] diff --git a/notify/src/error.rs b/notify/src/error.rs index b4d09f66..b7feb8be 100644 --- a/notify/src/error.rs +++ b/notify/src/error.rs @@ -131,17 +131,30 @@ impl From for Error { } } +#[cfg(feature = "crossbeam-channel")] impl From> for Error { fn from(err: crossbeam_channel::SendError) -> Self { Error::generic(&format!("internal channel disconnect: {:?}", err)) } } - +#[cfg(not(feature = "crossbeam-channel"))] +impl From> for Error { + fn from(err: std::sync::mpsc::SendError) -> Self { + Error::generic(&format!("internal channel disconnect: {:?}", err)) + } +} +#[cfg(feature = "crossbeam-channel")] impl From for Error { fn from(err: crossbeam_channel::RecvError) -> Self { Error::generic(&format!("internal channel disconnect: {:?}", err)) } } +#[cfg(not(feature = "crossbeam-channel"))] +impl From for Error { + fn from(err: std::sync::mpsc::RecvError) -> Self { + Error::generic(&format!("internal channel disconnect: {:?}", err)) + } +} impl From> for Error { fn from(err: std::sync::PoisonError) -> Self { diff --git a/notify/src/fsevent.rs b/notify/src/fsevent.rs index cf27d9ef..ff245b7e 100644 --- a/notify/src/fsevent.rs +++ b/notify/src/fsevent.rs @@ -15,8 +15,7 @@ #![allow(non_upper_case_globals, dead_code)] use crate::event::*; -use crate::{Config, Error, EventHandler, RecursiveMode, Result, Watcher}; -use crossbeam_channel::{unbounded, Sender}; +use crate::{Config, Error, EventHandler, RecursiveMode, Result, Watcher, unbounded, Sender}; use fsevent_sys as fs; use fsevent_sys::core_foundation as cf; use std::collections::HashMap; diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index 83481cb2..aba1ad06 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -6,7 +6,7 @@ use super::event::*; use super::{Config, Error, ErrorKind, EventHandler, RecursiveMode, Result, Watcher}; -use crossbeam_channel::{bounded, unbounded, Sender}; +use crate::{unbounded, bounded, Sender, BoundSender, Receiver}; use inotify as inotify_sys; use inotify_sys::{EventMask, Inotify, WatchDescriptor, WatchMask}; use std::collections::HashMap; @@ -32,8 +32,8 @@ struct EventLoop { running: bool, poll: mio::Poll, event_loop_waker: Arc, - event_loop_tx: crossbeam_channel::Sender, - event_loop_rx: crossbeam_channel::Receiver, + event_loop_tx: Sender, + event_loop_rx: Receiver, inotify: Option, event_handler: Box, watches: HashMap, @@ -44,7 +44,7 @@ struct EventLoop { /// Watcher implementation based on inotify #[derive(Debug)] pub struct INotifyWatcher { - channel: crossbeam_channel::Sender, + channel: Sender, waker: Arc, } @@ -53,7 +53,7 @@ enum EventLoopMsg { RemoveWatch(PathBuf, Sender>), Shutdown, RenameTimeout(usize), - Configure(Config, Sender>), + Configure(Config, BoundSender>), } #[inline] @@ -101,7 +101,7 @@ fn remove_watch_by_event( impl EventLoop { pub fn new(inotify: Inotify, event_handler: Box) -> Result { - let (event_loop_tx, event_loop_rx) = crossbeam_channel::unbounded::(); + let (event_loop_tx, event_loop_rx) = unbounded::(); let poll = mio::Poll::new()?; let event_loop_waker = Arc::new(mio::Waker::new(poll.registry(), MESSAGE)?); @@ -204,7 +204,7 @@ impl EventLoop { } } - fn configure_raw_mode(&mut self, _config: Config, tx: Sender>) { + fn configure_raw_mode(&mut self, _config: Config, tx: BoundSender>) { tx.send(Ok(false)) .expect("configuration channel disconnected"); } diff --git a/notify/src/kqueue.rs b/notify/src/kqueue.rs index 30d3d2ad..1399fa04 100644 --- a/notify/src/kqueue.rs +++ b/notify/src/kqueue.rs @@ -6,7 +6,7 @@ use super::event::*; use super::{Error, EventHandler, RecursiveMode, Result, Watcher}; -use crossbeam_channel::{unbounded, Sender}; +use crate::{unbounded, Sender, Receiver}; use kqueue::{EventData, EventFilter, FilterFlag, Ident}; use std::collections::HashMap; use std::env; @@ -29,8 +29,8 @@ struct EventLoop { running: bool, poll: mio::Poll, event_loop_waker: Arc, - event_loop_tx: crossbeam_channel::Sender, - event_loop_rx: crossbeam_channel::Receiver, + event_loop_tx: Sender, + event_loop_rx: Receiver, kqueue: kqueue::Watcher, event_handler: Box, watches: HashMap, @@ -39,7 +39,7 @@ struct EventLoop { /// Watcher implementation based on inotify #[derive(Debug)] pub struct KqueueWatcher { - channel: crossbeam_channel::Sender, + channel: Sender, waker: Arc, } @@ -51,7 +51,7 @@ enum EventLoopMsg { impl EventLoop { pub fn new(kqueue: kqueue::Watcher, event_handler: Box) -> Result { - let (event_loop_tx, event_loop_rx) = crossbeam_channel::unbounded::(); + let (event_loop_tx, event_loop_rx) = unbounded::(); let poll = mio::Poll::new()?; let event_loop_waker = Arc::new(mio::Waker::new(poll.registry(), MESSAGE)?); diff --git a/notify/src/lib.rs b/notify/src/lib.rs index 603adffa..e1a51701 100644 --- a/notify/src/lib.rs +++ b/notify/src/lib.rs @@ -105,6 +105,46 @@ pub use error::{Error, ErrorKind, Result}; pub use event::{Event, EventKind}; use std::path::Path; +#[allow(dead_code)] +#[cfg(feature = "crossbeam-channel")] +pub(crate) type Receiver = crossbeam_channel::Receiver; +#[allow(dead_code)] +#[cfg(not(feature = "crossbeam-channel"))] +pub(crate) type Receiver = std::sync::mpsc::Receiver; + +#[allow(dead_code)] +#[cfg(feature = "crossbeam-channel")] +pub(crate) type Sender = crossbeam_channel::Sender; +#[allow(dead_code)] +#[cfg(not(feature = "crossbeam-channel"))] +pub(crate) type Sender = std::sync::mpsc::Sender; + +// std limitation +#[allow(dead_code)] +#[cfg(feature = "crossbeam-channel")] +pub(crate) type BoundSender = crossbeam_channel::Sender; +#[allow(dead_code)] +#[cfg(not(feature = "crossbeam-channel"))] +pub(crate) type BoundSender = std::sync::mpsc::SyncSender; + +#[allow(dead_code)] +#[inline] +pub(crate) fn unbounded() -> (Sender, Receiver) { + #[cfg(feature = "crossbeam-channel")] + return crossbeam_channel::unbounded(); + #[cfg(not(feature = "crossbeam-channel"))] + return std::sync::mpsc::channel(); +} + +#[allow(dead_code)] +#[inline] +pub(crate) fn bounded(cap: usize) -> (BoundSender, Receiver) { + #[cfg(feature = "crossbeam-channel")] + return crossbeam_channel::bounded(cap); + #[cfg(not(feature = "crossbeam-channel"))] + return std::sync::mpsc::sync_channel(cap); +} + #[cfg(all(target_os = "macos", not(feature = "macos_kqueue")))] pub use crate::fsevent::FsEventWatcher; #[cfg(target_os = "linux")] @@ -176,6 +216,7 @@ where } } +#[cfg(feature = "crossbeam-channel")] impl EventHandler for crossbeam_channel::Sender> { fn handle_event(&mut self, event: Result) { let _ = self.send(event); diff --git a/notify/src/windows.rs b/notify/src/windows.rs index bfa3fa54..b4f23b8a 100644 --- a/notify/src/windows.rs +++ b/notify/src/windows.rs @@ -17,7 +17,7 @@ use winapi::um::winnt::{self, FILE_NOTIFY_INFORMATION, HANDLE}; use crate::{event::*, WatcherKind}; use crate::{Config, Error, EventHandler, RecursiveMode, Result, Watcher}; -use crossbeam_channel::{bounded, unbounded, Receiver, Sender}; +use crate::{unbounded, bounded, Sender, Receiver, BoundSender}; use std::collections::HashMap; use std::env; use std::ffi::OsString; @@ -51,7 +51,7 @@ enum Action { Watch(PathBuf, RecursiveMode), Unwatch(PathBuf), Stop, - Configure(Config, Sender>), + Configure(Config, BoundSender>), } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -228,7 +228,7 @@ impl ReadDirectoryChangesServer { } } - fn configure_raw_mode(&mut self, _config: Config, tx: Sender>) { + fn configure_raw_mode(&mut self, _config: Config, tx: BoundSender>) { tx.send(Ok(false)) .expect("configuration channel disconnect"); }