Image example to show Ownership
This commit is contained in:
parent
bd260f1769
commit
12a54c42da
81
src/main.rs
81
src/main.rs
@ -1,37 +1,70 @@
|
|||||||
struct Vector3 {
|
#[derive(Copy, Clone)]
|
||||||
x: f32,
|
struct RGB {
|
||||||
y: f32,
|
red: u8,
|
||||||
z: f32,
|
green: u8,
|
||||||
|
blue: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vector3 {
|
impl RGB {
|
||||||
pub fn new(x: f32, y: f32, z: f32) -> Vector3 {
|
fn new(red: u8, green: u8, blue: u8) -> RGB {
|
||||||
Vector3 { x, y, z }
|
RGB { red, green, blue }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn black() -> RGB {
|
||||||
|
RGB {
|
||||||
|
red: 0,
|
||||||
|
green: 0,
|
||||||
|
blue: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn white() -> RGB {
|
||||||
|
RGB {
|
||||||
|
red: 255,
|
||||||
|
green: 255,
|
||||||
|
blue: 255,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print(vec: Vector3) -> Vector3 {
|
struct Image {
|
||||||
println!("Vector3( x = {}, y = {}, z = {})", vec.x, vec.y, vec.z);
|
width: usize,
|
||||||
|
height: usize,
|
||||||
vec
|
data: Vec<RGB>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Vector3 {
|
impl Image {
|
||||||
fn drop(&mut self) {
|
fn new(width: usize, height: usize, data: Vec<RGB>) -> Image {
|
||||||
println!("Vector3 was dropped!");
|
if width * height != data.len() {
|
||||||
|
panic!("Width and height of image does not match amount of data.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let img = Image {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
|
||||||
|
img
|
||||||
|
}
|
||||||
|
|
||||||
|
fn white_image(width: usize, height: usize) -> Image {
|
||||||
|
let mut data = Vec::new();
|
||||||
|
|
||||||
|
for i in 0..width * height {
|
||||||
|
data.push(RGB::white());
|
||||||
|
}
|
||||||
|
|
||||||
|
Image::new(width, height, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(&self, x: usize, y: usize) -> RGB {
|
||||||
|
self.data[y * self.width + x]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut vec = Vector3::new(1.0, 0.0, 0.0);
|
let image = Image::white_image(640, 480);
|
||||||
|
|
||||||
/*
|
let c = image.get(0, 0);
|
||||||
println!("Calling print");
|
|
||||||
let vec2 = print(vec);
|
|
||||||
println!("Calling print returned");
|
|
||||||
*/
|
|
||||||
|
|
||||||
let vec2 = vec;
|
|
||||||
|
|
||||||
vec.y = 2.0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user