Skip to content

Commit

Permalink
make crossbeam-channels optional
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpr03 committed Aug 9, 2022
1 parent cbcc23e commit cf37946
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 27 deletions.
27 changes: 20 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions notify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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"]
Expand Down
15 changes: 14 additions & 1 deletion notify/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,30 @@ impl From<io::Error> for Error {
}
}

#[cfg(feature = "crossbeam-channel")]
impl<T> From<crossbeam_channel::SendError<T>> for Error {
fn from(err: crossbeam_channel::SendError<T>) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}

#[cfg(not(feature = "crossbeam-channel"))]
impl<T> From<std::sync::mpsc::SendError<T>> for Error {
fn from(err: std::sync::mpsc::SendError<T>) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}
#[cfg(feature = "crossbeam-channel")]
impl From<crossbeam_channel::RecvError> for Error {
fn from(err: crossbeam_channel::RecvError) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}
#[cfg(not(feature = "crossbeam-channel"))]
impl From<std::sync::mpsc::RecvError> for Error {
fn from(err: std::sync::mpsc::RecvError) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}

impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(err: std::sync::PoisonError<T>) -> Self {
Expand Down
3 changes: 1 addition & 2 deletions notify/src/fsevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions notify/src/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,8 +32,8 @@ struct EventLoop {
running: bool,
poll: mio::Poll,
event_loop_waker: Arc<mio::Waker>,
event_loop_tx: crossbeam_channel::Sender<EventLoopMsg>,
event_loop_rx: crossbeam_channel::Receiver<EventLoopMsg>,
event_loop_tx: Sender<EventLoopMsg>,
event_loop_rx: Receiver<EventLoopMsg>,
inotify: Option<Inotify>,
event_handler: Box<dyn EventHandler>,
watches: HashMap<PathBuf, (WatchDescriptor, WatchMask, bool)>,
Expand All @@ -44,7 +44,7 @@ struct EventLoop {
/// Watcher implementation based on inotify
#[derive(Debug)]
pub struct INotifyWatcher {
channel: crossbeam_channel::Sender<EventLoopMsg>,
channel: Sender<EventLoopMsg>,
waker: Arc<mio::Waker>,
}

Expand All @@ -53,7 +53,7 @@ enum EventLoopMsg {
RemoveWatch(PathBuf, Sender<Result<()>>),
Shutdown,
RenameTimeout(usize),
Configure(Config, Sender<Result<bool>>),
Configure(Config, BoundSender<Result<bool>>),
}

#[inline]
Expand Down Expand Up @@ -101,7 +101,7 @@ fn remove_watch_by_event(

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

let event_loop_waker = Arc::new(mio::Waker::new(poll.registry(), MESSAGE)?);
Expand Down Expand Up @@ -204,7 +204,7 @@ impl EventLoop {
}
}

fn configure_raw_mode(&mut self, _config: Config, tx: Sender<Result<bool>>) {
fn configure_raw_mode(&mut self, _config: Config, tx: BoundSender<Result<bool>>) {
tx.send(Ok(false))
.expect("configuration channel disconnected");
}
Expand Down
10 changes: 5 additions & 5 deletions notify/src/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,8 +29,8 @@ struct EventLoop {
running: bool,
poll: mio::Poll,
event_loop_waker: Arc<mio::Waker>,
event_loop_tx: crossbeam_channel::Sender<EventLoopMsg>,
event_loop_rx: crossbeam_channel::Receiver<EventLoopMsg>,
event_loop_tx: Sender<EventLoopMsg>,
event_loop_rx: Receiver<EventLoopMsg>,
kqueue: kqueue::Watcher,
event_handler: Box<dyn EventHandler>,
watches: HashMap<PathBuf, bool>,
Expand All @@ -39,7 +39,7 @@ struct EventLoop {
/// Watcher implementation based on inotify
#[derive(Debug)]
pub struct KqueueWatcher {
channel: crossbeam_channel::Sender<EventLoopMsg>,
channel: Sender<EventLoopMsg>,
waker: Arc<mio::Waker>,
}

Expand All @@ -51,7 +51,7 @@ enum EventLoopMsg {

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

let event_loop_waker = Arc::new(mio::Waker::new(poll.registry(), MESSAGE)?);
Expand Down
41 changes: 41 additions & 0 deletions notify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = crossbeam_channel::Receiver<T>;
#[allow(dead_code)]
#[cfg(not(feature = "crossbeam-channel"))]
pub(crate) type Receiver<T> = std::sync::mpsc::Receiver<T>;

#[allow(dead_code)]
#[cfg(feature = "crossbeam-channel")]
pub(crate) type Sender<T> = crossbeam_channel::Sender<T>;
#[allow(dead_code)]
#[cfg(not(feature = "crossbeam-channel"))]
pub(crate) type Sender<T> = std::sync::mpsc::Sender<T>;

// std limitation
#[allow(dead_code)]
#[cfg(feature = "crossbeam-channel")]
pub(crate) type BoundSender<T> = crossbeam_channel::Sender<T>;
#[allow(dead_code)]
#[cfg(not(feature = "crossbeam-channel"))]
pub(crate) type BoundSender<T> = std::sync::mpsc::SyncSender<T>;

#[allow(dead_code)]
#[inline]
pub(crate) fn unbounded<T>() -> (Sender<T>, Receiver<T>) {
#[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<T>(cap: usize) -> (BoundSender<T>, Receiver<T>) {
#[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")]
Expand Down Expand Up @@ -176,6 +216,7 @@ where
}
}

#[cfg(feature = "crossbeam-channel")]
impl EventHandler for crossbeam_channel::Sender<Result<Event>> {
fn handle_event(&mut self, event: Result<Event>) {
let _ = self.send(event);
Expand Down
6 changes: 3 additions & 3 deletions notify/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +51,7 @@ enum Action {
Watch(PathBuf, RecursiveMode),
Unwatch(PathBuf),
Stop,
Configure(Config, Sender<Result<bool>>),
Configure(Config, BoundSender<Result<bool>>),
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -228,7 +228,7 @@ impl ReadDirectoryChangesServer {
}
}

fn configure_raw_mode(&mut self, _config: Config, tx: Sender<Result<bool>>) {
fn configure_raw_mode(&mut self, _config: Config, tx: BoundSender<Result<bool>>) {
tx.send(Ok(false))
.expect("configuration channel disconnect");
}
Expand Down

0 comments on commit cf37946

Please sign in to comment.