integer_type.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 adl_span::{Symbol, sym}; 18 19 use serde::{Deserialize, Serialize}; 20 use std::fmt; 21 22 /// Explicit integer type. 23 #[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize)] 24 pub enum IntegerType { 25 U8, 26 U16, 27 U32, 28 U64, 29 U128, 30 31 I8, 32 I16, 33 I32, 34 I64, 35 I128, 36 } 37 38 impl IntegerType { 39 /// Is the integer type a signed one? 40 pub fn is_signed(&self) -> bool { 41 use IntegerType::*; 42 matches!(self, I8 | I16 | I32 | I64 | I128) 43 } 44 45 /// Returns the symbol for the integer type. 46 pub fn symbol(self) -> Symbol { 47 match self { 48 Self::I8 => sym::i8, 49 Self::I16 => sym::i16, 50 Self::I32 => sym::i32, 51 Self::I64 => sym::i64, 52 Self::I128 => sym::i128, 53 Self::U8 => sym::u8, 54 Self::U16 => sym::u16, 55 Self::U32 => sym::u32, 56 Self::U64 => sym::u64, 57 Self::U128 => sym::u128, 58 } 59 } 60 } 61 62 impl fmt::Display for IntegerType { 63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 64 match *self { 65 IntegerType::U8 => write!(f, "u8"), 66 IntegerType::U16 => write!(f, "u16"), 67 IntegerType::U32 => write!(f, "u32"), 68 IntegerType::U64 => write!(f, "u64"), 69 IntegerType::U128 => write!(f, "u128"), 70 71 IntegerType::I8 => write!(f, "i8"), 72 IntegerType::I16 => write!(f, "i16"), 73 IntegerType::I32 => write!(f, "i32"), 74 IntegerType::I64 => write!(f, "i64"), 75 IntegerType::I128 => write!(f, "i128"), 76 } 77 } 78 }