Skip to content

Instantly share code, notes, and snippets.

@Mathspy
Last active April 24, 2020 11:52
Show Gist options
  • Save Mathspy/032329a5edfd5ae0def43117b024163b to your computer and use it in GitHub Desktop.
Save Mathspy/032329a5edfd5ae0def43117b024163b to your computer and use it in GitHub Desktop.
My first sound unsafe code lol
use std::{
pin::Pin,
task::{Context, Poll},
};
use tokio::io::AsyncRead;
enum Either<T, U> {
A(T),
B(U),
}
impl<T, U> AsyncRead for Either<T, U>
where
T: AsyncRead,
U: AsyncRead,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<tokio::io::Result<usize>> {
let either = unsafe { Pin::into_inner_unchecked(self) };
match either {
Either::A(reader) => {
AsyncRead::poll_read(unsafe { Pin::new_unchecked(reader) }, cx, buf)
}
Either::B(reader) => {
AsyncRead::poll_read(unsafe { Pin::new_unchecked(reader) }, cx, buf)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment