async_socket/
state.rs

1use std::task::Waker;
2
3/// A central shared state of the socket where all socket data are stored.
4pub struct State {
5    /// A buffer holding buffered bytes of the socket.
6    pub buf: Vec<u8>,
7
8    /// The Waker reference of the root socket. The socket stores this reference
9    /// to enable async notifications across cloned socket instances.
10    pub waker: Option<Waker>,
11
12    /// The maximum size of a chunk returned by a stream.
13    pub chunk_size: usize,
14}
15
16impl State {
17    /// Returns a new instance with a specific chunk size.
18    pub fn with_chunk_size(chunk_size: usize) -> Self {
19        let mut this = Self::default();
20        this.chunk_size = chunk_size;
21        this
22    }
23
24    /// Triggers the wake event if the waker has been set by the socket.
25    pub fn wake(&mut self) {
26        if let Some(waker) = self.waker.take() {
27            waker.wake();
28        }
29    }
30}
31
32impl Default for State {
33    fn default() -> Self {
34        Self {
35            buf: vec![],
36            waker: None,
37            chunk_size: 10,
38        }
39    }
40}