From 8727ae9d878121d85f7b1d16d031040bc87794a2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 26 Mar 2020 19:03:42 +0100 Subject: [PATCH] library: use #[serde(from)] for Deserialize --- pathtracer/src/core/camera.rs | 15 +++------------ pathtracer/src/light/spot_light.rs | 15 +++------------ pathtracer/src/render/scene.rs | 14 +++----------- pathtracer/src/shape/triangle.rs | 15 +++------------ 4 files changed, 12 insertions(+), 47 deletions(-) diff --git a/pathtracer/src/core/camera.rs b/pathtracer/src/core/camera.rs index 0c9b625..e11ba1b 100644 --- a/pathtracer/src/core/camera.rs +++ b/pathtracer/src/core/camera.rs @@ -2,10 +2,11 @@ use super::film::Film; use crate::{Point, Vector}; -use serde::{Deserialize, Deserializer}; +use serde::Deserialize; /// Represent an abstract camera to observe the scene. -#[derive(Debug, PartialEq)] +#[serde(from = "SerializedCamera")] +#[derive(Debug, PartialEq, Deserialize)] pub struct Camera { /// Where the camera is set in the scene (i.e: its focal point). origin: Point, @@ -140,16 +141,6 @@ impl From for Camera { } } -impl<'de> Deserialize<'de> for Camera { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let cam: SerializedCamera = Deserialize::deserialize(deserializer)?; - Ok(cam.into()) - } -} - #[cfg(test)] mod test { use super::*; diff --git a/pathtracer/src/light/spot_light.rs b/pathtracer/src/light/spot_light.rs index 5fed907..09d67ca 100644 --- a/pathtracer/src/light/spot_light.rs +++ b/pathtracer/src/light/spot_light.rs @@ -2,12 +2,13 @@ use super::{Light, SpatialLight}; use crate::core::LinearColor; use crate::{Point, Vector}; use nalgebra::Unit; -use serde::{Deserialize, Deserializer}; +use serde::Deserialize; /// Represent a light emanating from a directed light-source, outputting rays in a cone. /// /// The illumination cone cannot have an FOV over 180°. -#[derive(Debug, PartialEq)] +#[serde(from = "SerializedSpotLight")] +#[derive(Debug, PartialEq, Deserialize)] pub struct SpotLight { position: Point, direction: Unit, @@ -82,16 +83,6 @@ impl From for SpotLight { } } -impl<'de> Deserialize<'de> for SpotLight { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let cam: SerializedSpotLight = Deserialize::deserialize(deserializer)?; - Ok(cam.into()) - } -} - #[cfg(test)] mod test { use super::*; diff --git a/pathtracer/src/render/scene.rs b/pathtracer/src/render/scene.rs index 57b2180..ff61f86 100644 --- a/pathtracer/src/render/scene.rs +++ b/pathtracer/src/render/scene.rs @@ -13,9 +13,11 @@ use image::RgbImage; use nalgebra::Unit; use rand::prelude::thread_rng; use rand::Rng; -use serde::{Deserialize, Deserializer}; +use serde::Deserialize; /// Represent the scene being rendered. +#[serde(from = "SerializedScene")] +#[derive(Debug, PartialEq, Deserialize)] pub struct Scene { camera: Camera, lights: LightAggregate, @@ -339,16 +341,6 @@ impl From for Scene { } } -impl<'de> Deserialize<'de> for Scene { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let cam: SerializedScene = Deserialize::deserialize(deserializer)?; - Ok(cam.into()) - } -} - #[cfg(test)] mod test { use super::*; diff --git a/pathtracer/src/shape/triangle.rs b/pathtracer/src/shape/triangle.rs index 0ab3861..7786b14 100644 --- a/pathtracer/src/shape/triangle.rs +++ b/pathtracer/src/shape/triangle.rs @@ -4,10 +4,11 @@ use beevee::aabb::{Bounded, AABB}; use beevee::bvh::Intersected; use beevee::ray::Ray; use nalgebra::Unit; -use serde::{Deserialize, Deserializer}; +use serde::Deserialize; /// Represent a triangle inside the scene. -#[derive(Clone, Debug, PartialEq)] +#[serde(from = "SerializedTriangle")] +#[derive(Clone, Debug, PartialEq, Deserialize)] pub struct Triangle { c0: Point, c0c1: Vector, @@ -129,16 +130,6 @@ impl From for Triangle { } } -impl<'de> Deserialize<'de> for Triangle { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let cam: SerializedTriangle = Deserialize::deserialize(deserializer)?; - Ok(cam.into()) - } -} - #[cfg(test)] mod test { use super::*;