Start building out type inference.
This commit is contained in:
@@ -80,6 +80,62 @@ impl FromStr for PrimitiveType {
|
||||
}
|
||||
|
||||
impl PrimitiveType {
|
||||
/// Return true if this type can be safely cast into the target type.
|
||||
pub fn can_cast_to(&self, target: &PrimitiveType) -> bool {
|
||||
match self {
|
||||
PrimitiveType::U8 => match target {
|
||||
PrimitiveType::U8 => true,
|
||||
PrimitiveType::U16 => true,
|
||||
PrimitiveType::U32 => true,
|
||||
PrimitiveType::U64 => true,
|
||||
PrimitiveType::I16 => true,
|
||||
PrimitiveType::I32 => true,
|
||||
PrimitiveType::I64 => true,
|
||||
_ => false,
|
||||
},
|
||||
PrimitiveType::U16 => match target {
|
||||
PrimitiveType::U16 => true,
|
||||
PrimitiveType::U32 => true,
|
||||
PrimitiveType::U64 => true,
|
||||
PrimitiveType::I32 => true,
|
||||
PrimitiveType::I64 => true,
|
||||
_ => false,
|
||||
},
|
||||
PrimitiveType::U32 => match target {
|
||||
PrimitiveType::U32 => true,
|
||||
PrimitiveType::U64 => true,
|
||||
PrimitiveType::I64 => true,
|
||||
_ => false,
|
||||
},
|
||||
PrimitiveType::U64 => match target {
|
||||
PrimitiveType::U64 => true,
|
||||
_ => false,
|
||||
},
|
||||
PrimitiveType::I8 => match target {
|
||||
PrimitiveType::I8 => true,
|
||||
PrimitiveType::I16 => true,
|
||||
PrimitiveType::I32 => true,
|
||||
PrimitiveType::I64 => true,
|
||||
_ => false,
|
||||
},
|
||||
PrimitiveType::I16 => match target {
|
||||
PrimitiveType::I16 => true,
|
||||
PrimitiveType::I32 => true,
|
||||
PrimitiveType::I64 => true,
|
||||
_ => false,
|
||||
},
|
||||
PrimitiveType::I32 => match target {
|
||||
PrimitiveType::I32 => true,
|
||||
PrimitiveType::I64 => true,
|
||||
_ => false,
|
||||
},
|
||||
PrimitiveType::I64 => match target {
|
||||
PrimitiveType::I64 => true,
|
||||
_ => false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to cast the given value to this type, returning the new value.
|
||||
///
|
||||
/// Returns an error if the cast is not safe *in* *general*. This means that
|
||||
|
||||
Reference in New Issue
Block a user