Skip to content

Commit

Permalink
flx clippy hints
Browse files Browse the repository at this point in the history
  • Loading branch information
dalton-oliveira committed Dec 13, 2023
1 parent 86b01d3 commit 111cb10
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 110 deletions.
24 changes: 10 additions & 14 deletions core/src/food.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,26 @@ pub struct FoodField {
pub count: u16,
bag: [FoodType; BAG.len()],
}

impl FoodField {
pub fn new() -> FoodField {
impl Default for FoodField {
fn default() -> Self {
FoodField {
foods: Vec::new(),
count: 0,
minimum: 0,
bag: BAG.clone(),
bag: BAG,
}
}
}
impl FoodField {
pub fn total_filled(&self) -> u16 {
let mut total: u16 = 0;
for f in self.foods.iter() {
total += f.size as u16;
}
return total;
total
}
pub fn has_at(&self, p: &FieldPoint) -> Option<usize> {
for i in 0..self.foods.len() {
if self.foods[i].is_at(p) {
return Some(i);
}
}
return None;
(0..self.foods.len()).find(|&i| self.foods[i].is_at(p))
}

pub fn grab(&mut self, p: &FieldPoint) -> Option<Food> {
Expand All @@ -50,7 +46,7 @@ impl FoodField {
let food = self.foods.remove(i);
return Some(food);
}
return None;
None
}

pub fn set_food(&mut self, food: Food) {
Expand Down Expand Up @@ -139,7 +135,7 @@ impl FoodField {

self.bag.shuffle(&mut rng);
let food = Food::new(self.bag[0], field.from_idx(idx));
return Some(food);
Some(food)
}

pub fn tick(&mut self) {
Expand All @@ -150,7 +146,7 @@ impl FoodField {
if food.ticks_left > 1 {
food.ticks_left -= 1;
}
return true;
true
});
}
}
14 changes: 7 additions & 7 deletions core/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ impl Game {
pub fn new(config: GameConfig) -> Game {
let (width, height) = config.dim;
let field = Field::new(width, height);
let game = Game {

Game {
config,
food: FoodField::new(),
food: FoodField::default(),
snakes: HashMap::new(),
field,
state: GameState::None,
};
return game;
}
}

pub fn encode_game_data(&self) -> Vec<u8> {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Game {
let id = (self.snakes.len() + 1) as u16;
let snake = Snake::new(&mut self.field, &config, id);
self.snakes.insert(id, snake);
return id;
id
}

pub fn remove_snake(&mut self, snake_id: u16) {
Expand Down Expand Up @@ -133,10 +133,10 @@ impl Game {
pub fn draw(&mut self, render: &mut impl GameRender) {
// @todo use snake_id as render param
for (_id, snake) in self.snakes.iter() {
render.snake(&snake, &self.food);
render.snake(snake, &self.food);
}
for food in self.food.foods.iter() {
render.food(&food);
render.food(food);
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ impl Snake {
};

snake.egg_hatch(field, start, config.size);
return snake;
snake
}

pub fn head_to(&mut self, to: Direction) -> bool {
if self.should_ignore_turn(to) {
return false;
}
self.direction.to = to;
return true;
true
}

pub fn next_head(&self) -> SnakeNode {
Expand All @@ -57,7 +57,7 @@ impl Snake {

pub fn should_ignore_turn(&self, to: Direction) -> bool {
let direction = self.nodes.back().unwrap().direction;
return opposite_of(direction) == to || direction == to;
opposite_of(direction) == to || direction == to
}

pub fn egg_hatch(&mut self, field: &mut Field, location: FieldPoint, size: u16) {
Expand Down
26 changes: 13 additions & 13 deletions core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ pub struct Food {

impl Food {
pub fn new(shape: FoodType, p: FieldPoint) -> Food {
return match shape {
match shape {
FoodType::Basic => Food {
ticks_left: 0,
size: 1,
weight: 8,
shape,
location: p.clone(),
location: p,
},
_ => Food {
ticks_left: 30,
size: 2,
weight: 45,
shape,
location: p.clone(),
location: p,
},
};
}
}
// not generic but does the job
pub fn is_at(&self, p: &FieldPoint) -> bool {
return match self.shape {
FoodType::Basic => self.location.eq(&p),
_ => self.location.eq(&p) || (self.location.add_tuple((1, 0))).eq(&p),
};
match self.shape {
FoodType::Basic => self.location.eq(p),
_ => self.location.eq(p) || (self.location.add_tuple((1, 0))).eq(p),
}
}
}

Expand All @@ -65,14 +65,14 @@ pub struct Field {
impl Field {
pub fn new(width: u16, height: u16) -> Field {
let bit_set = FixedBitSet::with_capacity(width as usize * height as usize);
return Field {
Field {
width,
height,
bit_set,
};
}
}
fn to_idx(&self, p: &FieldPoint) -> u16 {
((p.y * self.width) + p.x) as u16
(p.y * self.width) + p.x
}
pub fn from_idx(&self, idx: u16) -> FieldPoint {
FieldPoint {
Expand Down Expand Up @@ -123,9 +123,9 @@ impl FieldPoint {
FieldPoint { x, y }
}
pub fn wrapping_sub(&self, direction: WrappableDirection) -> FieldPoint {
let mut direction = direction.clone();
let mut direction = direction;
direction.to = opposite_of(direction.to);
return self.wrapping_add(direction);
self.wrapping_add(direction)
}
}

Expand Down
2 changes: 1 addition & 1 deletion snake-termion/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ fn main() {
handle.join().unwrap();
}

let mut render = TermionRender::new();
let mut render = TermionRender::default();
render.show_cursor();
}
35 changes: 18 additions & 17 deletions snake-termion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl GameRender for TermionRender {
let mut node = head;
for _i in 1..nodes.len() - 1 {
node = iter.next_back().unwrap();
write("*", &node, &mut self.screen);
write("*", node, &mut self.screen);
}
let tail = iter.next_back().unwrap();
self.replace_tail(tail, node.direction);
Expand All @@ -52,18 +52,19 @@ impl GameRender for TermionRender {
// coming soon...
}
}

impl TermionRender {
pub fn new() -> TermionRender {
impl Default for TermionRender {
fn default() -> Self {
let stdout = stdout()
.into_raw_mode()
.unwrap()
.into_alternate_screen()
.unwrap();
let mut screen = AlternateScreen::from(stdout);
let mut screen = stdout;
write!(screen, "{}{}", termion::cursor::Hide, ToAlternateScreen).unwrap();
TermionRender { screen, tail: None }
}
}
impl TermionRender {
pub fn clear(&mut self) {
write!(self.screen, "{}", termion::clear::All).unwrap();
}
Expand All @@ -80,28 +81,28 @@ impl TermionRender {
}
}
fn snake_tail(node: &SnakeNode) -> &str {
return match node.direction {
match node.direction {
Direction::Up | Direction::Down => "Ꞌ",
Direction::Right | Direction::Left => "-",
};
}
}

fn snake_mounth_treat(node: &SnakeNode) -> &str {
return match node.direction {
match node.direction {
Direction::Up => "v",
Direction::Down => "ʌ",
Direction::Right => "<",
Direction::Left => ">",
};
}
}

fn snake_mounth(node: &SnakeNode) -> &str {
return match node.direction {
match node.direction {
Direction::Up => "⩀",
Direction::Down => "⨃",
Direction::Right => "⪾",
Direction::Left => "⪽",
};
}
}
fn replace_tail(&mut self, tail: &SnakeNode, direction: Direction) {
self.clear_tail();
Expand All @@ -112,17 +113,17 @@ impl TermionRender {
stuffed: false,
};

self.save_tail(tail.clone());
self.save_tail(tail);

write(TermionRender::snake_tail(&tail), &tail, &mut self.screen);
}

fn replace_head(&mut self, head: &SnakeNode, mouth_open: bool) {
let sprite = match mouth_open {
true => TermionRender::snake_mounth_treat(&head),
false => TermionRender::snake_mounth(&head),
true => TermionRender::snake_mounth_treat(head),
false => TermionRender::snake_mounth(head),
};
write(sprite, &head, &mut self.screen);
write(sprite, head, &mut self.screen);
}
}

Expand All @@ -134,8 +135,8 @@ pub fn write(c: &str, node: &SnakeNode, screen: &mut AlternateScreen<RawTerminal
}

pub fn write_point(c: &str, point: &FieldPoint, screen: &mut AlternateScreen<RawTerminal<Stdout>>) {
let x = point.x as u16;
let y = point.y as u16;
let x = point.x;
let y = point.y;
write!(
screen,
"{}{}",
Expand Down
2 changes: 1 addition & 1 deletion snake-termion/src/ticker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::render::TermionRender;

pub fn run(game: Arc<RwLock<Game>>) {
RwLock::write(&game).expect("can't add food").add_food();
let mut render = TermionRender::new();
let mut render = TermionRender::default();
loop {
let now = Instant::now();

Expand Down
4 changes: 2 additions & 2 deletions snake-web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use tracing_subscriber::{self};
#[folder = "www/"]
struct Assets;

static GAME: Lazy<WsGame> = Lazy::new(|| WsGame::new());
static GAME: Lazy<WsGame> = Lazy::new(WsGame::default);
const PORT_BIND: &str = "80";

fn init_tracer() -> Tracer {
Expand All @@ -39,7 +39,7 @@ fn init_tracer() -> Tracer {
.with(opentelemetry)
.init();

return tracer;
tracer
}

#[tokio::main]
Expand Down
Loading

0 comments on commit 111cb10

Please sign in to comment.