Last active
April 24, 2020 11:52
-
-
Save Mathspy/032329a5edfd5ae0def43117b024163b to your computer and use it in GitHub Desktop.
My first sound unsafe code lol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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