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

impl<T> Read for UniquePtr<T> where ... Pin<&a mut T> : Read. #1368

Merged
merged 1 commit into from
Aug 15, 2024
Merged
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
35 changes: 35 additions & 0 deletions src/unique_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use core::mem::{self, MaybeUninit};
use core::ops::{Deref, DerefMut};
use core::pin::Pin;

#[cfg(feature = "std")]
use std::io::Read;

/// Binding to C++ `std::unique_ptr<T, std::default_delete<T>>`.
#[repr(C)]
pub struct UniquePtr<T>
Expand Down Expand Up @@ -181,6 +184,38 @@ where
}
}

/// Forwarding `Read` trait implementation in a manner similar to `Box<T>`. Note that the
/// implementation will panic for null `UniquePtr<T>`.
#[cfg(feature = "std")]
impl<T> Read for UniquePtr<T>
where
for<'a> Pin<&'a mut T>: Read,
T: UniquePtrTarget,
{
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.pin_mut().read(buf)
}

#[inline]
fn read_to_end(&mut self, buf: &mut std::vec::Vec<u8>) -> std::io::Result<usize> {
self.pin_mut().read_to_end(buf)
}

#[inline]
fn read_to_string(&mut self, buf: &mut std::string::String) -> std::io::Result<usize> {
self.pin_mut().read_to_string(buf)
}

#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
self.pin_mut().read_exact(buf)
}

// TODO: Foward other `Read` trait methods when they get stabilized (e.g.
// `read_buf` and/or `is_read_vectored`).
}

/// Trait bound for types which may be used as the `T` inside of a
/// `UniquePtr<T>` in generic code.
///
Expand Down