Trait Format

Source
pub trait Format: Display {
    // Required methods
    fn rule(&self) -> Rule;
    fn live_cells(&self) -> Box<dyn Iterator<Item = Position<usize>> + '_>;
}
Expand description

Provides several methods for Conway’s Game of Life pattern file formats.

§Examples

use std::fs::File;
use life_backend::{Format, Rule};
use life_backend::format::Plaintext;
let file = File::open("patterns/rpentomino.cells")?;
let handler: Box<dyn Format> = Box::new(Plaintext::new(file)?);
assert_eq!(handler.rule(), Rule::conways_life());
assert_eq!(handler.live_cells().count(), 5);

Required Methods§

Source

fn rule(&self) -> Rule

Returns the rule.

§Examples
use life_backend::{Format, Rule};
use life_backend::format::Rle;
let pattern = "\
    #N T-tetromino\n\
    x = 3, y = 2, rule = B3/S23\n\
    3o$bo!\n\
";
let handler: Box<dyn Format> = Box::new(pattern.parse::<Rle>()?);
assert_eq!(handler.rule(), Rule::conways_life());
Source

fn live_cells(&self) -> Box<dyn Iterator<Item = Position<usize>> + '_>

Creates an owning iterator over the series of live cell positions in ascending order.

§Examples
use life_backend::{Format, Position, Rule};
use life_backend::format::Rle;
let pattern = "\
    #N T-tetromino\n\
    x = 3, y = 2, rule = B3/S23\n\
    3o$bo!\n\
";
let handler: Box<dyn Format> = Box::new(pattern.parse::<Rle>()?);
assert!(handler.live_cells().eq([Position(0, 0), Position(1, 0), Position(2, 0), Position(1, 1)]));

Implementors§