Skip to content

Instantly share code, notes, and snippets.

@drbawb
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbawb/9114052 to your computer and use it in GitHub Desktop.
Save drbawb/9114052 to your computer and use it in GitHub Desktop.
pub mod units;
/// A main() outside the module does not work, #to_pixel() not in scope?
fn main() {
let mut gs: units::Game = units::Game(640.0) * units::Game(480.0);
println!("gs->pix, outside mod: {:?}", gs.to_pixel());
}
use std::f64;
static TILE_SIZE: i32 = 32;
pub struct Game(f64);
pub struct Pixel(i32);
pub struct Tile(uint);
pub struct Frame(uint);
pub trait AsGame {fn to_game(&self) -> Game;}
pub trait AsTile {fn to_tile(&self) -> Tile;}
pub trait AsPixel {fn to_pixel(&self) -> Pixel;}
/// This main() works fine, to_pixel() is in scope.
fn main() {
let mut gs = Game(320.0) * Game(240.0);
println!("gs to pix: {:?}", gs.to_pixel());
}
/// Game's merely dereference themselves.
impl AsGame for Game {
#[inline(always)]
fn to_game(&self) -> Game { *self }
}
/// A `Game` divided by the current `TILE_SIZE` (32 | 16) expressed
/// as an unsigned integer.
///
/// TODO: Perhaps I should round here as well
impl AsTile for Game {
#[inline(always)]
fn to_tile(&self) -> Tile {
let Game(a) = *self;
Tile((a / TILE_SIZE as f64) as uint)
}
}
/// A `Game` is simply a more precise `Pixel`, it must simply be rounded and returned
/// as a signed integer.
impl AsPixel for Game {
#[inline(always)]
fn to_pixel(&self) -> Pixel { let Game(a) = *self; Pixel(f64::round(a) as i32) }
}
/// A single `Tile` represents `TILE_SIZE` game units.
/// The conversion is a simple multiplication.
impl AsGame for Tile {
#[inline(always)]
fn to_game(&self) -> Game {
let Tile(a) = *self;
Game((a * (TILE_SIZE as uint)) as f64)
}
}
/// A `Tile` merely dereferences itself, as it is already a `Tile`.
impl AsTile for Tile {
#[inline(always)]
fn to_tile(&self) -> Tile { *self }
}
/// A `Tile` must first be converted to `Game` units, which can then be expressed
/// in terms of `Pixel`'s on the screen.
impl AsPixel for Tile {
#[inline(always)]
fn to_pixel(&self) -> Pixel { self.to_game().to_pixel() }
}
/// A `Pixel` merely dereferences itself, as it is already a `Pixel`.
impl AsPixel for Pixel {
#[inline(always)]
fn to_pixel(&self) -> Pixel { *self }
}
// Allow `+` operator for anything which can be converted `#to_game()`
impl<T: AsGame> Add<T, Game> for Game {
#[inline(always)]
fn add(&self, rhs: &T) -> Game {
let (Game(a), Game(b)) = (*self, rhs.to_game());
Game(a + b)
}
}
// Allow `*` operator for anything which can be converted `#to_game()`
impl <T: AsGame> Mul<T, Game> for Game {
#[inline(always)]
fn mul(&self, rhs: &T) -> Game {
let (Game(a), Game(b)) = (*self, rhs.to_game());
Game (a * b)
}
}
// Allow `+` operator for anything which can be converted `#to_tile()`
impl<T: AsTile> Add<T, Tile> for Tile {
#[inline(always)]
fn add(&self, rhs: &T) -> Tile {
let (Tile(a), Tile(b)) = (*self, rhs.to_tile());
Tile(a + b)
}
}
// Allow `+` operator for anything which can be converted `#to_pixel()`
impl<T: AsPixel> Add<T, Pixel> for Pixel {
#[inline(always)]
fn add(&self, rhs: &T) -> Pixel {
let (Pixel(a), Pixel(b)) = (*self, rhs.to_pixel());
Pixel(a + b)
}
}
@drbawb
Copy link
Author

drbawb commented Feb 20, 2014

main.rs:5:41: 5:54 error: type units::Game does not implement any method in scope named to_pixel
main.rs:5 println!("gs->pix, outside mod: {:?}", gs.to_pixel());
^~~~~~~~~~~~~
note: in expansion of format_args!
:2:23: 2:77 note: expansion site
:1:1: 1:1 note: in expansion of println!
main.rs:5:2: 5:56 note: expansion site
main.rs:5:41: 5:54 error: cannot determine a type for this bounded type parameter: unconstrained type
main.rs:5 println!("gs->pix, outside mod: {:?}", gs.to_pixel());
^~~~~~~~~~~~~
note: in expansion of format_args!
:2:23: 2:77 note: expansion site
:1:1: 1:1 note: in expansion of println!
main.rs:5:2: 5:56 note: expansion site

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment