future.rs
1 // Copyright (C) 2019-2025 ADnet Contributors 2 // This file is part of the ADL library. 3 4 // The ADL library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 9 // The ADL library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 14 // You should have received a copy of the GNU General Public License 15 // along with the ADL library. If not, see <https://www.gnu.org/licenses/>. 16 17 use crate::{Location, Type}; 18 19 use serde::{Deserialize, Serialize}; 20 use std::fmt; 21 22 /// A future type consisting of the type of the inputs. 23 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] 24 pub struct FutureType { 25 // Optional type specification of inputs. 26 pub inputs: Vec<Type>, 27 // The location of the function that produced the future. 28 pub location: Option<Location>, 29 // Whether or not the type has been explicitly specified. 30 pub is_explicit: bool, 31 } 32 33 impl FutureType { 34 /// Initialize a new future type. 35 pub fn new(inputs: Vec<Type>, location: Option<Location>, is_explicit: bool) -> Self { 36 Self { inputs, location, is_explicit } 37 } 38 39 /// Returns the inputs of the future type. 40 pub fn inputs(&self) -> &[Type] { 41 &self.inputs 42 } 43 44 /// Returns the location of the future type. 45 pub fn location(&self) -> &Option<Location> { 46 &self.location 47 } 48 } 49 50 impl Default for crate::FutureType { 51 fn default() -> Self { 52 Self::new(vec![], None, false) 53 } 54 } 55 56 impl fmt::Display for crate::FutureType { 57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 58 write!(f, "Future<Fn({})>", self.inputs.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(",")) 59 } 60 } 61 62 impl From<FutureType> for Type { 63 fn from(value: FutureType) -> Self { 64 Type::Future(value) 65 } 66 }