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
//! Definition of the VariableData trait. A VariableData can contain any data which has a `'static`
//! type.

use std::any::{Any, TypeId};
use std::fmt::Debug;

use crate::protocol::{EvaluatedTerm, ProtocolTypes};

pub trait VariableData<PT: ProtocolTypes>: Debug + EvaluatedTerm<PT> {
    fn boxed(&self) -> Box<dyn VariableData<PT>>;
    fn boxed_any(&self) -> Box<dyn Any>;
    fn boxed_term(&self) -> Box<dyn EvaluatedTerm<PT>>;
    fn type_id(&self) -> TypeId;
    fn type_name(&self) -> &'static str;
}

/// A VariableData is cloneable and has a `'static` type. This data type is used throughout
/// tlspuffin to handle data of dynamic size.
impl<T, PT: ProtocolTypes> VariableData<PT> for T
where
    T: Clone + Debug + EvaluatedTerm<PT> + 'static,
{
    fn boxed(&self) -> Box<dyn VariableData<PT>> {
        Box::new(self.clone())
    }

    fn boxed_any(&self) -> Box<dyn Any> {
        Box::new(self.clone())
    }

    fn boxed_term(&self) -> Box<dyn EvaluatedTerm<PT>> {
        Box::new(self.clone())
    }

    fn type_id(&self) -> TypeId {
        Any::type_id(self)
    }

    fn type_name(&self) -> &'static str {
        std::any::type_name::<T>()
    }
}