Verified Commit 42957464 authored by Simon Sapin's avatar Simon Sapin Committed by Alex Butler

Replace `transmute` with an explicit match and panic.

parent dd5251d8
......@@ -39,11 +39,39 @@ pub struct Vertex {
impl Vertex {
pub fn vertex_type(&self) -> VertexType {
unsafe{::std::mem::transmute(self.type_)}
match self.type_ {
1 => VertexType::MoveTo,
2 => VertexType::LineTo,
3 => VertexType::CurveTo,
type_ => panic!("Invalid vertex type: {}", type_),
}
}
}
#[derive(Copy, Clone, Debug)]
#[test]
fn test_vertex_type() {
fn v(type_: VertexType) -> Vertex {
Vertex { x: 0, y: 0, cx: 0, cy: 0, type_: type_ as u8 }
}
assert_eq!(v(VertexType::MoveTo).vertex_type(), VertexType::MoveTo);
assert_eq!(v(VertexType::LineTo).vertex_type(), VertexType::LineTo);
assert_eq!(v(VertexType::CurveTo).vertex_type(), VertexType::CurveTo);
}
#[test]
#[should_panic]
fn test_invalid_vertex_type() {
let v = Vertex { x: 0, y: 0, cx: 0, cy: 0, type_: 255 };
let s = match v.vertex_type() {
VertexType::MoveTo => "move to",
VertexType::LineTo => "line to",
VertexType::CurveTo => "curve to",
};
// With `Vertex::vertex_type` defined as `transmute` this would be undefined behavior:
println!("{}", s);
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum VertexType {
MoveTo = 1,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment