Reformat using cargofmt

This commit is contained in:
timvisee
2018-09-22 18:30:29 +02:00
parent c37da9b40b
commit bd6b48cd3b
10 changed files with 146 additions and 223 deletions

View File

@@ -1,16 +1,14 @@
use std::sync::mpsc;
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use image::DynamicImage;
use painter::painter::Painter;
use painter::handle::Handle;
use painter::painter::Painter;
use pix::client::Client;
use rect::Rect;
/// A pixflut instance
pub struct Canvas {
host: String,
@@ -22,12 +20,7 @@ pub struct Canvas {
impl Canvas {
/// Create a new pixelflut canvas.
pub fn new(
host: &str,
painter_count: usize,
size: (u32, u32),
offset: (u32, u32),
) -> Canvas {
pub fn new(host: &str, painter_count: usize, size: (u32, u32), offset: (u32, u32)) -> Canvas {
// Initialize the object
let mut canvas = Canvas {
host: host.to_string(),
@@ -51,19 +44,14 @@ impl Canvas {
fn spawn_painters(&mut self) {
// Spawn some painters
for i in 0..self.painter_count {
// Determine the slice width
let width = self.size.0 / (self.painter_count as u32);
// Determine the slice width
let width = self.size.0 / (self.painter_count as u32);
// Define the area to paint per thread
let painter_area = Rect::from(
(i as u32) * width,
0,
width,
self.size.1,
);
// Define the area to paint per thread
let painter_area = Rect::from((i as u32) * width, 0, width, self.size.1);
// Spawn the painter
self.spawn_painter(painter_area);
// Spawn the painter
self.spawn_painter(painter_area);
}
}
@@ -76,38 +64,24 @@ impl Canvas {
let offset = (self.offset.0, self.offset.1);
// Create a channel to push new images
let (tx, rx): (Sender<DynamicImage>, Receiver<DynamicImage>)
= mpsc::channel();
let (tx, rx): (Sender<DynamicImage>, Receiver<DynamicImage>) = mpsc::channel();
// Create the painter thread
let thread = thread::spawn(move || {
// Create a new client
let client = Client::connect(host)
.expect("failed to open stream to pixelflut");
let client = Client::connect(host).expect("failed to open stream to pixelflut");
// Create a painter
let mut painter = Painter::new(
client,
area,
offset,
None,
);
let mut painter = Painter::new(client, area, offset, None);
// Do some work
loop {
painter.work(&rx)
.expect("Painter failed to perform work");
painter.work(&rx).expect("Painter failed to perform work");
}
});
// Create a new painter handle, pust it to the list
self.painter_handles.push(
Handle::new(
thread,
area,
tx,
)
);
self.painter_handles.push(Handle::new(thread, area, tx));
}
// Update the image that is being rendered for all painters.

View File

@@ -1,8 +1,8 @@
extern crate bufstream;
extern crate regex;
use std::io::{Error, ErrorKind};
use std::io::prelude::*;
use std::io::{Error, ErrorKind};
use std::net::TcpStream;
use self::bufstream::BufStream;
@@ -10,18 +10,13 @@ use self::regex::Regex;
use color::Color;
// The default buffer size for reading the client stream.
// - Big enough so we don't have to expand
// - Small enough to not take up to much memory
const CMD_READ_BUFFER_SIZE: usize = 32;
// The response format of the screen size from a pixelflut server.
const PIX_SERVER_SIZE_REGEX: &'static str =
r"^(?i)\s*SIZE\s+([[:digit:]]+)\s+([[:digit:]]+)\s*$";
const PIX_SERVER_SIZE_REGEX: &'static str = r"^(?i)\s*SIZE\s+([[:digit:]]+)\s+([[:digit:]]+)\s*$";
/// A pixelflut client.
///
@@ -45,19 +40,13 @@ impl Client {
/// Create a new client instane from the given host, and connect to it.
pub fn connect(host: String) -> Result<Client, Error> {
// Create a new stream, and instantiate the client
Ok(
Client::new(
create_stream(host)?
)
)
Ok(Client::new(create_stream(host)?))
}
/// Write a pixel to the given stream.
pub fn write_pixel(&mut self, x: u32, y: u32, color: &Color) -> Result<(), Error> {
// Write the command to set a pixel
self.write_command(
format!("PX {} {} {}", x, y, color.as_hex()),
)
self.write_command(format!("PX {} {} {}", x, y, color.as_hex()))
}
/// Read the size of the screen.
@@ -73,12 +62,17 @@ impl Client {
// Find captures in the data, return the result
match re.captures(&data) {
Some(matches) => Ok((
matches[1].parse::<u32>().expect("Failed to parse screen width, received malformed data"),
matches[2].parse::<u32>().expect("Failed to parse screen height, received malformed data"),
matches[1]
.parse::<u32>()
.expect("Failed to parse screen width, received malformed data"),
matches[2]
.parse::<u32>()
.expect("Failed to parse screen height, received malformed data"),
)),
None => Err(Error::new(
ErrorKind::Other,
"Failed to parse screen size, received malformed data",
)),
None => Err(
Error::new(ErrorKind::Other, "Failed to parse screen size, received malformed data")
),
}
}
@@ -92,7 +86,8 @@ impl Client {
// TODO: only flush each 100 pixels?
// TODO: make flushing configurable?
// TODO: make buffer size configurable?
self.stream.flush()
self.stream
.flush()
.expect("failed to flush write buffer to server");
// Everything seems to be ok
@@ -124,8 +119,6 @@ impl Drop for Client {
}
}
/// Create a stream to talk to the pixelflut server.
///
/// The stream is returned as result.