Skip to content

Commit 516d51a

Browse files
authored
Merge pull request #20 from scontain/updated-deps
refactor!: upgrade deps and fix compile errors
2 parents eb6947f + e95e7c0 commit 516d51a

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ version = "0.3.4-alpha.0"
2020
repository = "jcreekmore/timeout-readwrite-rs"
2121

2222
[dependencies]
23-
nix = { version = "0.26.0", default-features = false, features = ["poll"] }
23+
nix = { version = "0.29.0", default-features = false, features = ["poll"] }
2424

2525
[dev-dependencies]
26-
lazy_static = "1.3.0"
26+
lazy_static = "1.5.0"

src/reader.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::io::Read;
1212
use std::io::Result;
1313
use std::io::Seek;
1414
use std::io::SeekFrom;
15-
use std::os::unix::io::AsRawFd;
15+
use std::os::fd::AsFd;
1616
use std::time::Duration;
1717

1818
use super::utils;
@@ -29,15 +29,15 @@ use super::utils;
2929
/// of the `Read` trait could also be produced by the `TimeoutReader`.
3030
pub struct TimeoutReader<H>
3131
where
32-
H: Read + AsRawFd,
32+
H: Read + AsFd,
3333
{
3434
timeout: Option<c_int>,
3535
handle: H,
3636
}
3737

3838
impl<H> Read for TimeoutReader<H>
3939
where
40-
H: Read + AsRawFd,
40+
H: Read + AsFd,
4141
{
4242
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
4343
utils::wait_until_ready(self.timeout, &self.handle, PollFlags::POLLIN)?;
@@ -47,25 +47,25 @@ where
4747

4848
impl<H> Seek for TimeoutReader<H>
4949
where
50-
H: Read + AsRawFd + Seek,
50+
H: Read + AsFd + Seek,
5151
{
5252
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
5353
self.handle.seek(pos)
5454
}
5555
}
5656

57-
impl<H> AsRawFd for TimeoutReader<H>
57+
impl<H> AsFd for TimeoutReader<H>
5858
where
59-
H: Read + AsRawFd,
59+
H: Read + AsFd,
6060
{
61-
fn as_raw_fd(&self) -> c_int {
62-
self.handle.as_raw_fd()
61+
fn as_fd(&self) -> std::os::unix::prelude::BorrowedFd<'_> {
62+
self.handle.as_fd()
6363
}
6464
}
6565

