Struct Context

Source
pub struct Context { /* private fields */ }

Implementations§

Source§

impl Context

Source

pub fn new(handle: HANDLE, buff: Vec<u8>, io_type: IOType) -> Self

Source

pub fn get_buff(&self) -> &[u8]

Source

pub fn io_type(&self) -> &IOType

Source

pub fn cancel(self) -> Result<()>

Cancel this context with handle and overlapped. There is no guarantee that underlying drivers correctly support cancellation.

use std::net::{TcpStream, TcpListener};
use iocp_rs::{CompletionPort, net::TcpStreamExt, AsHandle};
use std::thread::{spawn, sleep};
use std::os::windows::io::{AsRawSocket, RawSocket};
use std::io::Write;
use std::time::Duration;
use windows_sys::Win32::Foundation::HANDLE;
 
struct MyTcpStream {
    inner: TcpStream
}
 
impl AsRawSocket for MyTcpStream {
    fn as_raw_socket(&self) -> RawSocket {
        self.inner.as_raw_socket()
    }
}
 
impl AsHandle for MyTcpStream {
    type Handle = HANDLE;
    fn as_handle(&self) -> Self::Handle {
        self.inner.as_raw_socket() as HANDLE
    }
}
 
impl TcpStreamExt for MyTcpStream {}
 
fn main() {
    let cmp = CompletionPort::new(1).unwrap();
    let join = spawn(|| {
            let listener = TcpListener::bind("127.0.0.1:999").unwrap();
            let (mut stream, _) = listener.accept().unwrap();
             
            sleep(Duration::from_secs(5));
            let ret = stream.write(b"123");
            dbg!(ret);
    });
 
    let stream = TcpStream::connect("127.0.0.1:999").unwrap();
    let mut stream1 = MyTcpStream {inner: stream};
    cmp.add(1, &stream1).unwrap();
 
    let context = stream1.read(vec![0; 3]).unwrap();
    context.cancel();
    let result = cmp.get(None).unwrap();
    assert_eq!(result.token(), 1);
}
 

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.