Skip to content

Commit

Permalink
Merge pull request #109 from inotify-rs/buffer
Browse files Browse the repository at this point in the history
Use user-provided buffer for stream
  • Loading branch information
hannobraun authored Jul 31, 2018
2 parents 64b89c8 + 3e872ee commit b1b07af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
16 changes: 10 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ use std::ffi::{
};

use libc::{
FILENAME_MAX,
F_GETFL,
F_SETFL,
O_NONBLOCK,
Expand Down Expand Up @@ -499,8 +498,10 @@ impl Inotify {
///
/// [`Inotify::event_stream_with_handle`]: struct.Inotify.html#method.event_stream_with_handle
#[cfg(feature = "stream")]
pub fn event_stream(&mut self) -> EventStream {
EventStream::new(self.fd.clone())
pub fn event_stream<'buffer>(&mut self, buffer: &'buffer mut [u8])
-> EventStream<'buffer>
{
EventStream::new(self.fd.clone(), buffer)
}

/// Create a stream which collects events, associated with the given
Expand All @@ -512,10 +513,13 @@ impl Inotify {
///
/// [`Inotify::event_stream`]: struct.Inotify.html#method.event_stream
#[cfg(feature = "stream")]
pub fn event_stream_with_handle(&mut self, handle: &Handle)
-> io::Result<EventStream>
pub fn event_stream_with_handle<'buffer>(&mut self,
handle: &Handle,
buffer: &'buffer mut [u8],
)
-> io::Result<EventStream<'buffer>>
{
EventStream::new_with_handle(self.fd.clone(), handle)
EventStream::new_with_handle(self.fd.clone(), handle, buffer)
}

/// Closes the inotify instance
Expand Down
27 changes: 14 additions & 13 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,42 @@ use std::{
/// Allows for streaming events returned by [`Inotify::event_stream`].
///
/// [`Inotify::event_stream`]: struct.Inotify.html#method.event_stream
pub struct EventStream {
pub struct EventStream<'buffer> {
fd: PollEvented<EventedFdGuard>,
buffer: [u8; EVENT_MAX_SIZE],
buffer: &'buffer mut [u8],
pos: usize,
size: usize,
}

// Use this when 1.24 is available for use. We can use the hard-coded 16 due to Linux's ABI
// guarantees.
// const EVENT_MAX_SIZE: usize = mem::size_of::<ffi::inotify_event>() + (FILENAME_MAX as usize) + 1;
const EVENT_MAX_SIZE: usize = 16 + (FILENAME_MAX as usize) + 1;

impl EventStream {
impl<'buffer> EventStream<'buffer> {
/// Returns a new `EventStream` associated with the default reactor.
pub(crate) fn new(fd: Arc<FdGuard>) -> Self {
pub(crate) fn new(fd: Arc<FdGuard>, buffer: &'buffer mut [u8]) -> Self {
EventStream {
fd: PollEvented::new(EventedFdGuard(fd)),
buffer: [0; EVENT_MAX_SIZE],
buffer: buffer,
pos: 0,
size: 0,
}
}

/// Returns a new `EventStream` associated with the specified reactor.
pub(crate) fn new_with_handle(fd: Arc<FdGuard>, handle: &Handle) -> io::Result<Self> {
pub(crate) fn new_with_handle(
fd : Arc<FdGuard>,
handle: &Handle,
buffer: &'buffer mut [u8],
)
-> io::Result<Self>
{
Ok(EventStream {
fd: PollEvented::new_with_handle(EventedFdGuard(fd), handle)?,
buffer: [0; EVENT_MAX_SIZE],
buffer: buffer,
pos: 0,
size: 0,
})
}
}

impl Stream for EventStream {
impl<'buffer> Stream for EventStream<'buffer> {
type Item = EventOwned;
type Error = io::Error;

Expand Down
8 changes: 7 additions & 1 deletion tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ fn it_should_watch_a_file_async() {

write_to(&mut file);

let mut buffer = [0; 1024];

use futures::Stream;
let events = inotify.event_stream().take(1).wait().collect::<Vec<_>>();
let events = inotify
.event_stream(&mut buffer)
.take(1)
.wait()
.collect::<Vec<_>>();

let mut num_events = 0;
for event in events {
Expand Down

0 comments on commit b1b07af

Please sign in to comment.