Skip to content

Commit

Permalink
update rust to 1.77.2
Browse files Browse the repository at this point in the history
  • Loading branch information
rlane committed Apr 14, 2024
1 parent 67cd656 commit 9e4e31b
Show file tree
Hide file tree
Showing 20 changed files with 74 additions and 75 deletions.
6 changes: 2 additions & 4 deletions frontend/app/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,8 @@ impl Component for Game {
false
}
Msg::Start => {
let shortcodes = vec![
context.props().player0.clone(),
context.props().player1.clone(),
];
let shortcodes = [context.props().player0.clone(),
context.props().player1.clone()];
let has_shortcodes = !shortcodes.iter().all(Option::is_none);
self.change_scenario(context, &context.props().scenario, !has_shortcodes);
if has_shortcodes {
Expand Down
3 changes: 1 addition & 2 deletions frontend/renderer/src/bullet_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ void main() {

let mut draws = vec![];
for bullets in snapshot.bullets.chunks(1000) {
let mut attribs = vec![];
attribs.reserve(bullets.len());
let mut attribs = Vec::with_capacity(bullets.len());
for bullet in bullets.iter() {
let p: Point2<f32> = bullet.position.cast();
let v: Vector2<f32> = bullet.velocity.cast();
Expand Down
3 changes: 1 addition & 2 deletions frontend/renderer/src/flare_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ void main() {
let vertices = geometry::quad();
let vertices_token = self.buffer_arena.write(vertices.as_slice());

let mut attribs: Vec<Attribs> = vec![];
attribs.reserve(snapshot.ships.len() * 4);
let mut attribs: Vec<Attribs> = Vec::with_capacity(snapshot.ships.len() * 4);
for ship in snapshot.ships.iter() {
let flare_positions = flare_positions(ship.class);
if flare_positions.is_empty() {
Expand Down
3 changes: 1 addition & 2 deletions frontend/renderer/src/line_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ void main() {
pub fn upload(&mut self, projection_matrix: &Matrix4<f32>, lines: &[Line]) -> DrawSet {
let mut draws = vec![];
for lines in lines.chunks(1000) {
let mut attribs = vec![];
attribs.reserve(2 * lines.len());
let mut attribs = Vec::with_capacity(2 * lines.len());
for line in lines {
for position in [line.a, line.b] {
let p = position.coords.cast();
Expand Down
3 changes: 1 addition & 2 deletions frontend/renderer/src/ship_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ void main() {
let vertices_token = self.buffer_arena.write(&vertices);
let num_vertices = vertices.len();

let mut attribs: Vec<Attribs> = vec![];
attribs.reserve(ships.len());
let mut attribs: Vec<Attribs> = Vec::with_capacity(ships.len());
for ship in ships.iter() {
let p = ship.position.coords.cast::<f32>();
let shielded = ship.active_abilities.contains(&oort_api::Ability::Shield);
Expand Down
3 changes: 1 addition & 2 deletions frontend/renderer/src/text_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ void main() {
let font_pixel_width = 1.0 / (FONT_COLS * FONT_GLYPH_SIZE) as f32;
let font_pixel_height = 1.0 / (FONT_ROWS * FONT_GLYPH_SIZE) as f32;

let mut attribs = vec![];
attribs.reserve(num_glyphs);
let mut attribs = Vec::with_capacity(num_glyphs);
for text in texts {
let worldpos = vector![text.x as f32, text.y as f32];
let projected =
Expand Down
3 changes: 1 addition & 2 deletions frontend/renderer/src/trail_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ void main() {
}

pub fn update(&mut self, snapshot: &Snapshot) {
let mut data = vec![];
data.reserve(snapshot.ships.len() * 2 * FLOATS_PER_VERTEX as usize);
let mut data = Vec::with_capacity(snapshot.ships.len() * 2 * FLOATS_PER_VERTEX as usize);
let mut n = 0;
let creation_time = snapshot.time as f32;
for ship in snapshot.ships.iter() {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.73.0"
channel = "1.77.2"
targets = ["wasm32-unknown-unknown"]
62 changes: 35 additions & 27 deletions shared/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,23 +372,23 @@ pub type Message = [f64; 4];
// Public for fuzzer.
#[doc(hidden)]
pub mod sys {
use crate::MAX_ENVIRONMENT_SIZE;

use super::SystemState;
use crate::MAX_ENVIRONMENT_SIZE;
use std::ptr;

// TODO crashes rust-analyzer
#[no_mangle]
pub static mut SYSTEM_STATE: [u64; SystemState::MaxSize as usize] =
[0; SystemState::MaxSize as usize];

pub fn read_system_state_u64(index: SystemState) -> u64 {
let system_state = unsafe { &SYSTEM_STATE };
system_state[index as usize]
let system_state = unsafe { ptr::addr_of!(SYSTEM_STATE) };
unsafe { (*system_state)[index as usize] }
}

pub fn write_system_state_u64(index: SystemState, value: u64) {
let system_state = unsafe { &mut SYSTEM_STATE };
system_state[index as usize] = value;
let system_state = unsafe { ptr::addr_of_mut!(SYSTEM_STATE) };
unsafe { (*system_state)[index as usize] = value };
}

pub fn read_system_state(index: SystemState) -> f64 {
Expand All @@ -404,12 +404,15 @@ pub mod sys {

pub fn read_environment() -> &'static str {
// Format is key=value\nkey=value\n... ending with a null byte.
let environment = unsafe { &ENVIRONMENT };
let n = environment
.iter()
.position(|&c| c == 0)
.unwrap_or(environment.len());
std::str::from_utf8(&environment[..n]).expect("Failed to convert environment to string")
unsafe {
let environment = ptr::addr_of!(ENVIRONMENT);
let n = (*environment)
.iter()
.position(|&c| c == 0)
.unwrap_or((*environment).len());
std::str::from_utf8(&(*environment)[..n])
.expect("Failed to convert environment to string")
}
}

pub fn getenv(key: &str) -> Option<&'static str> {
Expand Down Expand Up @@ -945,6 +948,7 @@ pub mod dbg {
use crate::sys::write_system_state;
use crate::vec::*;
use std::f64::consts::TAU;
use std::ptr;

static mut TEXT_BUFFER: String = String::new();
static mut LINE_BUFFER: Vec<Line> = Vec::new();
Expand All @@ -964,9 +968,11 @@ pub mod dbg {
#[doc(hidden)]
pub fn write(args: std::fmt::Arguments) {
use std::fmt::Write;
let buf = unsafe { &mut TEXT_BUFFER };
let _ = std::fmt::write(buf, args);
buf.push('\n');
unsafe {
let buf = ptr::addr_of_mut!(TEXT_BUFFER);
let _ = std::fmt::write(&mut *buf, args);
(*buf).push('\n');
}
}

/// Creates a 24-bit RGB color from the arguments.
Expand All @@ -985,14 +991,16 @@ pub mod dbg {
/// Up to 1024 lines can be drawn per ship, per tick. This quota is also consumed
/// by the various shape drawing functions.
pub fn draw_line(a: Vec2, b: Vec2, color: u32) {
let buf = unsafe { &mut LINE_BUFFER };
buf.push(Line {
x0: a.x,
y0: a.y,
x1: b.x,
y1: b.y,
color,
});
unsafe {
let buf = ptr::addr_of_mut!(LINE_BUFFER);
(*buf).push(Line {
x0: a.x,
y0: a.y,
x1: b.x,
y1: b.y,
color,
});
}
}

#[deprecated]
Expand Down Expand Up @@ -1102,7 +1110,7 @@ pub mod dbg {
use std::fmt::Write;
let mut text = String::new();
let _ = std::fmt::write(&mut text, args);
let buf = unsafe { &mut DRAWN_TEXT_BUFFER };
let buf = unsafe { &mut *ptr::addr_of_mut!(DRAWN_TEXT_BUFFER) };
// TODO handle longer text
let mut text_buf = [0u8; 11];
text_buf
Expand All @@ -1121,7 +1129,7 @@ pub mod dbg {
#[doc(hidden)]
pub fn update() {
{
let slice = unsafe { &mut TEXT_BUFFER }.as_bytes();
let slice = unsafe { &mut *ptr::addr_of_mut!(TEXT_BUFFER) }.as_bytes();
write_system_state(
super::SystemState::DebugTextPointer,
slice.as_ptr() as u32 as f64,
Expand All @@ -1132,7 +1140,7 @@ pub mod dbg {
);
}
{
let slice = unsafe { &mut LINE_BUFFER }.as_slice();
let slice = unsafe { &mut *ptr::addr_of_mut!(LINE_BUFFER) }.as_slice();
write_system_state(
super::SystemState::DebugLinesPointer,
slice.as_ptr() as u32 as f64,
Expand All @@ -1143,7 +1151,7 @@ pub mod dbg {
);
}
{
let slice = unsafe { &mut DRAWN_TEXT_BUFFER }.as_slice();
let slice = unsafe { &mut *ptr::addr_of_mut!(DRAWN_TEXT_BUFFER) }.as_slice();
write_system_state(
super::SystemState::DrawnTextPointer,
slice.as_ptr() as u32 as f64,
Expand Down
2 changes: 1 addition & 1 deletion shared/multifile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub struct Ship {}
let mut files = std::collections::HashMap::new();
let reference = include_str!("../../builtin_ai/src/reference.rs");
//let reference = "fn baz() {}\nfn boo() {}\n";
let lib = format!("mod foo;\nmod bar;\n");
let lib = "mod foo;\nmod bar;\n".to_string();
files.insert("lib.rs".to_string(), lib.clone());
files.insert("foo.rs".to_string(), reference.to_string());
files.insert("bar.rs".to_string(), reference.to_string());
Expand Down
2 changes: 1 addition & 1 deletion shared/simulator/benches/tutorials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn tutorials() {
.1;
assert!(!scenario_names.is_empty());
for scenario_name in scenario_names {
check_solution(&scenario_name);
check_solution(scenario_name);
}
}

Expand Down
3 changes: 1 addition & 2 deletions shared/simulator/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ pub struct Line {
}

pub fn emit_ship(sim: &mut Simulation, handle: ShipHandle) {
let mut lines = vec![];
lines.reserve(2 + sim.ship(handle).data().guns.len());
let mut lines = Vec::with_capacity(2 + sim.ship(handle).data().guns.len());
let body = sim.ship(handle).body();
let p = body.position().translation.vector.into();
lines.push(Line {
Expand Down
33 changes: 16 additions & 17 deletions shared/simulator/src/radar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,7 @@ fn check_planet_contact(

fn draw_emitter(sim: &mut Simulation, emitter: &RadarEmitter, reliable_distance: f64) {
let color = vector![0.2, 0.66, 0.97, 1.0];
let mut lines = vec![];
lines.reserve(48);
let mut lines = Vec::with_capacity(48);
let w = emitter.end_bearing - emitter.start_bearing;
let center = emitter.center;
let mut draw_arc = |r| {
Expand Down Expand Up @@ -782,35 +781,35 @@ mod test {
ship::target(1),
);
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());

// Explicit heading and width.
sim.ship_mut(ship0).radar_mut().unwrap().heading = 0.0;
sim.ship_mut(ship0).radar_mut().unwrap().width = TAU / 6.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());

// Just outside of sector (clockwise).
sim.ship_mut(ship0).radar_mut().unwrap().heading = TAU / 12.0 + EPSILON;
sim.ship_mut(ship0).radar_mut().unwrap().width = TAU / 6.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), false);
assert!(sim.ship(ship0).radar().unwrap().result.is_none());

// Just inside of sector (clockwise).
sim.ship_mut(ship0).radar_mut().unwrap().heading -= 2.0 * EPSILON;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());

// Just outside of sector (counter-clockwise).
sim.ship_mut(ship0).radar_mut().unwrap().heading = -TAU / 12.0 - EPSILON;
sim.ship_mut(ship0).radar_mut().unwrap().width = TAU / 6.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), false);
assert!(sim.ship(ship0).radar().unwrap().result.is_none());

// Just inside of sector (counter-clockwise).
sim.ship_mut(ship0).radar_mut().unwrap().heading += 2.0 * EPSILON;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());

// Out of range.
sim.ship_mut(ship0).radar_mut().unwrap().heading = 0.0;
Expand All @@ -819,7 +818,7 @@ mod test {
.body()
.set_translation(vector![1e6, 0.0], true);
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), false);
assert!(sim.ship(ship0).radar().unwrap().result.is_none());
}

#[test]
Expand Down Expand Up @@ -848,17 +847,17 @@ mod test {
// Pointing at center of target
sim.ship_mut(ship0).radar_mut().unwrap().width = TAU / 6.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());

// Pointing at target but not at center
sim.ship_mut(ship0).radar_mut().unwrap().heading = 0.001 + TAU / 12.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());

// Not pointing at target
sim.ship_mut(ship0).radar_mut().unwrap().heading = TAU / 4.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), false);
assert!(sim.ship(ship0).radar().unwrap().result.is_none());
}

#[test]
Expand Down Expand Up @@ -886,7 +885,7 @@ mod test {

sim.ship_mut(ship0).radar_mut().unwrap().width = TAU / 3600.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), false);
assert!(sim.ship(ship0).radar().unwrap().result.is_none());
}

#[test]
Expand All @@ -909,22 +908,22 @@ mod test {
ship::target(1),
);
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());

sim.ship_mut(ship0).radar_mut().unwrap().min_distance = 900.0;
sim.ship_mut(ship0).radar_mut().unwrap().max_distance = 1100.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());

sim.ship_mut(ship0).radar_mut().unwrap().min_distance = 1050.0;
sim.ship_mut(ship0).radar_mut().unwrap().max_distance = 1100.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), false);
assert!(sim.ship(ship0).radar().unwrap().result.is_none());

sim.ship_mut(ship0).radar_mut().unwrap().min_distance = 985.0;
sim.ship_mut(ship0).radar_mut().unwrap().max_distance = 995.0;
sim.step();
assert_eq!(sim.ship(ship0).radar().unwrap().result.is_some(), true);
assert!(sim.ship(ship0).radar().unwrap().result.is_some());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion shared/simulator/src/ship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ mod test {
);

for _ in 0..10 {
sim.ship_mut(ship0).torque(6.28);
sim.ship_mut(ship0).torque(std::f64::consts::TAU);
sim.step();
let dist = sim.ship(ship0).position().vector.magnitude();
assert!(
Expand Down
2 changes: 1 addition & 1 deletion shared/simulator/tests/api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn test_id() {
vector![0.0, 0.0],
vector![0.0, 0.0],
0.0,
fighter(team as i32),
fighter(team),
)
})
.collect::<Vec<_>>();
Expand Down
4 changes: 2 additions & 2 deletions shared/simulator/tests/bullet_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ fn test_penetration() {

sim.ship_mut(ship0).fire_gun(0);
let bullet = *sim.bullets.iter().next().unwrap();
let initial_bullet_mass = bullet::data(&mut sim, bullet).mass;
let initial_velocity = *bullet::body(&mut sim, bullet).linvel();
let initial_bullet_mass = bullet::data(&sim, bullet).mass;
let initial_velocity = *bullet::body(&sim, bullet).linvel();

for _ in 0..100 {
sim.step();
Expand Down
2 changes: 1 addition & 1 deletion shared/simulator/tests/collision_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn test_missile_bullet_collision_same_team() {
}

assert_eq!(sim.ship(msl).velocity().x, 0.0);
assert_eq!(bullet::body(&mut sim, blt).linvel().x, 1000.0);
assert_eq!(bullet::body(&sim, blt).linvel().x, 1000.0);
}

#[test]
Expand Down
Loading

0 comments on commit 9e4e31b

Please sign in to comment.