width: uint,
height: uint,
priv fill: char,
- priv lines: ~[~[mut char]],
+ priv lines: ~[~[char]],
// This struct can be quite large so we'll disable copying: developers need
// to either pass these structs around via borrowed pointers or move them.
{
// Use an anonymous function to build a vector of vectors containing
// blank characters for each position in our canvas.
- let lines = do vec::build_sized(height)
+ let mut lines = do vec::build_sized(height)
|push|
{
for height.times
{
let mut line = ~[];
vec::grow_set(&mut line, width-1, &'.', '.');
- push(vec::cast_to_mut(line));
+ push(line);
}
};
// Methods particular to the AsciiArt struct.
impl AsciiArt
{
- fn add_pt(x: int, y: int)
+ fn add_pt(&mut self, x: int, y: int)
{
if x >= 0 && x < self.width as int
{
// element is:
// 1) potentially large
// 2) needs to be modified
- let row = &self.lines[v];
+ let row = &mut self.lines[v];
row[h] = self.fill;
}
}
#[allow(default_methods)]
trait Canvas
{
- fn add_point(shape: Point);
- fn add_rect(shape: Rect);
+ fn add_point(&mut self, shape: Point);
+ fn add_rect(&mut self, shape: Rect);
// Unlike interfaces traits support default implementations.
// Got an ICE as soon as I added this method.
- fn add_points(shapes: &[Point])
+ fn add_points(&mut self, shapes: &[Point])
{
for shapes.each |pt| {self.add_point(*pt)};
}
// and code can use them polymorphically via the Canvas trait.
impl AsciiArt : Canvas
{
- fn add_point(shape: Point)
+ fn add_point(&mut self, shape: Point)
{
self.add_pt(shape.x, shape.y);
}
- fn add_rect(shape: Rect)
+ fn add_rect(&mut self, shape: Rect)
{
// Add the top and bottom lines.
for int::range(shape.top_left.x, shape.top_left.x + shape.size.width)
fn test_add_pt()
{
- let art = AsciiArt(3, 3, '*');
+ let mut art = AsciiArt(3, 3, '*');
art.add_pt(0, 0);
art.add_pt(0, -10);
art.add_pt(1, 2);
fn test_shapes()
{
- let art = AsciiArt(4, 4, '*');
+ let mut art = AsciiArt(4, 4, '*');
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});
assert check_strs(art.to_str(), "****\n*..*\n*.**\n****");