2018-08-30 12:18:55 +00:00
|
|
|
//@ run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(unused_imports)]
|
2018-08-31 13:02:01 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2015-01-02 07:53:35 +00:00
|
|
|
// ASCII art shape renderer. Demonstrates traits, impls, operator overloading,
|
|
|
|
// non-copyable struct, unit testing. To run execute: rustc --test shapes.rs &&
|
|
|
|
// ./shapes
|
2013-01-08 03:34:13 +00:00
|
|
|
|
2015-01-02 07:53:35 +00: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 environments like embedded
|
|
|
|
// devices or special environments like kernel code) so it must be explicitly
|
|
|
|
// linked in.
|
2013-01-08 03:34:13 +00:00
|
|
|
|
2015-01-02 07:53:35 +00:00
|
|
|
// 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 23:28:08 +00:00
|
|
|
|
2014-02-20 02:56:33 +00:00
|
|
|
use std::fmt;
|
2015-01-02 07:53:35 +00:00
|
|
|
use std::iter::repeat;
|
|
|
|
use std::slice;
|
2013-01-08 03:34:13 +00:00
|
|
|
|
|
|
|
// Represents a position on a canvas.
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2013-02-15 05:17:26 +00:00
|
|
|
struct Point {
|
2015-03-26 00:06:52 +00:00
|
|
|
x: isize,
|
|
|
|
y: isize,
|
2013-01-08 03:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents an offset on a canvas. (This has the same structure as a Point.
|
|
|
|
// but different semantics).
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2013-06-21 12:29:53 +00:00
|
|
|
struct Size {
|
2015-03-26 00:06:52 +00:00
|
|
|
width: isize,
|
|
|
|
height: isize,
|
2013-01-08 03:34:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2013-06-21 12:29:53 +00:00
|
|
|
struct Rect {
|
2013-01-08 03:34:13 +00:00
|
|
|
top_left: Point,
|
|
|
|
size: Size,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains the information needed to do shape rendering via ASCII art.
|
2013-06-21 12:29:53 +00:00
|
|
|
struct AsciiArt {
|
2015-03-26 00:06:52 +00:00
|
|
|
width: usize,
|
|
|
|
height: usize,
|
2014-01-24 19:02:03 +00:00
|
|
|
fill: char,
|
2014-03-05 22:02:44 +00:00
|
|
|
lines: Vec<Vec<char> > ,
|
2013-01-08 03:34:13 +00:00
|
|
|
|
|
|
|
// This struct can be quite large so we'll disable copying: developers need
|
2014-01-08 02:49:13 +00:00
|
|
|
// to either pass these structs around via references or move them.
|
2013-02-28 00:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for AsciiArt {
|
2013-09-17 01:18:07 +00:00
|
|
|
fn drop(&mut self) {}
|
2013-01-08 03:34:13 +00: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 23:25:04 +00:00
|
|
|
// Other constructor sort of functions are typically named from_foo, from_bar, etc.
|
2015-03-26 00:06:52 +00:00
|
|
|
fn AsciiArt(width: usize, height: usize, fill: char) -> AsciiArt {
|
2015-07-08 20:52:55 +00:00
|
|
|
// Build a vector of vectors containing blank characters for each position in
|
|
|
|
// our canvas.
|
|
|
|
let lines = vec![vec!['.'; width]; height];
|
2013-01-08 03:34:13 +00: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 12:29:53 +00:00
|
|
|
impl AsciiArt {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn add_pt(&mut self, x: isize, y: isize) {
|
|
|
|
if x >= 0 && x < self.width as isize {
|
|
|
|
if y >= 0 && y < self.height as isize {
|
2013-01-08 03:34:13 +00:00
|
|
|
// Note that numeric types don't implicitly convert to each other.
|
2015-03-26 00:06:52 +00:00
|
|
|
let v = y as usize;
|
|
|
|
let h = x as usize;
|
2013-01-08 03:34:13 +00:00
|
|
|
|
|
|
|
// 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 17:25:16 +00:00
|
|
|
let row = &mut self.lines[v];
|
|
|
|
row[h] = self.fill;
|
2013-01-08 03:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
// Allows AsciiArt to be converted to a string using the libcore ToString trait.
|
2013-01-08 03:34:13 +00:00
|
|
|
// Note that the %s fmt! specifier will not call this automatically.
|
2015-02-21 11:28:28 +00:00
|
|
|
impl fmt::Display for AsciiArt {
|
2014-02-20 02:56:33 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2013-01-08 03:34:13 +00:00
|
|
|
// Convert each line into a string.
|
2014-03-28 19:42:34 +00:00
|
|
|
let lines = self.lines.iter()
|
2015-01-02 07:53:35 +00:00
|
|
|
.map(|line| line.iter().cloned().collect())
|
2014-05-22 23:57:53 +00:00
|
|
|
.collect::<Vec<String>>();
|
2013-01-08 03:34:13 +00:00
|
|
|
|
|
|
|
// Concatenate the lines together using a new-line.
|
2015-07-10 12:19:21 +00:00
|
|
|
write!(f, "{}", lines.join("\n"))
|
2013-01-08 03:34:13 +00: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 12:29:53 +00:00
|
|
|
trait Canvas {
|
2013-01-31 03:52:56 +00:00
|
|
|
fn add_point(&mut self, shape: Point);
|
|
|
|
fn add_rect(&mut self, shape: Rect);
|
2013-01-08 03:34:13 +00:00
|
|
|
|
|
|
|
// Unlike interfaces traits support default implementations.
|
|
|
|
// Got an ICE as soon as I added this method.
|
2024-02-07 02:42:01 +00:00
|
|
|
fn add_points(&mut self, shapes: &[Point]) { //~ WARN method `add_points` is never used
|
2015-01-31 17:20:46 +00:00
|
|
|
for pt in shapes {self.add_point(*pt)};
|
2013-01-08 03:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we provide an implementation of the Canvas methods for AsciiArt.
|
2018-11-27 02:59:49 +00:00
|
|
|
// Other implementations could also be provided (e.g., for PDF or Apple's Quartz)
|
2013-01-08 03:34:13 +00:00
|
|
|
// and code can use them polymorphically via the Canvas trait.
|
2013-02-15 05:17:26 +00:00
|
|
|
impl Canvas for AsciiArt {
|
2013-06-21 12:29:53 +00:00
|
|
|
fn add_point(&mut self, shape: Point) {
|
2013-01-08 03:34:13 +00:00
|
|
|
self.add_pt(shape.x, shape.y);
|
|
|
|
}
|
|
|
|
|
2013-06-21 12:29:53 +00:00
|
|
|
fn add_rect(&mut self, shape: Rect) {
|
2013-01-08 03:34:13 +00:00
|
|
|
// Add the top and bottom lines.
|
2015-01-26 20:46:12 +00:00
|
|
|
for x in shape.top_left.x..shape.top_left.x + shape.size.width {
|
2013-01-08 03:34:13 +00: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.
|
2015-01-26 20:46:12 +00:00
|
|
|
for y in shape.top_left.y..shape.top_left.y + shape.size.height {
|
2013-01-08 03:34:13 +00: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 12:29:53 +00:00
|
|
|
pub fn check_strs(actual: &str, expected: &str) -> bool {
|
|
|
|
if actual != expected {
|
2013-10-14 01:48:47 +00:00
|
|
|
println!("Found:\n{}\nbut expected\n{}", actual, expected);
|
2013-01-08 03:34:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 12:29:53 +00:00
|
|
|
fn test_ascii_art_ctor() {
|
2013-01-08 03:34:13 +00:00
|
|
|
let art = AsciiArt(3, 3, '*');
|
2015-02-02 02:53:25 +00:00
|
|
|
assert!(check_strs(&art.to_string(), "...\n...\n..."));
|
2013-01-08 03:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 12:29:53 +00:00
|
|
|
fn test_add_pt() {
|
2013-01-31 03:52:56 +00:00
|
|
|
let mut art = AsciiArt(3, 3, '*');
|
2013-01-08 03:34:13 +00:00
|
|
|
art.add_pt(0, 0);
|
|
|
|
art.add_pt(0, -10);
|
|
|
|
art.add_pt(1, 2);
|
2015-02-02 02:53:25 +00:00
|
|
|
assert!(check_strs(&art.to_string(), "*..\n...\n.*."));
|
2013-01-08 03:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 12:29:53 +00:00
|
|
|
fn test_shapes() {
|
2013-01-31 03:52:56 +00:00
|
|
|
let mut art = AsciiArt(4, 4, '*');
|
2013-01-08 03:34:13 +00: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});
|
2015-02-02 02:53:25 +00:00
|
|
|
assert!(check_strs(&art.to_string(), "****\n*..*\n*.**\n****"));
|
2013-01-08 03:34:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2013-01-08 03:34:13 +00:00
|
|
|
test_ascii_art_ctor();
|
|
|
|
test_add_pt();
|
|
|
|
test_shapes();
|
|
|
|
}
|