Function show_notes::e029::translate

source ·
pub fn translate(point: &mut Point, by_x: f32, by_y: f32)
Expand description

A safe interface for the unsafe ffi::translate function.

In this case, there are no invariants we need to maintain other than those which Rust always maintains, i.e. that the reference we have with &mut Point is a valid reference (not null, actually points to a Point, and so on). Since Rust guarantees this, we can simply directly call the unsafe extern function here.

I explicitly included the cast as *mut Point, but an &mut Point will be automatically converted to a *mut Point when needed, so it is unnecessary from a compiler perspective. It may, however, be helpful for making your intent clear to other users!

let mut point = Point { x: 0.0, y: 0.0 };
translate(&mut point, 2.4, 4.8);
assert_eq!(point, Point { x: 2.4, y: 4.8 });