Unverified Commit 60643806 authored by Fuzen's avatar Fuzen 🍵

Remove no_std

Remove no_std from stable since there was no clean method of implementing
no_std rand needed a way to globally set a seed and mutate, which isn't a safe
operation nor optimal. SmallRNG was added as a replacement rng for those who
do not want a resource heavy RNG
parent bc44cd64
Pipeline #71994086 passed with stages
in 9 minutes and 24 seconds
......@@ -41,36 +41,36 @@ bench-test:
paths:
- target/
build-no-std:
build-small_rng:
stage: build
script:
- rustc --version
- cargo --version
- cargo update
- cargo build --no-default-features
- cargo build --features="small_rng"
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
- target/
test-no-std:
test-small_rng:
stage: test
script:
- rustc --version
- cargo --version
- cargo test --no-default-features
- cargo test --features="small_rng"
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
- target/
policy: pull
bench-test-no-std:
bench-test-small_rng:
stage: test
script:
- rustc --version
- cargo --version
- cargo bench --no-default-features -- --test
- cargo bench --features="small_rng" -- --test
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
......
......@@ -20,9 +20,9 @@ name = "games"
# crate-type = ["rlib", "dylib", "staticlib"]
[dependencies]
failure= {version ="0.1", default-features=false, features=["derive"]}
rand = {version= "0.7", default-features=false, features=["small_rng"]}
serde = { version = "1.0", default-features=false, features = ["derive"] }
failure= {version ="0.1", features=["derive"]}
rand = "0.7"
serde = { version = "1.0", features = ["derive"] }
[[bench]]
......@@ -30,8 +30,8 @@ name = "benchmark"
harness = false
[features]
default = ["std"]
std = ["serde/std", "failure/std", "rand/std"]
default = []
small_rng = ["rand/small_rng"]
[package.metadata.docs.rs]
all-features = true
use games::blackjack::*;
fn main() {
let mut bj = BlackJack::default(); // Only use 1 deck, use `BlackJack::new_game(5)` for 5 decks
println!("{}",bj.display_game());
let _ = bj.player_hit().is_ok(); // Make a move
// Game can be frozen with `bj.freeze()`;
// And can be defrosted with `BlackJack::defrost`
println!("{}",bj.display_game());
match bj.finish() {
GameState::PlayerWon => println!("You win!"),
GameState::PlayerLost => println!("You lose!"),
_ => unreachable!(), // Only PlayerWon / PlayerLost is returned
}
}
......@@ -3,9 +3,6 @@ use crate::blackjack::hand::Hand;
use crate::cards::StandardCard as Card;
use crate::deck::{Deck, DefaultCollection};
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
//use failure::Error;
/// A struct to represent the games state
......
use crate::cards::StandardCard as Card;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
/// Used to manage BlackJack hands
#[repr(C)]
......
......@@ -26,11 +26,6 @@ use crate::color::Color;
use core::fmt::{self, Display};
use core::str::FromStr;
#[cfg(not(feature = "std"))]
use alloc::{
format,
string::{String, ToString},
};
use crate::errors::{ErrorCode, BASE_CARD_ERROR_CODE};
......
......@@ -27,15 +27,7 @@
use crate::cards::StandardCard;
use crate::deck_of_cards::STANDARD_DECK_OF_CARDS;
use crate::errors::{ErrorCode, BASE_DECK_ERROR_CODE};
#[cfg(not(feature = "std"))]
use alloc::{
collections::vec_deque::{IntoIter as DequeIntoIter, VecDeque},
vec::Vec,
};
#[cfg(not(feature = "std"))]
use core::fmt::{Debug, Display};
use rand::Rng;
#[cfg(feature = "std")]
use std::{
collections::vec_deque::IntoIter as DequeIntoIter,
collections::VecDeque,
......
//! Errors for Games-rs
#[cfg(not(feature = "std"))]
use alloc::{format, string::String};
#[cfg(not(feature = "std"))]
use core::fmt;
#[cfg(feature = "std")]
use std::fmt;
/// Base Error code for Card errors (1XXX)
......
......@@ -16,9 +16,6 @@
while_true
)]
#![warn(unreachable_pub, variant_size_differences)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
extern crate alloc;
pub mod blackjack;
pub mod cards;
......@@ -33,39 +30,21 @@ pub mod slot_machine;
pub mod solitaire; // TODO: Improve performance
#[cfg(not(feature = "std"))]
static mut SEED: [u8; 16] = [
70, 97, 99, 116, 58, 32, 84, 101, 97, 62, 67, 111, 102, 102, 101, 101,
];
#[cfg(feature="small_rng")]
use rand::SeedableRng;
/// [no-std] Set global seed
///
///
/// # Saftey
/// This function uses unsafe code to set a `statuc mut [u8;16]`.
/// This shouldn't cause any problems since I only update a specific index.
/// Do not that running this function in a seperate thread may lead to a unexpected seed
/// although I do not believe this to be a problem for a random number generator.
///
#[cfg(not(feature = "std"))]
pub fn set_seed(s: &'static str) {
let mut iter = s.as_bytes().iter().cycle().take(32).copied();
for i in 0..16 {
unsafe {
SEED[i] = iter.next().unwrap_or_default();
}
}
}
#[cfg(feature = "std")]
#[cfg(not(feature="small_rng"))]
pub(crate) fn get_rng() -> rand::rngs::ThreadRng {
rand::thread_rng()
}
#[cfg(not(feature = "std"))]
pub(crate) fn get_rng() -> rand::rngs::SmallRng {
unsafe { rand::SeedableRng::from_seed(SEED) }
#[cfg(feature="small_rng")]
pub(crate) fn get_rng() -> rand::rngs::SmallRng
{
// Once per thread
let mut thread_rng = rand::thread_rng();
rand::rngs::SmallRng::from_rng(&mut thread_rng).expect("Assume this won't fail.")
}
// TODO: Cleanup codebase
// TODO: each game a feature
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
#[cfg(not(feature = "std"))]
use core::{
cmp::{Ord, Ordering, PartialOrd},
fmt,
str::FromStr,
};
use rand::Rng;
#[cfg(feature = "std")]
use std::{
cmp::{Ord, Ordering, PartialOrd},
fmt,
......
......@@ -2,8 +2,6 @@ use crate::solitaire::{
errors::{SError, SolitaireError},
SolitaireCard,
};
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
/// Area of cards
#[repr(C)]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment