Skip to content

Commit a3c8b41

Browse files
committed
Update to io-lifetimes 0.7.0-beta.0.
The main change here is that view types no longer implement `DerefMut`, which is needed by `Read` and `Write`. Fortunately, there's a way to get a `DerefMut` implementation: sunfishcode/io-lifetimes#32 (comment)
1 parent bf71a25 commit a3c8b41

File tree

4 files changed

+53
-45
lines changed

4 files changed

+53
-45
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repository = "https://github.com/sunfishcode/io-extras"
1111
exclude = ["/.github"]
1212

1313
[dependencies]
14-
io-lifetimes = { version = "0.7.0-alpha.0", default-features = false }
14+
io-lifetimes = { version = "0.7.0-beta.0", default-features = false }
1515

1616
# Optionally depend on async-std to implement traits for its types.
1717
#

examples/hello.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() -> io::Result<()> {
2020
)?;
2121

2222
// Obtain a `FilelikeView` and use it to write.
23-
writeln!(stdout.as_filelike_view::<std::fs::File>(), "hello, world")?;
23+
writeln!(&*stdout.as_filelike_view::<std::fs::File>(), "hello, world")?;
2424

2525
Ok(())
2626
}

src/raw.rs

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -196,33 +196,33 @@ impl Read for RawReadable {
196196
// Safety: The caller of `as_readable()`, which is unsafe, is expected
197197
// to ensure that the underlying resource outlives this
198198
// `RawReadable`.
199-
unsafe { as_file_view(self.0) }.read(buf)
199+
unsafe { &*as_file_view(self.0) }.read(buf)
200200
}
201201

202202
#[inline]
203203
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
204-
unsafe { as_file_view(self.0) }.read_vectored(bufs)
204+
unsafe { &*as_file_view(self.0) }.read_vectored(bufs)
205205
}
206206

207207
#[cfg(can_vector)]
208208
#[inline]
209209
fn is_read_vectored(&self) -> bool {
210-
unsafe { as_file_view(self.0) }.is_read_vectored()
210+
unsafe { &*as_file_view(self.0) }.is_read_vectored()
211211
}
212212

213213
#[inline]
214214
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
215-
unsafe { as_file_view(self.0) }.read_to_end(buf)
215+
unsafe { &*as_file_view(self.0) }.read_to_end(buf)
216216
}
217217

218218
#[inline]
219219
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
220-
unsafe { as_file_view(self.0) }.read_to_string(buf)
220+
unsafe { &*as_file_view(self.0) }.read_to_string(buf)
221221
}
222222

223223
#[inline]
224224
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
225-
unsafe { as_file_view(self.0) }.read_exact(buf)
225+
unsafe { &*as_file_view(self.0) }.read_exact(buf)
226226
}
227227
}
228228

@@ -231,18 +231,20 @@ impl Read for RawReadable {
231231
#[inline]
232232
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
233233
match self.0 .0 {
234-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.read(buf),
235-
RawEnum::Socket(raw_socket) => unsafe { as_socket_view(raw_socket) }.read(buf),
234+
RawEnum::Handle(raw_handle) => unsafe { &*as_file_view(raw_handle) }.read(buf),
235+
RawEnum::Socket(raw_socket) => unsafe { &*as_socket_view(raw_socket) }.read(buf),
236236
RawEnum::Stdio(ref mut stdio) => stdio.read(buf),
237237
}
238238
}
239239

240240
#[inline]
241241
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
242242
match self.0 .0 {
243-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.read_vectored(bufs),
243+
RawEnum::Handle(raw_handle) => {
244+
unsafe { &*as_file_view(raw_handle) }.read_vectored(bufs)
245+
}
244246
RawEnum::Socket(raw_socket) => {
245-
unsafe { as_socket_view(raw_socket) }.read_vectored(bufs)
247+
unsafe { &*as_socket_view(raw_socket) }.read_vectored(bufs)
246248
}
247249
RawEnum::Stdio(ref mut stdio) => stdio.read_vectored(bufs),
248250
}
@@ -252,27 +254,31 @@ impl Read for RawReadable {
252254
#[inline]
253255
fn is_read_vectored(&self) -> bool {
254256
match self.0 .0 {
255-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.is_read_vectored(),
256-
RawEnum::Socket(raw_socket) => unsafe { as_socket_view(raw_socket) }.is_read_vectored(),
257+
RawEnum::Handle(raw_handle) => unsafe { &*as_file_view(raw_handle) }.is_read_vectored(),
258+
RawEnum::Socket(raw_socket) => {
259+
unsafe { &*as_socket_view(raw_socket) }.is_read_vectored()
260+
}
257261
RawEnum::Stdio(ref stdio) => stdio.is_read_vectored(),
258262
}
259263
}
260264

261265
#[inline]
262266
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
263267
match self.0 .0 {
264-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.read_to_end(buf),
265-
RawEnum::Socket(raw_socket) => unsafe { as_socket_view(raw_socket) }.read_to_end(buf),
268+
RawEnum::Handle(raw_handle) => unsafe { &*as_file_view(raw_handle) }.read_to_end(buf),
269+
RawEnum::Socket(raw_socket) => unsafe { &*as_socket_view(raw_socket) }.read_to_end(buf),
266270
RawEnum::Stdio(ref mut stdio) => stdio.read_to_end(buf),
267271
}
268272
}
269273

270274
#[inline]
271275
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
272276
match self.0 .0 {
273-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.read_to_string(buf),
277+
RawEnum::Handle(raw_handle) => {
278+
unsafe { &*as_file_view(raw_handle) }.read_to_string(buf)
279+
}
274280
RawEnum::Socket(raw_socket) => {
275-
unsafe { as_socket_view(raw_socket) }.read_to_string(buf)
281+
unsafe { &*as_socket_view(raw_socket) }.read_to_string(buf)
276282
}
277283
RawEnum::Stdio(ref mut stdio) => stdio.read_to_string(buf),
278284
}
@@ -281,8 +287,8 @@ impl Read for RawReadable {
281287
#[inline]
282288
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
283289
match self.0 .0 {
284-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.read_exact(buf),
285-
RawEnum::Socket(raw_socket) => unsafe { as_socket_view(raw_socket) }.read_exact(buf),
290+
RawEnum::Handle(raw_handle) => unsafe { &*as_file_view(raw_handle) }.read_exact(buf),
291+
RawEnum::Socket(raw_socket) => unsafe { &*as_socket_view(raw_socket) }.read_exact(buf),
286292
RawEnum::Stdio(ref mut stdio) => stdio.read_exact(buf),
287293
}
288294
}
@@ -295,39 +301,39 @@ impl Write for RawWriteable {
295301
// Safety: The caller of `as_writeable()`, which is unsafe, is expected
296302
// to ensure that the underlying resource outlives this
297303
// `RawReadable`.
298-
unsafe { as_file_view(self.0) }.write(buf)
304+
unsafe { &*as_file_view(self.0) }.write(buf)
299305
}
300306

301307
#[inline]
302308
fn flush(&mut self) -> io::Result<()> {
303-
unsafe { as_file_view(self.0) }.flush()
309+
unsafe { &*as_file_view(self.0) }.flush()
304310
}
305311

306312
#[inline]
307313
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
308-
unsafe { as_file_view(self.0) }.write_vectored(bufs)
314+
unsafe { &*as_file_view(self.0) }.write_vectored(bufs)
309315
}
310316

311317
#[cfg(can_vector)]
312318
#[inline]
313319
fn is_write_vectored(&self) -> bool {
314-
unsafe { as_file_view(self.0) }.is_write_vectored()
320+
unsafe { &*as_file_view(self.0) }.is_write_vectored()
315321
}
316322

317323
#[inline]
318324
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
319-
unsafe { as_file_view(self.0) }.write_all(buf)
325+
unsafe { &*as_file_view(self.0) }.write_all(buf)
320326
}
321327

322328
#[cfg(write_all_vectored)]
323329
#[inline]
324330
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
325-
unsafe { as_file_view(self.0) }.write_all_vectored(bufs)
331+
unsafe { &*as_file_view(self.0) }.write_all_vectored(bufs)
326332
}
327333

328334
#[inline]
329335
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
330-
unsafe { as_file_view(self.0) }.write_fmt(fmt)
336+
unsafe { &*as_file_view(self.0) }.write_fmt(fmt)
331337
}
332338
}
333339

@@ -336,27 +342,29 @@ impl Write for RawWriteable {
336342
#[inline]
337343
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
338344
match self.0 .0 {
339-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.write(buf),
340-
RawEnum::Socket(raw_socket) => unsafe { as_socket_view(raw_socket) }.write(buf),
345+
RawEnum::Handle(raw_handle) => unsafe { &*as_file_view(raw_handle) }.write(buf),
346+
RawEnum::Socket(raw_socket) => unsafe { &*as_socket_view(raw_socket) }.write(buf),
341347
RawEnum::Stdio(ref mut stdio) => stdio.write(buf),
342348
}
343349
}
344350

345351
#[inline]
346352
fn flush(&mut self) -> io::Result<()> {
347353
match self.0 .0 {
348-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.flush(),
349-
RawEnum::Socket(raw_socket) => unsafe { as_socket_view(raw_socket) }.flush(),
354+
RawEnum::Handle(raw_handle) => unsafe { &*as_file_view(raw_handle) }.flush(),
355+
RawEnum::Socket(raw_socket) => unsafe { &*as_socket_view(raw_socket) }.flush(),
350356
RawEnum::Stdio(ref mut stdio) => stdio.flush(),
351357
}
352358
}
353359

354360
#[inline]
355361
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
356362
match self.0 .0 {
357-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.write_vectored(bufs),
363+
RawEnum::Handle(raw_handle) => {
364+
unsafe { &*as_file_view(raw_handle) }.write_vectored(bufs)
365+
}
358366
RawEnum::Socket(raw_socket) => {
359-
unsafe { as_socket_view(raw_socket) }.write_vectored(bufs)
367+
unsafe { &*as_socket_view(raw_socket) }.write_vectored(bufs)
360368
}
361369
RawEnum::Stdio(ref mut stdio) => stdio.write_vectored(bufs),
362370
}
@@ -366,9 +374,11 @@ impl Write for RawWriteable {
366374
#[inline]
367375
fn is_write_vectored(&self) -> bool {
368376
match self.0 .0 {
369-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.is_write_vectored(),
377+
RawEnum::Handle(raw_handle) => {
378+
unsafe { &*as_file_view(raw_handle) }.is_write_vectored()
379+
}
370380
RawEnum::Socket(raw_socket) => {
371-
unsafe { as_socket_view(raw_socket) }.is_write_vectored()
381+
unsafe { &*as_socket_view(raw_socket) }.is_write_vectored()
372382
}
373383
RawEnum::Stdio(ref stdio) => stdio.is_write_vectored(),
374384
}
@@ -377,8 +387,8 @@ impl Write for RawWriteable {
377387
#[inline]
378388
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
379389
match self.0 .0 {
380-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.write_all(buf),
381-
RawEnum::Socket(raw_socket) => unsafe { as_socket_view(raw_socket) }.write_all(buf),
390+
RawEnum::Handle(raw_handle) => unsafe { &*as_file_view(raw_handle) }.write_all(buf),
391+
RawEnum::Socket(raw_socket) => unsafe { &*as_socket_view(raw_socket) }.write_all(buf),
382392
RawEnum::Stdio(ref mut stdio) => stdio.write_all(buf),
383393
}
384394
}
@@ -388,10 +398,10 @@ impl Write for RawWriteable {
388398
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
389399
match self.0 .0 {
390400
RawEnum::Handle(raw_handle) => {
391-
unsafe { as_file_view(raw_handle) }.write_all_vectored(bufs)
401+
unsafe { &*as_file_view(raw_handle) }.write_all_vectored(bufs)
392402
}
393403
RawEnum::Socket(raw_socket) => {
394-
unsafe { as_socket_view(raw_socket) }.write_all_vectored(bufs)
404+
unsafe { &*as_socket_view(raw_socket) }.write_all_vectored(bufs)
395405
}
396406
RawEnum::Stdio(ref mut stdio) => stdio.write_all_vectored(bufs),
397407
}
@@ -400,8 +410,8 @@ impl Write for RawWriteable {
400410
#[inline]
401411
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
402412
match self.0 .0 {
403-
RawEnum::Handle(raw_handle) => unsafe { as_file_view(raw_handle) }.write_fmt(fmt),
404-
RawEnum::Socket(raw_socket) => unsafe { as_socket_view(raw_socket) }.write_fmt(fmt),
413+
RawEnum::Handle(raw_handle) => unsafe { &*as_file_view(raw_handle) }.write_fmt(fmt),
414+
RawEnum::Socket(raw_socket) => unsafe { &*as_socket_view(raw_socket) }.write_fmt(fmt),
405415
RawEnum::Stdio(ref mut stdio) => stdio.write_fmt(fmt),
406416
}
407417
}

tests/tcp_stream.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn tcp_stream_write() -> io::Result<()> {
2929

3030
// Obtain a `SocketlikeView` and use it to write.
3131
writeln!(
32-
stream.as_socketlike_view::<TcpStream>(),
32+
&*stream.as_socketlike_view::<TcpStream>(),
3333
"Write via SocketlikeView"
3434
)?;
3535

@@ -86,9 +86,7 @@ fn tcp_stream_socketlike_view() -> io::Result<()> {
8686
// Obtain a `SocketlikeView` and use it to read.
8787
let stream = accept()?;
8888
let mut buf = String::new();
89-
stream
90-
.as_socketlike_view::<TcpStream>()
91-
.read_to_string(&mut buf)?;
89+
(&*stream.as_socketlike_view::<TcpStream>()).read_to_string(&mut buf)?;
9290
assert_eq!(buf, "hello, world");
9391

9492
Ok(())

0 commit comments

Comments
 (0)