hirun 0.1.13

A concurrent framework for asynchronous programming based on event-driven, non-blocking I/O mechanism
Documentation

use crate::{Error, Result};
use core::any::Any;
use core::cell::UnsafeCell;

type Boxed<'a, T: ?Sized> = hipool::Boxed<'a, T, hipool::MemPool>;

pub struct Connection<'a> {
    inner: UnsafeCell<ConnInner<'a>>,
}

struct ConnInner<'a> {
    pool: &'a hipool::MemPool,
    ctx: Boxed<'a, [Boxed<'a, dyn Any>]>,
}

impl<'a> Connection<'a> {
    fn get_pool(&self) -> &hipool::MemPool {
        self.inner().pool
    }

    fn recv(&self, buf: Boxed<'a, [u8]>) {}

    fn set_ctx(&self, ctx: Boxed<'a, dyn Any + 'a>) {
        self.inner_mut().ctx[0] = ctx;
    }

    fn inner(&self) -> &ConnInner<'a> {
        unsafe { &*self.inner.get() }
    }

    fn inner_mut(&self) -> &mut ConnInner<'a> {
        unsafe { &mut *self.inner.get() }
    }

    //fn set_ctx(&self, 
}


    //fn set_ctx<T: ?Sized + 'a>(&'a mut self,  ctx: Boxed<'a, T>);
    //fn get_ctx<T: ?Sized>(&'a mut self) -> Option<Boxed<'a, T>>; 

enum Buf<'a> {
    PeerClosed,
    Shutdown(i32),
    Close,
    Data(&'a mut [u8]),
}

pub trait Filter {
    fn filter_in<'a>(&self, data: Boxed<'a, [u8]>, conn: &'a Connection<'a>) -> Result<()> {
        let pool = conn.get_pool();
        let buf = Boxed::new_slice_in(pool, 128, 0).unwrap();
        //conn.set_ctx(buf);
        conn.recv(buf); 

        let ctx = Boxed::new_in(pool, 124).unwrap();
        //let any_ctx: Boxed<'a, dyn Any> = ctx.cast().unwrap();
        //conn.set_ctx(any_ctx);
        Ok(())
    }
}