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::{Bullet, Invaders, Laser, Player, Shield, COLS, GRID, ROWS};
use std::collections::HashSet;
use alloc::collections::BTreeSet;
/// Store information about collisions (for debug mode).
#[derive(Debug, Default)]
pub(crate) struct Collision {
pub(crate) bullet_details: HashSet<BulletDetail>,
pub(crate) laser_details: HashSet<LaserDetail>,
pub(crate) bullet_details: BTreeSet<BulletDetail>,
pub(crate) laser_details: BTreeSet<LaserDetail>,
}
/// 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 {
/// A grid position (col, row) for an invader.
Invader(usize, usize),
@ -23,7 +23,7 @@ pub(crate) enum BulletDetail {
}
/// 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 {
/// A shield index.
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;
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;
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
//! to understand the code.
#![no_std]
#![deny(clippy::all)]
#![forbid(unsafe_code)]
extern crate alloc;
use alloc::vec::Vec;
use crate::collision::Collision;
pub use crate::controls::{Controls, Direction};
use crate::geo::Point;
use crate::loader::{load_assets, Assets};
use crate::sprites::{blit, Animation, Drawable, Frame, Sprite, SpriteRef};
use core::time::Duration;
use randomize::PCG32;
use std::time::Duration;
mod collision;
mod controls;

View file

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

View file

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