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
41
42
43
44
45
46
47
48
49
50
use crate::{pool_item::PoolItem, request_response::RequestResponse};

use super::ThreadRequestResponse;

/// This struct is returned in response to a request to add a pool item to the thread pool
/// The success field indicates that the pool item was successfully constructed
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AddResponse {
    id: usize,
    result: Result<usize, String>,
}

impl AddResponse {
    pub fn new(id: usize, result: Result<usize, String>) -> Self {
        assert!(
            result.is_err() || result.clone().unwrap() == id,
            "id in the success result must match the result"
        );
        Self { id, result }
    }

    pub fn id(&self) -> usize {
        self.id
    }

    pub fn result(&self) -> Result<&usize, &String> {
        self.result.as_ref()
    }
}

impl<P> From<AddResponse> for ThreadRequestResponse<P>
where
    P: PoolItem,
{
    fn from(response: AddResponse) -> Self {
        ThreadRequestResponse::AddPoolItem(RequestResponse::<P, P::Init>::Response(response))
    }
}

impl<P> From<ThreadRequestResponse<P>> for AddResponse
where
    P: PoolItem,
{
    fn from(response: ThreadRequestResponse<P>) -> Self {
        let ThreadRequestResponse::<P>::AddPoolItem(RequestResponse::<P, P::Init>::Response(response)) = response else {
            panic!("unexpected")
        };
        response
    }
}