roussillon_type_system/types/
functional.rs

1use std::rc::Rc;
2
3use crate::identity::Identifier;
4use crate::types::concept::{DataType, Type};
5use crate::types::primitive::Primitive;
6use crate::types::sequence::Tuple;
7use crate::value::concept::ValueCell;
8use crate::value::error::TypeResult;
9
10#[derive(Clone, Debug)]
11pub struct FunctionType {
12    pub arguments: Tuple,
13    pub return_type: Type,
14}
15
16impl FunctionType {
17    pub fn new(arguments: Tuple, return_type: Type) -> Self {
18        Self {
19            arguments,
20            return_type,
21        }
22    }
23    pub fn to_rc(self) -> Rc<Self> {
24        Rc::new(self)
25    }
26}
27
28impl DataType for FunctionType {
29    fn size(&self) -> usize {
30        8
31    }
32
33    fn typename(&self) -> String {
34        format!("{} -> {}", self.arguments.typename(), self.return_type)
35    }
36
37    fn construct_from_raw(&self, raw: &[u8]) -> TypeResult<ValueCell> {
38        Primitive::Bytes(size_of::<usize>()).construct_from_raw(raw)
39    }
40}
41
42#[derive(Clone, Debug)]
43pub struct FunctionDeclaration {
44    pub identifier: Identifier,
45    pub signature: Rc<FunctionType>,
46}
47
48impl FunctionDeclaration {
49    pub fn new(identifier: Identifier, signature: Rc<FunctionType>) -> Self {
50        Self {
51            identifier,
52            signature,
53        }
54    }
55}