Last active
July 31, 2020 09:08
-
-
Save kwannoel/e5060588f768767e24dbd2083fad61f5 to your computer and use it in GitHub Desktop.
Usecase for Rc in Raytracers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0382]: use of moved value: `metal_shiny` | |
--> temp.rs:39:38 | |
| | |
37 | let metal_shiny = Metal::new(0.0); | |
| ----------- move occurs because `metal_shiny` has type `Metal`, which does not implement the `Copy` trait | |
38 | let sphere1 = Sphere::new(Box::new(metal_shiny)); | |
| ----------- value moved here | |
39 | let sphere2 = Sphere::new(Box::new(metal_shiny)); | |
| ^^^^^^^^^^^ value used here after move |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rustc use-box.rs | |
use std::rc::Rc; | |
pub trait Material { | |
// Scatters incident rays | |
fn scatter (&self); // Definition is elided | |
} | |
pub struct Metal { | |
// Surface fuzziness | |
fuzz: f64, | |
} | |
impl Metal { | |
pub fn new (fuzz: f64) -> Self { | |
Self { fuzz } | |
} | |
} | |
impl Material for Metal { | |
fn scatter(&self) { /* ... */ } | |
} | |
pub struct Sphere | |
{ | |
// Some other fields have been elided | |
material: Box<dyn Material>, | |
} | |
impl Sphere { | |
pub fn new(material: Box<dyn Material>) -> Self { | |
Sphere { material } | |
} | |
} | |
fn main () { | |
let metal_shiny = Box::new(Metal::new(0.0)); | |
let sphere1 = Sphere::new(metal_shiny); | |
let sphere1 = Sphere::new(metal_shiny); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kwannoel@Kwans-MacBook-Pro temp % rustc temp.rs | |
warning: unused variable: `sphere1` | |
--> temp.rs:38:7 | |
| | |
38 | let sphere1 = Sphere::new(metal_shiny.clone()); | |
| ^^^^^^^ help: consider prefixing with an underscore: `_sphere1` | |
| | |
= note: `#[warn(unused_variables)]` on by default | |
warning: unused variable: `sphere2` | |
--> temp.rs:39:7 | |
| | |
39 | let sphere2 = Sphere::new(metal_shiny.clone()); | |
| ^^^^^^^ help: consider prefixing with an underscore: `_sphere2` | |
warning: field is never read: `fuzz` | |
--> temp.rs:11:5 | |
| | |
11 | fuzz: f64, | |
| ^^^^^^^^^ | |
| | |
= note: `#[warn(dead_code)]` on by default | |
warning: field is never read: `material` | |
--> temp.rs:27:5 | |
| | |
27 | material: Rc<dyn Material>, | |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rustc use-rc.rs | |
use std::rc::Rc; | |
pub trait Material { | |
// Scatters incident rays | |
fn scatter (&self); // Definition is elided | |
} | |
pub struct Metal { | |
// Surface fuzziness | |
fuzz: f64, | |
} | |
impl Metal { | |
pub fn new (fuzz: f64) -> Self { | |
Self { fuzz } | |
} | |
} | |
impl Material for Metal { | |
fn scatter(&self) { /* ... */ } | |
} | |
pub struct Sphere | |
{ | |
// Some other fields have been elided | |
material: Rc<dyn Material>, | |
} | |
impl Sphere { | |
pub fn new(material: Rc<dyn Material>) -> Self { | |
Sphere { material } | |
} | |
} | |
fn main () { | |
let metal_shiny = Rc::new(Metal::new(0.0)); | |
let sphere1 = Sphere::new(metal_shiny.clone()); // Clone the pointer, incrementing the reference count | |
let sphere2 = Sphere::new(metal_shiny.clone()); // As above | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment