Producer

Struct Producer 

Source
pub struct Producer<T: Copy> { /* private fields */ }

Implementations§

Source§

impl<T: Copy> Producer<T>

Source

pub fn current_message(&mut self) -> &mut T

Examples found in repository?
examples/client.rs (line 84)
80    pub fn run(&mut self, cmds: &[MsgCommand]) {
81        let pause = time::Duration::from_millis(10);
82
83        for cmd in cmds {
84            self.command.current_message().clone_from(cmd);
85            self.command.force_push();
86
87            loop {
88                match self.response.pop() {
89                    ConsumeResult::QueueError => panic!(),
90                    ConsumeResult::NoMessage => {
91                        thread::sleep(pause);
92                        continue;
93                    }
94                    ConsumeResult::NoNewMessage => {
95                        thread::sleep(pause);
96                        continue;
97                    }
98                    ConsumeResult::Success => {}
99                    ConsumeResult::SuccessMessagesDiscarded => {}
100                };
101
102                println!(
103                    "client received response: {}",
104                    self.response.current_message().unwrap()
105                );
106                break;
107            }
108        }
109        thread::sleep(time::Duration::from_millis(100));
110        STOP_EVENT_LISTERNER.store(true, Ordering::Relaxed);
111        self.event_listener.take().map(|h| h.join());
112    }
More examples
Hide additional examples
examples/server.rs (line 68)
53    fn run(&mut self) {
54        let mut run = true;
55        let mut cnt = 0;
56
57        while run {
58            let eventfd = self.command.eventfd().unwrap();
59            let _ = wait_pollin(eventfd, Duration::from_millis(10));
60            match self.command.pop() {
61                ConsumeResult::QueueError => panic!(),
62                ConsumeResult::NoMessage => continue,
63                ConsumeResult::NoNewMessage => continue,
64                ConsumeResult::Success => {}
65                ConsumeResult::SuccessMessagesDiscarded => {}
66            };
67            let cmd = self.command.current_message().unwrap();
68            self.response.current_message().id = cmd.id;
69            let args: [i32; 3] = cmd.args;
70            println!("server received command: {}", cmd);
71
72            let cmdid: CommandId = unsafe { ::std::mem::transmute(cmd.id) };
73            self.response.current_message().result = match cmdid {
74                CommandId::Hello => 0,
75                CommandId::Stop => {
76                    run = false;
77                    0
78                }
79                CommandId::SendEvent => {
80                    self.send_events(args[0] as u32, args[1] as u32, args[2] != 0)
81                }
82                CommandId::Div => {
83                    let (err, res) = self.div(args[0], args[1]);
84                    self.response.current_message().data = res;
85                    err
86                }
87            };
88            self.response.force_push();
89
90            cnt = cnt + 1;
91        }
92    }
93    fn send_events(&mut self, id: u32, num: u32, force: bool) -> i32 {
94        for i in 0..num {
95            let event = self.event.current_message();
96            event.id = id;
97            event.nr = i;
98            if force {
99                self.event.force_push();
100            } else {
101                if self.event.try_push() == ProduceTryResult::QueueFull {
102                    return i as i32;
103                }
104            }
105        }
106        num as i32
107    }
Source

pub fn force_push(&mut self) -> ProduceForceResult

Examples found in repository?
examples/client.rs (line 85)
80    pub fn run(&mut self, cmds: &[MsgCommand]) {
81        let pause = time::Duration::from_millis(10);
82
83        for cmd in cmds {
84            self.command.current_message().clone_from(cmd);
85            self.command.force_push();
86
87            loop {
88                match self.response.pop() {
89                    ConsumeResult::QueueError => panic!(),
90                    ConsumeResult::NoMessage => {
91                        thread::sleep(pause);
92                        continue;
93                    }
94                    ConsumeResult::NoNewMessage => {
95                        thread::sleep(pause);
96                        continue;
97                    }
98                    ConsumeResult::Success => {}
99                    ConsumeResult::SuccessMessagesDiscarded => {}
100                };
101
102                println!(
103                    "client received response: {}",
104                    self.response.current_message().unwrap()
105                );
106                break;
107            }
108        }
109        thread::sleep(time::Duration::from_millis(100));
110        STOP_EVENT_LISTERNER.store(true, Ordering::Relaxed);
111        self.event_listener.take().map(|h| h.join());
112    }
More examples
Hide additional examples
examples/server.rs (line 88)
53    fn run(&mut self) {
54        let mut run = true;
55        let mut cnt = 0;
56
57        while run {
58            let eventfd = self.command.eventfd().unwrap();
59            let _ = wait_pollin(eventfd, Duration::from_millis(10));
60            match self.command.pop() {
61                ConsumeResult::QueueError => panic!(),
62                ConsumeResult::NoMessage => continue,
63                ConsumeResult::NoNewMessage => continue,
64                ConsumeResult::Success => {}
65                ConsumeResult::SuccessMessagesDiscarded => {}
66            };
67            let cmd = self.command.current_message().unwrap();
68            self.response.current_message().id = cmd.id;
69            let args: [i32; 3] = cmd.args;
70            println!("server received command: {}", cmd);
71
72            let cmdid: CommandId = unsafe { ::std::mem::transmute(cmd.id) };
73            self.response.current_message().result = match cmdid {
74                CommandId::Hello => 0,
75                CommandId::Stop => {
76                    run = false;
77                    0
78                }
79                CommandId::SendEvent => {
80                    self.send_events(args[0] as u32, args[1] as u32, args[2] != 0)
81                }
82                CommandId::Div => {
83                    let (err, res) = self.div(args[0], args[1]);
84                    self.response.current_message().data = res;
85                    err
86                }
87            };
88            self.response.force_push();
89
90            cnt = cnt + 1;
91        }
92    }
93    fn send_events(&mut self, id: u32, num: u32, force: bool) -> i32 {
94        for i in 0..num {
95            let event = self.event.current_message();
96            event.id = id;
97            event.nr = i;
98            if force {
99                self.event.force_push();
100            } else {
101                if self.event.try_push() == ProduceTryResult::QueueFull {
102                    return i as i32;
103                }
104            }
105        }
106        num as i32
107    }
Source

pub fn try_push(&mut self) -> ProduceTryResult

Examples found in repository?
examples/server.rs (line 101)
93    fn send_events(&mut self, id: u32, num: u32, force: bool) -> i32 {
94        for i in 0..num {
95            let event = self.event.current_message();
96            event.id = id;
97            event.nr = i;
98            if force {
99                self.event.force_push();
100            } else {
101                if self.event.try_push() == ProduceTryResult::QueueFull {
102                    return i as i32;
103                }
104            }
105        }
106        num as i32
107    }
Source

pub fn eventfd(&self) -> Option<BorrowedFd<'_>>

Source

pub fn take_eventfd(&mut self) -> Option<EventFd>

Source

pub fn enable_cache(&mut self)

Source

pub fn disable_cache(&mut self)

Auto Trait Implementations§

§

impl<T> Freeze for Producer<T>

§

impl<T> RefUnwindSafe for Producer<T>
where T: RefUnwindSafe,

§

impl<T> Send for Producer<T>
where T: Send,

§

impl<T> !Sync for Producer<T>

§

impl<T> Unpin for Producer<T>
where T: Unpin,

§

impl<T> UnwindSafe for Producer<T>
where T: UnwindSafe,

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.