Struct Context

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

Implementations§

Source§

impl Context

Source

pub fn new(uring: Uring, spawner: LocalSpawner) -> Self

Source

pub fn spawn<C, F>(&mut self, c: C)
where C: FnOnce(Context) -> F, F: Future<Output = ()> + 'static,

Examples found in repository?
examples/echo-server.rs (line 15)
10async fn run_server(mut kio: kio::Context) -> io::Result<()> {
11    let mut socket = kio.listen("127.0.0.1:8080")?;
12
13    loop {
14        let conn = kio.accept(&mut socket).await?;
15        kio.spawn(move |kio| async { run_conn(kio, conn).await.unwrap() });
16    }
17}
Source

pub async fn accept(&mut self, socket: &mut TcpListener) -> Result<TcpStream>

Accepts a new connection on the given listener.

Examples found in repository?
examples/echo-server.rs (line 14)
10async fn run_server(mut kio: kio::Context) -> io::Result<()> {
11    let mut socket = kio.listen("127.0.0.1:8080")?;
12
13    loop {
14        let conn = kio.accept(&mut socket).await?;
15        kio.spawn(move |kio| async { run_conn(kio, conn).await.unwrap() });
16    }
17}
Source

pub fn buffer(&mut self, size: usize) -> Box<[u8]>

Returns a buffer of at least the given size.

Examples found in repository?
examples/echo-server.rs (line 22)
19async fn run_conn(mut kio: kio::Context, mut conn: net::TcpStream) -> io::Result<()> {
20    // Set up the connection limit
21    let mut kio = kio.timeout(time::Duration::from_secs(5));
22    let mut buffer = kio.buffer(4096);
23
24    loop {
25        let size = kio.read(&mut conn, &mut buffer).await?;
26        if size == 0 {
27            return Ok(());
28        }
29
30        kio.write_full(&mut conn, &buffer[..size]).await?;
31    }
32}
More examples
Hide additional examples
examples/echo-client.rs (line 23)
11async fn run(mut kio: kio::Context) -> anyhow::Result<()> {
12    // Set up the connection limit
13    let mut kio = kio.timeout(time::Duration::from_secs(5));
14
15    let mut stream = kio.connect("127.0.0.1:8080").await?;
16    let message = "hello world";
17
18    kio.write_full(&mut stream, message.as_bytes()).await?;
19    kio.write_close(&mut stream)?;
20
21    println!("sent: {}", message);
22
23    let mut buffer = kio.buffer(32);
24    let size = kio.read_full(&mut stream, &mut buffer).await?;
25
26    println!("received: {}", String::from_utf8_lossy(&buffer[..size]));
27
28    Ok(())
29}
Source

pub async fn connect<A>(&mut self, addr: A) -> Result<TcpStream>
where A: ToSocketAddrs,

Examples found in repository?
examples/echo-client.rs (line 15)
11async fn run(mut kio: kio::Context) -> anyhow::Result<()> {
12    // Set up the connection limit
13    let mut kio = kio.timeout(time::Duration::from_secs(5));
14
15    let mut stream = kio.connect("127.0.0.1:8080").await?;
16    let message = "hello world";
17
18    kio.write_full(&mut stream, message.as_bytes()).await?;
19    kio.write_close(&mut stream)?;
20
21    println!("sent: {}", message);
22
23    let mut buffer = kio.buffer(32);
24    let size = kio.read_full(&mut stream, &mut buffer).await?;
25
26    println!("received: {}", String::from_utf8_lossy(&buffer[..size]));
27
28    Ok(())
29}
Source

pub fn listen<A>(&mut self, addr: A) -> Result<TcpListener>
where A: ToSocketAddrs,

Wrapper around net::TcpListener::bind.

Examples found in repository?
examples/echo-server.rs (line 11)
10async fn run_server(mut kio: kio::Context) -> io::Result<()> {
11    let mut socket = kio.listen("127.0.0.1:8080")?;
12
13    loop {
14        let conn = kio.accept(&mut socket).await?;
15        kio.spawn(move |kio| async { run_conn(kio, conn).await.unwrap() });
16    }
17}
Source

pub async fn read( &mut self, stream: &mut TcpStream, buffer: &mut [u8], ) -> Result<usize>

Reads at least one byte into the buffer range and returns the size. If the stream is closed, returns zero.

Examples found in repository?
examples/echo-server.rs (line 25)
19async fn run_conn(mut kio: kio::Context, mut conn: net::TcpStream) -> io::Result<()> {
20    // Set up the connection limit
21    let mut kio = kio.timeout(time::Duration::from_secs(5));
22    let mut buffer = kio.buffer(4096);
23
24    loop {
25        let size = kio.read(&mut conn, &mut buffer).await?;
26        if size == 0 {
27            return Ok(());
28        }
29
30        kio.write_full(&mut conn, &buffer[..size]).await?;
31    }
32}
Source

pub async fn read_full( &mut self, stream: &mut TcpStream, buffer: &mut [u8], ) -> Result<usize>

Reads until the entire buffer range is populated or the stream is closed.