6666
impl<H> Clone for TimeoutReader<H>
6767
where
68-
H: Read + AsRawFd + Clone,
68+
H: Read + AsFd + Clone,
6969
{
7070
fn clone(&self) -> TimeoutReader<H> {
7171
TimeoutReader {
@@ -77,7 +77,7 @@ where
7777

7878
impl<H> TimeoutReader<H>
7979
where
80-
H: Read + AsRawFd,
80+
H: Read + AsFd,
8181
{
8282
/// Create a new `TimeoutReader` with an optional timeout.
8383
///
@@ -113,21 +113,21 @@ where
113113
pub fn new<T: Into<Option<Duration>>>(handle: H, timeout: T) -> TimeoutReader<H> {
114114
TimeoutReader {
115115
timeout: timeout.into().map(utils::duration_to_ms),
116-
handle: handle,
116+
handle,
117117
}
118118
}
119119
}
120120

121121
pub trait TimeoutReadExt<H>
122122
where
123-
H: Read + AsRawFd,
123+
H: Read + AsFd,
124124
{
125125
fn with_timeout<T: Into<Option<Duration>>>(self, timeout: T) -> TimeoutReader<H>;
126126
}
127127

128128
impl<H> TimeoutReadExt<H> for H
129129
where
130-
H: Read + AsRawFd,
130+
H: Read + AsFd,
131131
{
132132
fn with_timeout<T: Into<Option<Duration>>>(self, timeout: T) -> TimeoutReader<H> {
133133
TimeoutReader::new(self, timeout)
@@ -139,6 +139,7 @@ mod tests {
139139
use std::env;
140140
use std::fs::File;
141141
use std::io::Read;
142+
use std::os::fd::AsRawFd;
142143
use std::path::PathBuf;
143144
use std::time::Duration;
144145

@@ -177,7 +178,7 @@ mod tests {
177178

178179
assert_eq!(original_contents, read_contents);
179180

180-
assert_eq!(fp_fd, fp.as_raw_fd());
181+
assert_eq!(fp_fd, fp.as_fd().as_raw_fd());
181182
}
182183

183184
#[test]

src/utils.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,37 @@
99
use nix::libc::c_int;
1010
use nix::poll;
1111
use std::cmp;
12+
use std::convert::TryFrom;
1213
use std::io::{Error, ErrorKind, Result};
13-
use std::os::unix::io::AsRawFd;
14+
use std::os::fd::AsFd;
1415
use std::slice;
1516
use std::time::Duration;
1617

1718
/// Convert from a duration into milliseconds as the c_int type that poll expects.
1819
/// If the duration exceeds the number of milliseconds that can fit into a c_int,
1920
/// saturate the time to the max_value of c_int.
2021
pub fn duration_to_ms(duration: Duration) -> c_int {
21-
let secs = cmp::min(duration.as_secs(), c_int::max_value() as u64) as c_int;
22+
let secs = cmp::min(duration.as_secs(), c_int::MAX as u64) as c_int;
2223
let nanos = duration.subsec_nanos() as c_int;
2324

2425
secs.saturating_mul(1_000).saturating_add(nanos / 1_000_000)
2526
}
2627

2728
/// Wait until `to_fd` receives the poll event from `events`, up to `timeout` length
2829
/// of time.
29-
pub fn wait_until_ready<R: AsRawFd>(
30+
pub fn wait_until_ready(
3031
timeout: Option<c_int>,
31-
to_fd: &R,
32+
fd: &impl AsFd,
3233
events: poll::PollFlags,
3334
) -> Result<()> {
3435
if let Some(timeout) = timeout {
35-
let mut pfd = poll::PollFd::new(to_fd.as_raw_fd(), events);
36-
let mut s = unsafe { slice::from_raw_parts_mut(&mut pfd, 1) };
36+
let mut pfd = poll::PollFd::new(fd.as_fd(), events);
37+
let s = slice::from_mut(&mut pfd);
3738

38-
let retval = poll::poll(&mut s, timeout).map_err(|e| Error::new(ErrorKind::Other, e))?;
39+
let timeout =
40+
poll::PollTimeout::try_from(timeout).map_err(|e| Error::new(ErrorKind::Other, e))?;
41+
42+
let retval = poll::poll(s, timeout).map_err(|e| Error::new(ErrorKind::Other, e))?;
3943
if retval == 0 {
4044
return Err(Error::new(
4145
ErrorKind::TimedOut,

src/writer.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use std::io::Result;
1212
use std::io::Seek;
1313
use std::io::SeekFrom;
1414
use std::io::Write;
15-
use std::os::unix::io::AsRawFd;
15+
use std::os::fd::AsFd;
16+
use std::os::fd::BorrowedFd;
1617
use std::time::Duration;
1718

1819
use super::utils;
@@ -29,15 +30,15 @@ use super::utils;
2930
/// of the `Write` trait could also be produced by the `TimeoutWriter`.
3031
pub struct TimeoutWriter<H>
3132
where
32-
H: Write + AsRawFd,
33+
H: Write + AsFd,
3334
{
3435
timeout: Option<c_int>,
3536
handle: H,
3637
}
3738

3839
impl<H> Write for TimeoutWriter<H>
3940
where
40-
H: Write + AsRawFd,
41+
H: Write + AsFd,
4142
{
4243
fn write(&mut self, buf: &[u8]) -> Result<usize> {
4344
utils::wait_until_ready(self.timeout, &self.handle, PollFlags::POLLOUT)?;
@@ -52,25 +53,25 @@ where
5253

5354
impl<H> Seek for TimeoutWriter<H>
5455
where
55-
H: Write + AsRawFd + Seek,
56+
H: Write + AsFd + Seek,
5657
{
5758
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
5859
self.handle.seek(pos)
5960
}
6061
}
6162

62-
impl<H> AsRawFd for TimeoutWriter<H>
63+
impl<H> AsFd for TimeoutWriter<H>
6364
where
64-
H: Write + AsRawFd,
65+
H: Write + AsFd,
6566
{
66-
fn as_raw_fd(&self) -> c_int {
67-
self.handle.as_raw_fd()
67+
fn as_fd(&self) -> BorrowedFd<'_> {
68+
self.handle.as_fd()
6869
}
6970
}
7071

7172
impl<H> Clone for TimeoutWriter<H>
7273
where
73-
H: Write + AsRawFd + Clone,
74+
H: Write + AsFd + Clone,
7475
{
7576
fn clone(&self) -> TimeoutWriter<H> {
7677
TimeoutWriter {
@@ -82,7 +83,7 @@ where
8283

8384
impl<H> TimeoutWriter<H>
8485
where
85-
H: Write + AsRawFd,
86+
H: Write + AsFd,
8687
{
8788
/// Create a new `TimeoutWriter` with an optional timeout.
8889
///
@@ -118,21 +119,21 @@ where
118119
pub fn new<T: Into<Option<Duration>>>(handle: H, timeout: T) -> TimeoutWriter<H> {
119120
TimeoutWriter {
120121
timeout: timeout.into().map(utils::duration_to_ms),
121-
handle: handle,
122+
handle,
122123
}
123124
}
124125
}
125126

126127
pub trait TimeoutWriteExt<H>
127128
where
128-
H: Write + AsRawFd,
129+
H: Write + AsFd,
129130
{
130131
fn with_timeout<T: Into<Option<Duration>>>(self, timeout: T) -> TimeoutWriter<H>;
131132
}
132133

133134
impl<H> TimeoutWriteExt<H> for H
134135
where
135-
H: Write + AsRawFd,
136+
H: Write + AsFd,
136137
{
137138
fn with_timeout<T: Into<Option<Duration>>>(self, timeout: T) -> TimeoutWriter<H> {
138139
TimeoutWriter::new(self, timeout)

0 commit comments

Comments
 (0)