Verified Commit 53d0f01a authored by Alex Butler's avatar Alex Butler
Browse files

Merge branch 'safe'

parents dd5251d8 77055823
Pipeline #1110 passed with stages
in 3 minutes and 31 seconds
......@@ -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,
......@@ -775,7 +803,7 @@ impl<Data: Deref<Target=[u8]>> FontInfo<Data> {
}
close_shape(&mut vertices[..], &mut num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy);
assert!(num_vertices <= vertices.len());
unsafe{vertices.set_len(num_vertices)};
vertices.truncate(num_vertices);
vertices
} else if number_of_contours == -1 {
// Compound shapes
......
Supports Markdown
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