Examples found in repository?
examples/echo-client.rs (line 24)
11async fn run(mut kio: kio::Context) -> anyhow::Result<()> {
12    // Set up the connection limit
13    let mut kio = kio.timeout(time::Duration::from_secs(5));
14
15    let mut stream = kio.connect("127.0.0.1:8080").await?;
16    let message = "hello world";
17
18    kio.write_full(&mut stream, message.as_bytes()).await?;
19    kio.write_close(&mut stream)?;
20
21    println!("sent: {}", message);
22
23    let mut buffer = kio.buffer(32);
24    let size = kio.read_full(&mut stream, &mut buffer).await?;
25
26    println!("received: {}", String::from_utf8_lossy(&buffer[..size]));
27
28    Ok(())
29}
Source

pub fn read_close(&self, stream: &mut TcpStream) -> Result<()>

Source

pub async fn write( &mut self, stream: &mut TcpStream, buffer: &[u8], ) -> Result<usize>

Writes at least one byte within the buffer range to the socket. Returns the amount written.

Source

pub async fn write_then( &mut self, stream: &mut TcpStream, buffer: &[u8], ) -> Result<usize>

Writes at least one byte within the buffer range to the socket before running the next task. Returns the amount written.

Source

pub async fn write_full( &mut self, stream: &mut TcpStream, buffer: &[u8], ) -> Result<()>

Writes the entire buffer range to the socket.

Examples found in repository?
examples/echo-server.rs (line 30)
19async fn run_conn(mut kio: kio::Context, mut conn: net::TcpStream) -> io::Result<()> {
20    // Set up the connection limit
21    let mut kio = kio.timeout(time::Duration::from_secs(5));
22    let mut buffer = kio.buffer(4096);
23
24    loop {
25        let size = kio.read(&mut conn, &mut buffer).await?;
26        if size == 0 {
27            return Ok(());
28        }
29
30        kio.write_full(&mut conn, &buffer[..size]).await?;
31    }
32}
More examples
Hide additional examples
examples/echo-client.rs (line 18)
11async fn run(mut kio: kio::Context) -> anyhow::Result<()> {
12    // Set up the connection limit
13    let mut kio = kio.timeout(time::Duration::from_secs(5));
14
15    let mut stream = kio.connect("127.0.0.1:8080").await?;
16    let message = "hello world";
17
18    kio.write_full(&mut stream, message.as_bytes()).await?;
19    kio.write_close(&mut stream)?;
20
21    println!("sent: {}", message);
22
23    let mut buffer = kio.buffer(32);
24    let size = kio.read_full(&mut stream, &mut buffer).await?;
25
26    println!("received: {}", String::from_utf8_lossy(&buffer[..size]));
27
28    Ok(())
29}
Source

pub fn write_close(&self, stream: &mut TcpStream) -> Result<()>

Examples found in repository?
examples/echo-client.rs (line 19)
11async fn run(mut kio: kio::Context) -> anyhow::Result<()> {
12    // Set up the connection limit
13    let mut kio = kio.timeout(time::Duration::from_secs(5));
14
15    let mut stream = kio.connect("127.0.0.1:8080").await?;
16    let message = "hello world";
17
18    kio.write_full(&mut stream, message.as_bytes()).await?;
19    kio.write_close(&mut stream)?;
20
21    println!("sent: {}", message);
22
23    let mut buffer = kio.buffer(32);
24    let size = kio.read_full(&mut stream, &mut buffer).await?;
25
26    println!("received: {}", String::from_utf8_lossy(&buffer[..size]));
27
28    Ok(())
29}
Source

pub fn cancel(&mut self)

Cancels all active tasks.

Source

pub fn timeout(&mut self, _expires: Duration) -> Self

Creates a new context that will be cancelled after the given duration.

Examples found in repository?
examples/echo-server.rs (line 21)
19async fn run_conn(mut kio: kio::Context, mut conn: net::TcpStream) -> io::Result<()> {
20    // Set up the connection limit
21    let mut kio = kio.timeout(time::Duration::from_secs(5));
22    let mut buffer = kio.buffer(4096);
23
24    loop {
25        let size = kio.read(&mut conn, &mut buffer).await?;
26        if size == 0 {
27            return Ok(());
28        }
29
30        kio.write_full(&mut conn, &buffer[..size]).await?;
31    }
32}
More examples
Hide additional examples
examples/echo-client.rs (line 13)
11async fn run(mut kio: kio::Context) -> anyhow::Result<()> {
12    // Set up the connection limit
13    let mut kio = kio.timeout(time::Duration::from_secs(5));
14
15    let mut stream = kio.connect("127.0.0.1:8080").await?;
16    let message = "hello world";
17
18    kio.write_full(&mut stream, message.as_bytes()).await?;
19    kio.write_close(&mut stream)?;
20
21    println!("sent: {}", message);
22
23    let mut buffer = kio.buffer(32);
24    let size = kio.read_full(&mut stream, &mut buffer).await?;
25
26    println!("received: {}", String::from_utf8_lossy(&buffer[..size]));
27
28    Ok(())
29}

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Context

§

impl !RefUnwindSafe for Context

§

impl !Send for Context

§

impl !Sync for Context

§

impl Unpin for Context

§

impl !UnwindSafe for Context

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.