2013-01-07 21:34:13 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// ASCII art shape renderer.
|
|
|
|
// Demonstrates traits, impls, operator overloading, non-copyable struct, unit testing.
|
|
|
|
// To run execute: rustc --test shapes.rs && ./shapes
|
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
// Rust's std library is tightly bound to the language itself so it is automatically linked in.
|
|
|
|
// However the extra library is designed to be optional (for code that must run on constrained
|
2013-01-07 21:34:13 -06:00
|
|
|
// environments like embedded devices or special environments like kernel code) so it must
|
|
|
|
// be explicitly linked in.
|
|
|
|
|
|
|
|
// Extern mod controls linkage. Use controls the visibility of names to modules that are
|
|
|
|
// already linked in. Using WriterUtil allows us to use the write_line method.
|
2014-03-05 17:28:08 -06:00
|
|
|
|
2014-03-08 17:11:52 -06:00
|
|
|
use std::slice;
|
2014-02-19 20:56:33 -06:00
|
|
|
use std::fmt;
|
2013-01-07 21:34:13 -06:00
|
|
|
|
|
|
|
// Represents a position on a canvas.
|
2013-02-14 23:17:26 -06:00
|
|
|
struct Point {
|
2013-01-07 21:34:13 -06:00
|
|
|
x: int,
|
|
|
|
y: int,
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for Point {}
|
|
|
|
|
2013-01-07 21:34:13 -06:00
|
|
|
// Represents an offset on a canvas. (This has the same structure as a Point.
|
|
|
|
// but different semantics).
|
2013-06-21 07:29:53 -05:00
|
|
|
struct Size {
|
2013-01-07 21:34:13 -06:00
|
|
|
width: int,
|
|
|
|
height: int,
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for Size {}
|
|
|
|
|
2013-06-21 07:29:53 -05:00
|
|
|
struct Rect {
|
2013-01-07 21:34:13 -06:00
|
|
|
top_left: Point,
|
|
|
|
size: Size,
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for Rect {}
|
|
|
|
|
2013-01-07 21:34:13 -06:00
|
|
|
// Contains the information needed to do shape rendering via ASCII art.
|
2013-06-21 07:29:53 -05:00
|
|
|
struct AsciiArt {
|
2013-01-07 21:34:13 -06:00
|
|
|
width: uint,
|
|
|
|
height: uint,
|
2014-01-24 13:02:03 -06:00
|
|
|
fill: char,
|
2014-03-05 16:02:44 -06:00
|
|
|
lines: Vec<Vec<char> > ,
|
2013-01-07 21:34:13 -06:00
|
|
|
|
|
|
|
// This struct can be quite large so we'll disable copying: developers need
|
2014-01-07 20:49:13 -06:00
|
|
|
// to either pass these structs around via references or move them.
|
2013-02-27 18:13:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for AsciiArt {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {}
|
2013-01-07 21:34:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// It's common to define a constructor sort of function to create struct instances.
|
|
|
|
// If there is a canonical constructor it is typically named the same as the type.
|
2013-05-03 18:25:04 -05:00
|
|
|
// Other constructor sort of functions are typically named from_foo, from_bar, etc.
|
2013-06-21 07:29:53 -05:00
|
|
|
fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt {
|
2013-01-07 21:34:13 -06:00
|
|
|
// Use an anonymous function to build a vector of vectors containing
|
|
|
|
// blank characters for each position in our canvas.
|
2014-03-05 17:28:08 -06:00
|
|
|
let mut lines = Vec::new();
|
|
|
|
for _ in range(0, height) {
|
|
|
|
lines.push(Vec::from_elem(width, '.'));
|
|
|
|
}
|
2013-01-07 21:34:13 -06:00
|
|
|
|
|
|
|
// Rust code often returns values by omitting the trailing semi-colon
|
|
|
|
// instead of using an explicit return statement.
|
|
|
|
AsciiArt {width: width, height: height, fill: fill, lines: lines}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Methods particular to the AsciiArt struct.
|
2013-06-21 07:29:53 -05:00
|
|
|
impl AsciiArt {
|
|
|
|
fn add_pt(&mut self, x: int, y: int) {
|
|
|
|
if x >= 0 && x < self.width as int {
|
|
|
|
if y >= 0 && y < self.height as int {
|
2013-01-07 21:34:13 -06:00
|
|
|
// Note that numeric types don't implicitly convert to each other.
|
|
|
|
let v = y as uint;
|
|
|
|
let h = x as uint;
|
|
|
|
|
|
|
|
// Vector subscripting will normally copy the element, but &v[i]
|
|
|
|
// will return a reference which is what we need because the
|
|
|
|
// element is:
|
|
|
|
// 1) potentially large
|
|
|
|
// 2) needs to be modified
|
2014-11-06 11:25:16 -06:00
|
|
|
let row = &mut self.lines[v];
|
|
|
|
row[h] = self.fill;
|
2013-01-07 21:34:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
// Allows AsciiArt to be converted to a string using the libcore ToString trait.
|
2013-01-07 21:34:13 -06:00
|
|
|
// Note that the %s fmt! specifier will not call this automatically.
|
2014-02-19 20:56:33 -06:00
|
|
|
impl fmt::Show for AsciiArt {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2013-01-07 21:34:13 -06:00
|
|
|
// Convert each line into a string.
|
2014-03-28 14:42:34 -05:00
|
|
|
let lines = self.lines.iter()
|
2014-07-04 14:55:58 -05:00
|
|
|
.map(|line| String::from_chars(line.as_slice()))
|
2014-05-22 18:57:53 -05:00
|
|
|
.collect::<Vec<String>>();
|
2013-01-07 21:34:13 -06:00
|
|
|
|
|
|
|
// Concatenate the lines together using a new-line.
|
2014-05-10 16:05:06 -05:00
|
|
|
write!(f, "{}", lines.connect("\n"))
|
2013-01-07 21:34:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is similar to an interface in other languages: it defines a protocol which
|
|
|
|
// developers can implement for arbitrary concrete types.
|
2013-06-21 07:29:53 -05:00
|
|
|
trait Canvas {
|
2013-01-30 21:52:56 -06:00
|
|
|
fn add_point(&mut self, shape: Point);
|
|
|
|
fn add_rect(&mut self, shape: Rect);
|
2013-01-07 21:34:13 -06:00
|
|
|
|
|
|
|
// Unlike interfaces traits support default implementations.
|
|
|
|
// Got an ICE as soon as I added this method.
|
2013-06-21 07:29:53 -05:00
|
|
|
fn add_points(&mut self, shapes: &[Point]) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for pt in shapes.iter() {self.add_point(*pt)};
|
2013-01-07 21:34:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we provide an implementation of the Canvas methods for AsciiArt.
|
|
|
|
// Other implementations could also be provided (e.g. for PDF or Apple's Quartz)
|
|
|
|
// and code can use them polymorphically via the Canvas trait.
|
2013-02-14 23:17:26 -06:00
|
|
|
impl Canvas for AsciiArt {
|
2013-06-21 07:29:53 -05:00
|
|
|
fn add_point(&mut self, shape: Point) {
|
2013-01-07 21:34:13 -06:00
|
|
|
self.add_pt(shape.x, shape.y);
|
|
|
|
}
|
|
|
|
|
2013-06-21 07:29:53 -05:00
|
|
|
fn add_rect(&mut self, shape: Rect) {
|
2013-01-07 21:34:13 -06:00
|
|
|
// Add the top and bottom lines.
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in range(shape.top_left.x, shape.top_left.x + shape.size.width) {
|
2013-01-07 21:34:13 -06:00
|
|
|
self.add_pt(x, shape.top_left.y);
|
|
|
|
self.add_pt(x, shape.top_left.y + shape.size.height - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the left and right lines.
|
2013-08-03 11:45:23 -05:00
|
|
|
for y in range(shape.top_left.y, shape.top_left.y + shape.size.height) {
|
2013-01-07 21:34:13 -06:00
|
|
|
self.add_pt(shape.top_left.x, y);
|
|
|
|
self.add_pt(shape.top_left.x + shape.size.width - 1, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rust's unit testing framework is currently a bit under-developed so we'll use
|
|
|
|
// this little helper.
|
2013-06-21 07:29:53 -05:00
|
|
|
pub fn check_strs(actual: &str, expected: &str) -> bool {
|
|
|
|
if actual != expected {
|
2013-10-13 20:48:47 -05:00
|
|
|
println!("Found:\n{}\nbut expected\n{}", actual, expected);
|
2013-01-07 21:34:13 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 07:29:53 -05:00
|
|
|
fn test_ascii_art_ctor() {
|
2013-01-07 21:34:13 -06:00
|
|
|
let art = AsciiArt(3, 3, '*');
|
2014-06-21 05:39:03 -05:00
|
|
|
assert!(check_strs(art.to_string().as_slice(), "...\n...\n..."));
|
2013-01-07 21:34:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 07:29:53 -05:00
|
|
|
fn test_add_pt() {
|
2013-01-30 21:52:56 -06:00
|
|
|
let mut art = AsciiArt(3, 3, '*');
|
2013-01-07 21:34:13 -06:00
|
|
|
art.add_pt(0, 0);
|
|
|
|
art.add_pt(0, -10);
|
|
|
|
art.add_pt(1, 2);
|
2014-06-21 05:39:03 -05:00
|
|
|
assert!(check_strs(art.to_string().as_slice(), "*..\n...\n.*."));
|
2013-01-07 21:34:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 07:29:53 -05:00
|
|
|
fn test_shapes() {
|
2013-01-30 21:52:56 -06:00
|
|
|
let mut art = AsciiArt(4, 4, '*');
|
2013-01-07 21:34:13 -06:00
|
|
|
art.add_rect(Rect {top_left: Point {x: 0, y: 0}, size: Size {width: 4, height: 4}});
|
|
|
|
art.add_point(Point {x: 2, y: 2});
|
2014-06-21 05:39:03 -05:00
|
|
|
assert!(check_strs(art.to_string().as_slice(), "****\n*..*\n*.**\n****"));
|
2013-01-07 21:34:13 -06:00
|
|
|
}
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2013-01-07 21:34:13 -06:00
|
|
|
test_ascii_art_ctor();
|
|
|
|
test_add_pt();
|
|
|
|
test_shapes();
|
|
|
|
}
|