Skip to content

Commit

Permalink
get -> read, get_mut -> write
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Aug 12, 2023
1 parent 21d6967 commit 470b022
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 83 deletions.
22 changes: 11 additions & 11 deletions egregoria/src/economy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,39 +175,39 @@ const ITEMS_PATH: &str = "../assets/items.json";
const COMPANIES_PATH: &str = "../assets/companies.json";

pub fn init_market(_: &mut World, res: &mut Resources) {
res.get_mut::<ItemRegistry>()
res.write::<ItemRegistry>()
.load_item_definitions(&common::saveload::load_string(ITEMS_PATH).unwrap());

res.get_mut::<GoodsCompanyRegistry>().load(
res.write::<GoodsCompanyRegistry>().load(
&common::saveload::load_string(COMPANIES_PATH).unwrap(),
&res.get::<ItemRegistry>(),
&res.read::<ItemRegistry>(),
);

let market = Market::new(
&res.get::<ItemRegistry>(),
&res.get::<GoodsCompanyRegistry>(),
&res.read::<ItemRegistry>(),
&res.read::<GoodsCompanyRegistry>(),
);
res.insert(market);
let stats = EcoStats::new(&res.get::<ItemRegistry>());
let stats = EcoStats::new(&res.read::<ItemRegistry>());
res.insert(stats);
}

#[profiling::function]
pub fn market_update(world: &mut World, resources: &mut Resources) {
let n_workers = world.humans.len();

let mut m = resources.get_mut::<Market>();
let job_opening = resources.get::<ItemRegistry>().id("job-opening");
let mut gvt = resources.get_mut::<Government>();
let tick = resources.get::<Tick>().0;
let mut m = resources.write::<Market>();
let job_opening = resources.read::<ItemRegistry>().id("job-opening");
let mut gvt = resources.write::<Government>();
let tick = resources.read::<Tick>().0;

if tick % TICKS_PER_SECOND == 0 {
gvt.money -= n_workers as i64 * WORKER_CONSUMPTION_PER_SECOND;
}

let trades = m.make_trades();

resources.get_mut::<EcoStats>().advance(tick, trades);
resources.write::<EcoStats>().advance(tick, trades);

for &trade in trades.iter() {
log::debug!("A trade was made! {:?}", trade);
Expand Down
4 changes: 2 additions & 2 deletions egregoria/src/engine_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl WorldCommand {
let cost = Government::action_cost(self, goria);
goria.write::<Government>().money -= cost;

let mut rep = goria.resources.get_mut::<Replay>();
let mut rep = goria.resources.write::<Replay>();
if rep.enabled {
let tick = goria.read::<Tick>();
rep.commands.push((*tick, self.clone()));
Expand Down Expand Up @@ -276,7 +276,7 @@ impl WorldCommand {
}
Init(ref opts) => {
if opts.save_replay {
let mut rep = goria.resources.get_mut::<Replay>();
let mut rep = goria.resources.write::<Replay>();
rep.enabled = true;
let tick = goria.read::<Tick>();
rep.commands.push((*tick, Init(opts.clone())));
Expand Down
16 changes: 8 additions & 8 deletions egregoria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl Egregoria {
}

pub fn get_tick(&self) -> u32 {
self.resources.get::<Tick>().0
self.resources.read::<Tick>().0
}

pub fn hashes(&self) -> BTreeMap<String, u64> {
Expand Down Expand Up @@ -295,7 +295,7 @@ impl Egregoria {

pub fn save_to_disk(&self, save_name: &str) {
common::saveload::CompressedBincode::save(&self, save_name);
let rep = self.resources.get::<Replay>();
let rep = self.resources.read::<Replay>();
if rep.enabled {
common::saveload::JSONPretty::save(&*rep, &format!("{save_name}_replay"));
}
Expand All @@ -318,27 +318,27 @@ impl Egregoria {
}

pub fn write_or_default<T: Any + Send + Sync + Default>(&mut self) -> RefMut<T> {
self.resources.get_mut_or_default::<T>()
self.resources.write_or_default::<T>()
}

pub fn try_write<T: Any + Send + Sync>(&self) -> Option<RefMut<T>> {
self.resources.try_get_mut().ok()
self.resources.try_write().ok()
}

pub fn write<T: Any + Send + Sync>(&self) -> RefMut<T> {
self.resources.get_mut()
self.resources.write()
}

pub fn read<T: Any + Send + Sync>(&self) -> Ref<T> {
self.resources.get()
self.resources.read()
}

pub fn map(&self) -> Ref<'_, Map> {
self.resources.get()
self.resources.read()
}

pub(crate) fn map_mut(&self) -> RefMut<'_, Map> {
self.resources.get_mut()
self.resources.write()
}

pub fn insert<T: Any + Send + Sync>(&mut self, res: T) {
Expand Down
4 changes: 2 additions & 2 deletions egregoria/src/map_dynamic/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ impl DispatchOne {
}

pub fn dispatch_system(world: &mut World, resources: &mut Resources) {
let mut dispatcher = resources.get_mut::<Dispatcher>();
let map = resources.get::<Map>();
let mut dispatcher = resources.write::<Dispatcher>();
let map = resources.read::<Map>();
dispatcher.update(&map, world);
}

Expand Down
6 changes: 3 additions & 3 deletions egregoria/src/map_dynamic/itinerary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ impl Inspect<ItineraryKind> for ItineraryKind {

#[profiling::function]
pub fn itinerary_update(world: &mut World, resources: &mut Resources) {
let time = &*resources.get::<GameTime>();
let map = &*resources.get::<Map>();
let tick = *resources.get::<Tick>();
let time = &*resources.read::<GameTime>();
let map = &*resources.read::<Map>();
let tick = *resources.read::<Tick>();

world.query_it_trans_speed().for_each(
|(it, trans, speed): (&mut Itinerary, &mut Transform, f32)| {
Expand Down
10 changes: 5 additions & 5 deletions egregoria/src/map_dynamic/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ debug_inspect_impl!(RoutingStep);

#[profiling::function]
pub fn routing_changed_system(world: &mut World, resources: &mut Resources) {
let map: &Map = &resources.get();
let parking: &mut ParkingManagement = &mut resources.get_mut();
let map: &Map = &resources.read();
let parking: &mut ParkingManagement = &mut resources.write();

world.humans.values_mut().for_each(|h| {
let router = &mut h.router;
Expand Down Expand Up @@ -111,9 +111,9 @@ pub fn routing_changed_system(world: &mut World, resources: &mut Resources) {

#[profiling::function]
pub fn routing_update_system(world: &mut World, resources: &mut Resources) {
let map: &Map = &resources.get();
let cbuf_human: &ParCommandBuffer<HumanEnt> = &resources.get();
let cbuf_vehicle: &ParCommandBuffer<VehicleEnt> = &resources.get();
let map: &Map = &resources.read();
let cbuf_human: &ParCommandBuffer<HumanEnt> = &resources.read();
let cbuf_vehicle: &ParCommandBuffer<VehicleEnt> = &resources.read();

world.humans.iter_mut().for_each(|(body, h)| {
if h.router.cur_step.is_none() && h.router.steps.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/physics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Collider {

#[profiling::function]
pub fn coworld_synchronize(world: &mut World, resources: &mut Resources) {
let mut coworld = resources.get_mut::<CollisionWorld>();
let mut coworld = resources.write::<CollisionWorld>();

world.query_trans_speed_coll_vehicle().for_each(
|(trans, kin, coll, v): (&Transform, &Speed, Collider, Option<&Vehicle>)| {
Expand Down
10 changes: 5 additions & 5 deletions egregoria/src/souls/freight_station.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ pub fn freight_station_soul(
}

pub fn freight_station_system(world: &mut World, resources: &mut Resources) {
let cbuf = resources.get::<ParCommandBuffer<FreightStationEnt>>();
let mut dispatch = resources.get_mut::<Dispatcher>();
let map = resources.get::<Map>();
let time = resources.get::<GameTime>();
let tick = *resources.get::<Tick>();
let cbuf = resources.read::<ParCommandBuffer<FreightStationEnt>>();
let mut dispatch = resources.write::<Dispatcher>();
let map = resources.read::<Map>();
let time = resources.read::<GameTime>();
let tick = *resources.read::<Tick>();

for (me, f) in world.freight_stations.iter_mut() {
let pos = f.trans;
Expand Down
14 changes: 7 additions & 7 deletions egregoria/src/souls/goods_company.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,12 @@ pub fn company_soul(goria: &mut Egregoria, company: GoodsCompany) -> Option<Soul

#[profiling::function]
pub fn company_system(world: &mut World, res: &mut Resources) {
let delta = res.get::<GameTime>().realdelta;
let cbuf: &ParCommandBuffer<CompanyEnt> = &res.get();
let cbuf_human: &ParCommandBuffer<HumanEnt> = &res.get();
let binfos: &BuildingInfos = &res.get();
let market: &Market = &res.get();
let map: &Map = &res.get();
let delta = res.read::<GameTime>().realdelta;
let cbuf: &ParCommandBuffer<CompanyEnt> = &res.read();
let cbuf_human: &ParCommandBuffer<HumanEnt> = &res.read();
let binfos: &BuildingInfos = &res.read();
let market: &Market = &res.read();
let map: &Map = &res.read();

world.companies.iter_mut().for_each(|(me, c)| {
let n_workers = c.workers.0.len();
Expand Down Expand Up @@ -326,7 +326,7 @@ pub fn company_system(world: &mut World, res: &mut Resources) {
cbuf.exec_ent(me, move |goria| {
let (world, res) = goria.world_res();
if let Some(SoulID::FreightStation(owner)) =
res.get::<BuildingInfos>().owner(owner_build)
res.read::<BuildingInfos>().owner(owner_build)
{
if let Some(mut f) = world.freight_stations.get_mut(owner) {

Check warning on line 331 in egregoria/src/souls/goods_company.rs

View workflow job for this annotation

GitHub Actions / build

variable does not need to be mutable

Check warning on line 331 in egregoria/src/souls/goods_company.rs

View workflow job for this annotation

GitHub Actions / build

variable does not need to be mutable

Check warning on line 331 in egregoria/src/souls/goods_company.rs

View workflow job for this annotation

GitHub Actions / build

variable does not need to be mutable
f.f.wanted_cargo += 1;
Expand Down
10 changes: 5 additions & 5 deletions egregoria/src/souls/human.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ enum NextDesire<'a> {

#[profiling::function]
pub fn update_decision_system(world: &mut World, resources: &mut Resources) {
let ra = &*resources.get();
let rb = &*resources.get();
let rc = &*resources.get();
let rd = &*resources.get();
let re = &*resources.get();
let ra = &*resources.read();
let rb = &*resources.read();
let rc = &*resources.read();
let rd = &*resources.read();
let re = &*resources.read();

world.humans.iter_mut().for_each(|(ent, h)| {
update_decision(
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/transportation/pedestrian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn random_pedestrian_shirt_color(r: &mut RandProvider) -> Color {

#[profiling::function]
pub fn pedestrian_decision_system(world: &mut World, resources: &mut Resources) {
let ra = &*resources.get();
let ra = &*resources.read();
world.humans
.values_mut()
//.par_bridge()
Expand Down
12 changes: 6 additions & 6 deletions egregoria/src/transportation/road.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use slotmapd::Key;

#[profiling::function]
pub fn vehicle_decision_system(world: &mut World, resources: &mut Resources) {
let ra = &*resources.get();
let rb = &*resources.get();
let rc = &*resources.get();
let ra = &*resources.read();
let rb = &*resources.read();
let rc = &*resources.read();

world.vehicles.iter_mut().for_each(|(ent, v)| {
let Some(ref coll) = v.collider else { return; };
Expand Down Expand Up @@ -78,9 +78,9 @@ pub fn vehicle_decision(

#[profiling::function]
pub fn vehicle_state_update_system(world: &mut World, resources: &mut Resources) {
let ra = &*resources.get();
let rb = &*resources.get();
let rc = &*resources.get();
let ra = &*resources.read();
let rb = &*resources.read();
let rc = &*resources.read();

world.vehicles.iter_mut().for_each(|(ent, v)| {
vehicle_state_update(
Expand Down
12 changes: 6 additions & 6 deletions egregoria/src/transportation/train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn spawn_train(
) -> Option<TrainID> {
let (world, res) = goria.world_res();

let map = res.get::<Map>();
let map = res.read::<Map>();
let lane = map.lanes().get(lane)?;

let (locopos, locodir) = lane.points.point_dir_along(dist);
Expand Down Expand Up @@ -197,8 +197,8 @@ pub fn traverse_forward<'a>(

#[profiling::function]
pub fn train_reservations_update(world: &mut World, resources: &mut Resources) {
let map = &*resources.get::<Map>();
let reservations = &mut *resources.get_mut::<TrainReservations>();
let map = &*resources.read::<Map>();
let reservations = &mut *resources.write::<TrainReservations>();
let lanes = map.lanes();
let inters = map.intersections();
world.trains.iter_mut().for_each(move |(me, train)| {
Expand Down Expand Up @@ -305,9 +305,9 @@ pub fn train_reservations_update(world: &mut World, resources: &mut Resources) {

#[profiling::function]
pub fn locomotive_system(world: &mut World, resources: &mut Resources) {
let map: &Map = &resources.get();
let time: &GameTime = &resources.get();
let reservs: &TrainReservations = &resources.get();
let map: &Map = &resources.read();
let time: &GameTime = &resources.read();
let reservs: &TrainReservations = &resources.read();

// asume iter order stays the same
let mut desired_speeds = Vec::with_capacity(world.trains.len());
Expand Down
14 changes: 7 additions & 7 deletions egregoria/src/utils/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ impl Resources {
.map(|resource| downcast_resource(resource.into_inner().unwrap()))
}

pub fn get_mut_or_default<T: Any + Send + Sync + Default>(&mut self) -> RefMut<T> {
self.get_mut_or_insert_with(Default::default)
pub fn write_or_default<T: Any + Send + Sync + Default>(&mut self) -> RefMut<T> {
self.write_or_insert_with(Default::default)
}

pub fn get_mut_or_insert_with<T: Any + Send + Sync>(
pub fn write_or_insert_with<T: Any + Send + Sync>(
&mut self,
f: impl FnOnce() -> T,
) -> RefMut<T> {
Expand All @@ -58,23 +58,23 @@ impl Resources {
}
}

pub fn get<T: Any + Send + Sync>(&self) -> Ref<T> {
pub fn read<T: Any + Send + Sync>(&self) -> Ref<T> {
Ref::from_lock(self.resources.get(&TypeId::of::<T>()).unwrap()).unwrap()
}

pub fn try_get<T: Any + Send + Sync>(&self) -> Result<Ref<T>, CantGetResource> {
pub fn try_read<T: Any + Send + Sync>(&self) -> Result<Ref<T>, CantGetResource> {
Ok(Ref::from_lock(
self.resources
.get(&TypeId::of::<T>())
.ok_or(NoSuchResource)?,
)?)
}

pub fn get_mut<T: Any + Send + Sync>(&self) -> RefMut<T> {
pub fn write<T: Any + Send + Sync>(&self) -> RefMut<T> {
RefMut::from_lock(self.resources.get(&TypeId::of::<T>()).unwrap()).unwrap()
}

pub fn try_get_mut<T: Any + Send + Sync>(&self) -> Result<RefMut<T>, CantGetResource> {
pub fn try_write<T: Any + Send + Sync>(&self) -> Result<RefMut<T>, CantGetResource> {
Ok(RefMut::from_lock(
self.resources
.get(&TypeId::of::<T>())
Expand Down
Loading

0 comments on commit 470b022

Please sign in to comment.