1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::task::Waker;

/// A central shared state of the socket where all socket data are stored.
pub struct State {
    /// A buffer holding buffered bytes of the socket.
    pub buf: Vec<u8>,

    /// The Waker reference of the root socket. The socket stores this reference
    /// to enable async notifications across cloned socket instances.
    pub waker: Option<Waker>,

    /// The maximum size of a chunk returned by a stream.
    pub chunk_size: usize,
}

impl State {
    /// Returns a new instance with a specific chunk size.
    pub fn with_chunk_size(chunk_size: usize) -> Self {
        let mut this = Self::default();
        this.chunk_size = chunk_size;
        this
    }

    /// Triggers the wake event if the waker has been set by the socket.
    pub fn wake(&mut self) {
        if let Some(waker) = self.waker.take() {
            waker.wake();
        }
    }
}

impl Default for State {
    fn default() -> Self {
        Self {
            buf: vec![],
            waker: None,
            chunk_size: 10,
        }
    }
}