Remove internal cell

This commit is contained in:
hodasemi 2023-12-01 11:14:10 +01:00
parent d3fcd8eb7d
commit e9980a2551

View file

@ -3,7 +3,7 @@ pub mod additional_rfactor;
#[allow(warnings)]
mod rfactor_structs;
use std::{cell::Cell, fs::File, mem, path::Path};
use std::{fs::File, mem, path::Path};
use additional_rfactor::*;
pub use additional_rfactor::{rF2Vec3, rF2VehicleTelemetry, rF2Wheel};
@ -17,8 +17,8 @@ struct ShMReader {
_file: File,
shm: Mmap,
version: Cell<VersionHeader>,
last_query: Cell<f32>,
version: VersionHeader,
last_query: f32,
time_between_writes: f32,
}
@ -31,30 +31,30 @@ impl ShMReader {
_file: file,
shm: mmap,
version: Cell::new(VersionHeader::default()),
last_query: Cell::new(now),
version: VersionHeader::default(),
last_query: now,
time_between_writes,
})
}
fn check_version_update(&self) -> bool {
fn check_version_update(&mut self) -> bool {
let new_version = VersionHeader::from(&self.shm[0..VersionHeader::SIZE]);
let are_different = self.version.get().check_for_update(&new_version);
let are_different = self.version.check_for_update(&new_version);
if are_different {
self.version.set(new_version);
self.version = new_version;
}
are_different
}
fn check_time(&self, now: f32) -> bool {
if now < self.last_query.get() + self.time_between_writes {
fn check_time(&mut self, now: f32) -> bool {
if now < self.last_query + self.time_between_writes {
return false;
}
self.last_query.set(now);
self.last_query = now;
true
}
@ -82,7 +82,7 @@ impl TelemetryReader {
})
}
pub fn query_telemetry(&self, now: f32) -> Option<Vec<rF2VehicleTelemetry>> {
pub fn query_telemetry(&mut self, now: f32) -> Option<Vec<rF2VehicleTelemetry>> {
if !self.mm_reader.check_time(now) || !self.mm_reader.check_version_update() {
return None;
}
@ -126,7 +126,7 @@ impl ScoringReader {
}
pub fn vehicle_scoring(
&self,
&mut self,
now: f32,
) -> Option<(ScoringInfoV01, Vec<VehicleScoringInfoV01>)> {
if !self.mm_reader.check_time(now) || !self.mm_reader.check_version_update() {
@ -162,7 +162,7 @@ mod tests {
#[test]
fn test_telemetry() {
let start = Instant::now();
let telemetry_reader = TelemetryReader::new(start.elapsed().as_secs_f32()).unwrap();
let mut telemetry_reader = TelemetryReader::new(start.elapsed().as_secs_f32()).unwrap();
for _ in 0..25 {
telemetry_reader.query_telemetry(start.elapsed().as_secs_f32());
@ -173,7 +173,7 @@ mod tests {
#[test]
fn test_scoring() {
let start = Instant::now();
let scoring_reader = ScoringReader::new(start.elapsed().as_secs_f32()).unwrap();
let mut scoring_reader = ScoringReader::new(start.elapsed().as_secs_f32()).unwrap();
for _ in 0..25 {
scoring_reader.vehicle_scoring(start.elapsed().as_secs_f32());
@ -184,8 +184,8 @@ mod tests {
#[test]
fn test_combined() {
let start = Instant::now();
let scoring_reader = ScoringReader::new(start.elapsed().as_secs_f32()).unwrap();
let telemetry_reader = TelemetryReader::new(start.elapsed().as_secs_f32()).unwrap();
let mut scoring_reader = ScoringReader::new(start.elapsed().as_secs_f32()).unwrap();
let mut telemetry_reader = TelemetryReader::new(start.elapsed().as_secs_f32()).unwrap();
let mut player_id = None;