Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow more flexible EventStream buffers #124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions examples/issue-120-workaround.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ use tempdir::TempDir;
use tokio::prelude::*;

fn main() -> Result<(), io::Error> {
/// XXX: this is a workaround to set the inotify's buffer to be
/// used, matching with tokio's runtime API
/// https://github.com/inotify-rs/inotify/issues/120
static mut BUFFER: [u8; 32] = [0; 32];

let mut inotify = Inotify::init()?;

Expand All @@ -26,7 +22,7 @@ fn main() -> Result<(), io::Error> {
});

let future = inotify
.event_stream(unsafe { &mut BUFFER })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this example should be removed and this technique here should be part of the API documentation (as a small example). Not necessary to do it in this PR, but we shouldn't forget to open an issue, at least.

.event_stream([0; 32])
.map_err(|e| println!("inotify error: {:?}", e))
.for_each(move |event| {
println!("event: {:?}", event);
Expand Down
15 changes: 8 additions & 7 deletions src/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,10 @@ impl Inotify {
///
/// [`Inotify::event_stream_with_handle`]: struct.Inotify.html#method.event_stream_with_handle
#[cfg(feature = "stream")]
pub fn event_stream<'buffer>(&mut self, buffer: &'buffer mut [u8])
-> EventStream<'buffer>
pub fn event_stream<T>(&mut self, buffer: T)
-> EventStream<T>
where
T: AsMut<[u8]> + AsRef<[u8]>,
{
EventStream::new(self.fd.clone(), buffer)
}
Expand All @@ -417,11 +419,10 @@ impl Inotify {
///
/// [`Inotify::event_stream`]: struct.Inotify.html#method.event_stream
#[cfg(feature = "stream")]
pub fn event_stream_with_handle<'buffer>(&mut self,
handle: &Handle,
buffer: &'buffer mut [u8],
)
-> io::Result<EventStream<'buffer>>
pub fn event_stream_with_handle<T>(&mut self, handle: &Handle, buffer: T)
-> io::Result<EventStream<T>>
where
T: AsMut<[u8]> + AsRef<[u8]>,
{
EventStream::new_with_handle(self.fd.clone(), handle, buffer)
}
Expand Down
22 changes: 14 additions & 8 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ use util::read_into_buffer;
/// Allows for streaming events returned by [`Inotify::event_stream`].
///
/// [`Inotify::event_stream`]: struct.Inotify.html#method.event_stream
pub struct EventStream<'buffer> {
pub struct EventStream<T> {
fd: PollEvented<EventedFdGuard>,
buffer: &'buffer mut [u8],
buffer: T,
buffer_pos: usize,
unused_bytes: usize,
}

impl<'buffer> EventStream<'buffer> {
impl<T> EventStream<T>
where
T: AsMut<[u8]> + AsRef<[u8]>,
{
/// Returns a new `EventStream` associated with the default reactor.
pub(crate) fn new(fd: Arc<FdGuard>, buffer: &'buffer mut [u8]) -> Self {
pub(crate) fn new(fd: Arc<FdGuard>, buffer: T) -> Self {
EventStream {
fd: PollEvented::new(EventedFdGuard(fd)),
buffer: buffer,
Expand All @@ -60,7 +63,7 @@ impl<'buffer> EventStream<'buffer> {
pub(crate) fn new_with_handle(
fd : Arc<FdGuard>,
handle: &Handle,
buffer: &'buffer mut [u8],
buffer: T,
)
-> io::Result<Self>
{
Expand All @@ -73,7 +76,10 @@ impl<'buffer> EventStream<'buffer> {
}
}

impl<'buffer> Stream for EventStream<'buffer> {
impl<T> Stream for EventStream<T>
where
T: AsMut<[u8]> + AsRef<[u8]>,
{
type Item = EventOwned;
type Error = io::Error;

Expand All @@ -82,7 +88,7 @@ impl<'buffer> Stream for EventStream<'buffer> {
if self.unused_bytes == 0 {
// Nothing usable in buffer. Need to reset and fill buffer.
self.buffer_pos = 0;
self.unused_bytes = try_ready!(self.fd.poll_read(&mut self.buffer));
self.unused_bytes = try_ready!(self.fd.poll_read(&mut self.buffer.as_mut()));
}

if self.unused_bytes == 0 {
Expand All @@ -96,7 +102,7 @@ impl<'buffer> Stream for EventStream<'buffer> {
// least one event in there and can call `from_buffer` to take it out.
let (bytes_consumed, event) = Event::from_buffer(
Arc::downgrade(self.fd.get_ref()),
&self.buffer[self.buffer_pos..],
&self.buffer.as_ref()[self.buffer_pos..],
);
self.buffer_pos += bytes_consumed;
self.unused_bytes -= bytes_consumed;
Expand Down
2 changes: 1 addition & 1 deletion tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn it_should_watch_a_file_async() {

use futures::Stream;
let events = inotify
.event_stream(&mut buffer)
.event_stream(&mut buffer[..])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this change was required to get it to compile? If so, this is a breaking change. Not a problem, just want to confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's correct --- it's only necessary when the array is longer than [T; 32], as only arrays of length 0-32 have AsRef/AsMut impls.

.take(1)
.wait()
.collect::<Vec<_>>();
Expand Down