Skip to main content
Sign in
Snippets Groups Projects
Select Git revision
  • 9b89acd3c45016320ab1355f34df09c7287c4551
  • master default protected
2 results

main.rs

Blame
  • main.rs 1.65 KiB
    extern crate heapless;
    extern crate untagged_option;
    use heapless::{ring_buffer::Consumer /*, ring_buffer::Producer */,
                   RingBuffer};
    use untagged_option::UntaggedOption;
    
    trait Dispatch {
        fn dispatch(&mut self);
    }
    
    struct MsgQ(RingBuffer<&'static mut Dispatch, [&'static mut Dispatch; 4]>);
    
    impl Dispatch for MsgQ {
        fn dispatch(&mut self) {
            self.0.dequeue().unwrap().dispatch();
        }
    }
    
    pub struct AConsumer<'a>(Consumer<'a, u8, [u8; 4]>);
    
    impl<'a> Dispatch for AConsumer<'a> {
        fn dispatch(&mut self) {
            a(self.0.dequeue().unwrap());
        }
    }
    
    pub struct BConsumer<'a>(Consumer<'a, u8, [u8; 4]>);
    
    impl<'a> Dispatch for BConsumer<'a> {
        fn dispatch(&mut self) {
            b(self.0.dequeue().unwrap());
        }
    }
    
    static mut RA: RingBuffer<u8, [u8; 4]> = RingBuffer::new();
    static mut RB: RingBuffer<u8, [u8; 4]> = RingBuffer::new();
    static mut AC: UntaggedOption<AConsumer<'static>> = UntaggedOption { none: () };
    static mut BC: UntaggedOption<BConsumer<'static>> = UntaggedOption { none: () };
    
    fn main() {
        unsafe {
            let (mut ap, ac) = RA.split();
            AC.some = AConsumer { 0: ac };
            let (mut bp, bc) = RB.split();
            BC.some = BConsumer { 0: bc };
    
            let mut mq = MsgQ {
                0: RingBuffer::new(),
            };
            let _ = ap.enqueue(1);
            let _ = mq.0.enqueue(&mut AC.some);
    
            let _ = ap.enqueue(2);
            let _ = mq.0.enqueue(&mut AC.some);
    
            let _ = bp.enqueue(3);
            let _ = mq.0.enqueue(&mut BC.some);
    
            mq.dispatch();
            mq.dispatch();
            mq.dispatch();
        }
    }
    
    fn a(d: u8) {
        println!("a : {}", d);
    }
    
    fn b(d: u8) {
        println!("b : {}", d);
    }