Add sfe_to_memcpy

This commit is contained in:
Richard Feldman 2020-03-16 21:55:24 -04:00
parent e7b391cd6b
commit 77bf2547ac

View file

@ -78,6 +78,22 @@ impl<'a> Layout<'a> {
} }
} }
pub fn safe_to_memcpy(&self) -> bool {
use Layout::*;
match self {
Builtin(builtin) => builtin.safe_to_memcpy(),
Struct(fields) => fields
.iter()
.all(|(_, field_layout)| field_layout.safe_to_memcpy()),
Tag(tags) => tags.iter().all(|tag_layout| tag_layout.safe_to_memcpy()),
FunctionPointer(_, _) => {
// Function pointers are immutable and can always be safely copied
true
}
}
}
pub fn stack_size(&self, pointer_size: u32) -> u32 { pub fn stack_size(&self, pointer_size: u32) -> u32 {
use Layout::*; use Layout::*;
@ -138,6 +154,17 @@ impl<'a> Builtin<'a> {
List(_) | EmptyList => Builtin::LIST_WORDS * pointer_size, List(_) | EmptyList => Builtin::LIST_WORDS * pointer_size,
} }
} }
pub fn safe_to_memcpy(&self) -> bool {
use Builtin::*;
match self {
Int64 | Float64 | Bool(_, _) | Byte(_) | EmptyStr | EmptyMap | EmptyList | EmptySet => {
true
}
Str | Map(_, _) | Set(_) | List(_) => false,
}
}
} }
#[allow(clippy::cognitive_complexity)] #[allow(clippy::cognitive_complexity)]