blob: e1c4831314504a03d45b63794a788421c33d6fc8 [file] [log] [blame]
David Tolnay0beba912020-12-09 23:42:07 -08001use crate::syntax::atom::Atom::{self, *};
2use crate::syntax::{derive, Trait, Type, Types};
3
4impl<'a> Types<'a> {
5 pub fn is_guaranteed_pod(&self, ty: &Type) -> bool {
6 match ty {
7 Type::Ident(ident) => {
8 let ident = &ident.rust;
9 if let Some(atom) = Atom::from(ident) {
10 match atom {
11 Bool | Char | U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64
12 | Isize | F32 | F64 => true,
13 CxxString | RustString => false,
14 }
15 } else if let Some(strct) = self.structs.get(ident) {
16 derive::contains(&strct.derives, Trait::Copy)
17 || strct
18 .fields
19 .iter()
20 .all(|field| self.is_guaranteed_pod(&field.ty))
21 } else {
22 self.enums.contains_key(ident)
23 }
24 }
25 Type::RustBox(_)
26 | Type::RustVec(_)
27 | Type::UniquePtr(_)
28 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -080029 | Type::WeakPtr(_)
David Tolnay0beba912020-12-09 23:42:07 -080030 | Type::CxxVector(_)
31 | Type::Void(_) => false,
32 Type::Ref(_) | Type::Str(_) | Type::Fn(_) | Type::SliceRef(_) => true,
33 Type::Array(array) => self.is_guaranteed_pod(&array.inner),
34 }
35 }
36}