Moved the state out to a different file

This commit is contained in:
Gender Shrapnel 2020-05-31 05:29:13 +02:00
parent a888ec9a8a
commit ceb26b40b1
3 changed files with 27 additions and 22 deletions

View File

@ -4,28 +4,21 @@
extern crate rocket; extern crate rocket;
extern crate serde; extern crate serde;
mod simulation;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use rocket::State; use rocket::State as RocketState;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use serde::Serialize;
use simulation::{Object, Simulation}; mod simulation;
mod state;
#[derive(Clone, Serialize)] use simulation::Simulation;
struct SimState { use state::{State, WrappedState};
pub iteration: u64,
pub objects: Vec<Object>
}
type WrappedState = Arc<Mutex<SimState>>;
#[get("/")] #[get("/")]
fn index(sim: State<WrappedState>) -> Template { fn index(sim: RocketState<WrappedState>) -> Template {
let state = sim.lock().unwrap(); let state = sim.lock().unwrap();
@ -49,7 +42,7 @@ fn simulation(s: WrappedState) {
} }
fn main() { fn main() {
let state = Arc::new(Mutex::new(SimState { let state = Arc::new(Mutex::new(State {
iteration: 0, objects: vec![] iteration: 0, objects: vec![]
})); }));

View File

@ -1,6 +1,7 @@
use legion::prelude::*; use legion::prelude::*;
use rand::prelude::*; use rand::prelude::*;
use serde::Serialize;
use crate::state::Object;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct Name { struct Name {
@ -23,13 +24,6 @@ pub struct Simulation {
world: World world: World
} }
#[derive(Clone, Serialize)]
pub struct Object {
pub name: String,
pub x: f64,
pub y: f64,
}
impl Simulation { impl Simulation {
pub fn new() -> Self { pub fn new() -> Self {
let universe = Universe::new(); let universe = Universe::new();

18
src/state.rs Normal file
View File

@ -0,0 +1,18 @@
use std::sync::{Arc, Mutex};
use serde::Serialize;
#[derive(Clone, Serialize)]
pub struct Object {
pub name: String,
pub x: f64,
pub y: f64,
}
#[derive(Clone, Serialize)]
pub struct State {
pub iteration: u64,
pub objects: Vec<Object>
}
pub type WrappedState = Arc<Mutex<State>>;