wahn / rs_pbrt

Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code. See also https://www.rs-pbrt.org/about ...

Home Page:https://www.rs-pbrt.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use generics instead of dynamic dispatch

fu5ha opened this issue · comments

In many places currently, the code uses dynamic dispatch when you could instead use generics and static dispatch. For example, the render method

pub fn render(
    scene: &Scene,
    camera: &Arc<Camera + Send + Sync>,
    sampler: &mut Box<Sampler + Send + Sync>,
    integrator: &mut Box<SamplerIntegrator + Send + Sync>,
    num_threads: u8,
) {

could instead be

pub fn render<SC, SA, I> (
    scene: &SC,
    camera: &Arc<Camera + Send + Sync>,
    sampler: &mut SA,
    integrator: &mut I,
    num_threads: u8,
) 
where
    SC: Scene + Send + Sync,
    SA: SamplerIntegrater + Send + Sync,
    I: Integrator + Send + Sync
{
    //....
}

Thanks for the suggestion. I will look into it ...

This is usually not possible because the concrete type of most of the arguments, such as the Integrator, isn't known at compile time. It is only known at runtime after parsing the scene file...

I was able to use a generic function in commit 68580a9, there might be other places where this makes sense to change ... Thanks (again) for the suggestion.