Make Rust standard library optional (WIP) (#342)

* Remove `std` dependency.

* Format code with `cargo fmt`.
This commit is contained in:
Aggelos Tselios 2023-03-17 00:13:36 +02:00 committed by GitHub
parent 899e27bafa
commit 295139a412
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 19 deletions

View file

@ -2,17 +2,17 @@
use crate::geo::{Point, Rect}; use crate::geo::{Point, Rect};
use crate::{Bullet, Invaders, Laser, Player, Shield, COLS, GRID, ROWS}; use crate::{Bullet, Invaders, Laser, Player, Shield, COLS, GRID, ROWS};
use std::collections::HashSet; use alloc::collections::BTreeSet;
/// Store information about collisions (for debug mode). /// Store information about collisions (for debug mode).
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub(crate) struct Collision { pub(crate) struct Collision {
pub(crate) bullet_details: HashSet<BulletDetail>, pub(crate) bullet_details: BTreeSet<BulletDetail>,
pub(crate) laser_details: HashSet<LaserDetail>, pub(crate) laser_details: BTreeSet<LaserDetail>,
} }
/// Information regarding collisions between bullets and invaders, lasers, or shields. /// Information regarding collisions between bullets and invaders, lasers, or shields.
#[derive(Debug, Eq, Hash, PartialEq)] #[derive(Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub(crate) enum BulletDetail { pub(crate) enum BulletDetail {
/// A grid position (col, row) for an invader. /// A grid position (col, row) for an invader.
Invader(usize, usize), Invader(usize, usize),
@ -23,7 +23,7 @@ pub(crate) enum BulletDetail {
} }
/// Information regarding collisions between lasers and shields or the player. /// Information regarding collisions between lasers and shields or the player.
#[derive(Debug, Eq, Hash, PartialEq)] #[derive(Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub(crate) enum LaserDetail { pub(crate) enum LaserDetail {
/// A shield index. /// A shield index.
Shield(usize), Shield(usize),

View file

@ -23,7 +23,7 @@ impl Point {
} }
} }
impl std::ops::Add for Point { impl core::ops::Add for Point {
type Output = Self; type Output = Self;
fn add(self, other: Self) -> Self { fn add(self, other: Self) -> Self {
@ -31,7 +31,7 @@ impl std::ops::Add for Point {
} }
} }
impl std::ops::Mul for Point { impl core::ops::Mul for Point {
type Output = Self; type Output = Self;
fn mul(self, other: Self) -> Self { fn mul(self, other: Self) -> Self {

View file

@ -4,16 +4,20 @@
//! this in practice. That said, the game is fully functional, and it should not be too difficult //! this in practice. That said, the game is fully functional, and it should not be too difficult
//! to understand the code. //! to understand the code.
#![no_std]
#![deny(clippy::all)] #![deny(clippy::all)]
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
extern crate alloc;
use alloc::vec::Vec;
use crate::collision::Collision; use crate::collision::Collision;
pub use crate::controls::{Controls, Direction}; pub use crate::controls::{Controls, Direction};
use crate::geo::Point; use crate::geo::Point;
use crate::loader::{load_assets, Assets}; use crate::loader::{load_assets, Assets};
use crate::sprites::{blit, Animation, Drawable, Frame, Sprite, SpriteRef}; use crate::sprites::{blit, Animation, Drawable, Frame, Sprite, SpriteRef};
use core::time::Duration;
use randomize::PCG32; use randomize::PCG32;
use std::time::Duration;
mod collision; mod collision;
mod controls; mod controls;

View file

@ -1,6 +1,7 @@
use std::collections::HashMap; use alloc::collections::BTreeMap;
use std::io::Cursor; use alloc::rc::Rc;
use std::rc::Rc;
use alloc::vec::Vec;
use crate::sprites::{CachedSprite, Frame}; use crate::sprites::{CachedSprite, Frame};
@ -8,11 +9,11 @@ use crate::sprites::{CachedSprite, Frame};
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Assets { pub(crate) struct Assets {
// sounds: TODO // sounds: TODO
sprites: HashMap<Frame, CachedSprite>, sprites: BTreeMap<Frame, CachedSprite>,
} }
impl Assets { impl Assets {
pub(crate) fn sprites(&self) -> &HashMap<Frame, CachedSprite> { pub(crate) fn sprites(&self) -> &BTreeMap<Frame, CachedSprite> {
&self.sprites &self.sprites
} }
} }
@ -21,7 +22,7 @@ impl Assets {
pub(crate) fn load_assets() -> Assets { pub(crate) fn load_assets() -> Assets {
use Frame::*; use Frame::*;
let mut sprites = HashMap::new(); let mut sprites = BTreeMap::new();
sprites.insert(Blipjoy1, load_pcx(include_bytes!("assets/blipjoy1.pcx"))); sprites.insert(Blipjoy1, load_pcx(include_bytes!("assets/blipjoy1.pcx")));
sprites.insert(Blipjoy2, load_pcx(include_bytes!("assets/blipjoy2.pcx"))); sprites.insert(Blipjoy2, load_pcx(include_bytes!("assets/blipjoy2.pcx")));
@ -57,7 +58,7 @@ pub(crate) fn load_assets() -> Assets {
/// Convert PCX data to raw pixels /// Convert PCX data to raw pixels
fn load_pcx(pcx: &[u8]) -> CachedSprite { fn load_pcx(pcx: &[u8]) -> CachedSprite {
let mut reader = pcx::Reader::new(Cursor::new(pcx)).unwrap(); let mut reader = pcx::Reader::new(pcx).unwrap();
let width = reader.width() as usize; let width = reader.width() as usize;
let height = reader.height() as usize; let height = reader.height() as usize;
let mut result = Vec::new(); let mut result = Vec::new();
@ -113,6 +114,8 @@ fn load_pcx(pcx: &[u8]) -> CachedSprite {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use alloc::vec;
use super::*; use super::*;
#[test] #[test]

View file

@ -1,16 +1,17 @@
use crate::loader::Assets; use crate::loader::Assets;
use crate::TIME_STEP; use crate::TIME_STEP;
use crate::{Point, HEIGHT, WIDTH}; use crate::{Point, HEIGHT, WIDTH};
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::cmp::min;
use core::time::Duration;
use line_drawing::Bresenham; use line_drawing::Bresenham;
use std::cmp::min;
use std::rc::Rc;
use std::time::Duration;
// This is the type stored in the `Assets` hash map // This is the type stored in the `Assets` hash map
pub(crate) type CachedSprite = (usize, usize, Rc<[u8]>); pub(crate) type CachedSprite = (usize, usize, Rc<[u8]>);
/// Frame identifier for managing animations. /// Frame identifier for managing animations.
#[derive(Debug, Eq, Hash, PartialEq)] #[derive(Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub(crate) enum Frame { pub(crate) enum Frame {
Blipjoy1, Blipjoy1,
Blipjoy2, Blipjoy2,