Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 8, 2018 06:20
Show Gist options
  • Save rust-play/2d3726c253d66839510e0e723f48b657 to your computer and use it in GitHub Desktop.
Save rust-play/2d3726c253d66839510e0e723f48b657 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
pub struct DemDiscrete {
pub y: Vec<f32>,
pub x: Vec<f32>,
pub fx: Vec<f32>,
}
pub trait Base{
fn get_fx(&self)-> &Vec<f32>;
fn get_x(&self)-> &Vec<f32>;
fn get_y(&self)-> &Vec<f32>;
fn get_mut_fx(&mut self)-> &mut Vec<f32>;
fn get_mut_x(&mut self)-> &mut Vec<f32>;
fn get_mut_y(&mut self)-> &mut Vec<f32>;
}
impl Base for DemDiscrete{
fn get_fx(&self) -> &Vec<f32>{
&self.fx
}
fn get_x(&self) -> &Vec<f32>{
&self.x
}
fn get_y(&self) -> &Vec<f32>{
&self.y
}
fn get_mut_fx(&mut self) -> &mut Vec<f32>{
&mut self.fx
}
fn get_mut_x(&mut self) -> &mut Vec<f32>{
&mut self.x
}
fn get_mut_y(&mut self) -> &mut Vec<f32>{
&mut self.y
}
}
fn body_force<T:Base>(entity: &mut T){
let fx = entity.get_mut_fx();
let x = entity.get_mut_x();
for i in 0..fx.len(){
fx[i] += 10.;
x[i] += 1.;
}
}
fn main(){
let mut ent = DemDiscrete{
x: vec![1., 2., 3.],
y: vec![1., 2., 3.],
fx: vec![1., 2., 3.],
};
body_force(&mut ent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment