Skip to content

Commit

Permalink
Fix some lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TrAyZeN committed Jun 9, 2024
1 parent 277b75c commit 1431815
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 60 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ required-features = ["uefi"]

[features]
default = ["std"]
std = ["dep:image", "rand/default", "dep:core_affinity"]
std = ["dep:image", "rand/default", "dep:core_affinity", "dep:anyhow"]
uefi = ["dep:uefi", "dep:core_maths", "dep:log"]

[dependencies]
Expand All @@ -36,6 +36,7 @@ core_affinity = { version = "0.8.1", optional = true }
uefi = { version = "0.28.0", features = ["alloc", "global_allocator", "logger", "panic_handler"], optional = true }
core_maths = { version = "0.1.0", optional = true }
log = { version = "0.4.21", optional = true }
anyhow = { version = "1.0.86", optional = true }

[dev-dependencies]
criterion = "0.4"
Expand Down
4 changes: 3 additions & 1 deletion examples/rt_nextweek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ fn earth() -> Scene {
.add_sphere(Sphere::new(
Point3::new(0., 0., 0.),
2.,
Arc::new(Lambertian::new(Image::load("resources/earthmap.jpg"))),
Arc::new(Lambertian::new(
Image::load("resources/earthmap.jpg").unwrap(),
)),
))
.build()
}
Expand Down
2 changes: 1 addition & 1 deletion src/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Aabb {
impl Aabb {
/// Constructs an AABB from two opposite points.
///
/// # Panic
/// # Panics
/// Panics in `debug` mode if `min.x > max.x || min.y > max.y || min.z > max.z`.
///
/// # Examples
Expand Down
2 changes: 1 addition & 1 deletion src/bin/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use uefi::table::boot::{EventType, Tpl};

/// Return the `num_views` [`PartialRowViewMut`] of the given slice.
///
/// # Panic
/// # Panics
/// Panics if given slice's length is not a multiple of width.
fn partial_row_views_mut<'a, T>(
slice: &'a mut [T],
Expand Down
8 changes: 4 additions & 4 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Camera {
impl Camera {
/// Constructs a new `Camera` with the given lookfrom and lookat points and the vfov and aspect ratio.
///
/// # Panic
/// # Panics
/// Panics if `lookfrom == lookat`.
/// Panics if `aspect_ratio <= 0.`.
///
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Camera {

/// Consumes the `Camera` and returns self after setting the vup.
///
/// # Panic
/// # Panics
/// Panics if `vup == Vec3::new(0., 0., 0.)`.
///
/// # Example
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Camera {

/// Consumes the `Camera` and returns self after setting the aperture.
///
/// # Panic
/// # Panics
/// Panics if `aperture < 0.`.
///
/// # Example
Expand All @@ -129,7 +129,7 @@ impl Camera {

/// Consumes the `Camera` and returns self after setting the focus distance.
///
/// # Panic
/// # Panics
/// Panics if `focus_dist == 0.`.
///
/// # Example
Expand Down
2 changes: 1 addition & 1 deletion src/core/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Ray {
impl Ray {
/// Constructs a new `Ray` from the given origin, direction and time.
///
/// # Panic
/// # Panics
/// Panics in `debug` mode if `direction == Vec3::new(0., 0., 0.)`.
///
/// # Examples
Expand Down
76 changes: 42 additions & 34 deletions src/core/vec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::{
convert, iter,
iter,
ops::{self, Add, Sub},
};

Expand Down Expand Up @@ -370,7 +370,7 @@ macro_rules! forward_ref_binop {
};
}

impl ops::Add<Vec3> for Vec3 {
impl ops::Add<Self> for Vec3 {
type Output = Self;

#[inline(always)]
Expand All @@ -381,7 +381,7 @@ impl ops::Add<Vec3> for Vec3 {

forward_ref_binop! { impl Add, add for Vec3, Vec3 }

impl ops::AddAssign<Vec3> for Vec3 {
impl ops::AddAssign<Self> for Vec3 {
#[inline]
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
Expand All @@ -408,7 +408,7 @@ impl ops::Neg for &Vec3 {
}
}

impl ops::Sub<Vec3> for Vec3 {
impl ops::Sub<Self> for Vec3 {
type Output = Self;

#[inline(always)]
Expand All @@ -420,7 +420,7 @@ impl ops::Sub<Vec3> for Vec3 {

forward_ref_binop! { impl Sub, sub for Vec3, Vec3 }

impl ops::SubAssign<Vec3> for Vec3 {
impl ops::SubAssign<Self> for Vec3 {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
Expand All @@ -429,7 +429,7 @@ impl ops::SubAssign<Vec3> for Vec3 {
}
}

impl ops::Mul<Vec3> for Vec3 {
impl ops::Mul<Self> for Vec3 {
type Output = Self;

#[inline(always)]
Expand Down Expand Up @@ -508,7 +508,7 @@ impl ops::DivAssign<f32> for Vec3 {
}
}

impl ops::Div<Vec3> for Vec3 {
impl ops::Div<Self> for Vec3 {
type Output = Self;

#[inline(always)]
Expand All @@ -534,55 +534,63 @@ impl ops::IndexMut<usize> for Vec3 {
}

#[cfg(feature = "std")]
impl convert::Into<Rgb<u8>> for Vec3 {
impl From<Vec3> for Rgb<u8> {
#[inline(always)]
fn into(self) -> Rgb<u8> {
Rgb([
(255. * self.x.clamp(0., 1.)) as u8,
(255. * self.y.clamp(0., 1.)) as u8,
(255. * self.z.clamp(0., 1.)) as u8,
fn from(vec: Vec3) -> Self {
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
Self([
(255. * vec.x.clamp(0., 1.)) as u8,
(255. * vec.y.clamp(0., 1.)) as u8,
(255. * vec.z.clamp(0., 1.)) as u8,
])
}
}

#[cfg(feature = "std")]
impl convert::Into<Rgb<u8>> for &Vec3 {
impl From<&Vec3> for Rgb<u8> {
#[inline(always)]
fn into(self) -> Rgb<u8> {
Rgb([
(255. * self.x.clamp(0., 1.)) as u8,
(255. * self.y.clamp(0., 1.)) as u8,
(255. * self.z.clamp(0., 1.)) as u8,
fn from(vec: &Vec3) -> Self {
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
Self([
(255. * vec.x.clamp(0., 1.)) as u8,
(255. * vec.y.clamp(0., 1.)) as u8,
(255. * vec.z.clamp(0., 1.)) as u8,
])
}
}

#[cfg(feature = "uefi")]
impl convert::Into<BltPixel> for Vec3 {
impl From<Vec3> for BltPixel {
#[inline(always)]
fn into(self) -> BltPixel {
BltPixel::new(
(255. * self.x.clamp(0., 1.)) as u8,
(255. * self.y.clamp(0., 1.)) as u8,
(255. * self.z.clamp(0., 1.)) as u8,
fn from(vec: Vec3) -> Self {
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
Self::new(
(255. * vec.x.clamp(0., 1.)) as u8,
(255. * vec.y.clamp(0., 1.)) as u8,
(255. * vec.z.clamp(0., 1.)) as u8,
)
}
}

#[cfg(feature = "uefi")]
impl convert::Into<BltPixel> for &Vec3 {
impl From<&Vec3> for BltPixel {
#[inline(always)]
fn into(self) -> BltPixel {
BltPixel::new(
(255. * self.x.clamp(0., 1.)) as u8,
(255. * self.y.clamp(0., 1.)) as u8,
(255. * self.z.clamp(0., 1.)) as u8,
fn from(vec: &Vec3) -> Self {
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
Self::new(
(255. * vec.x.clamp(0., 1.)) as u8,
(255. * vec.y.clamp(0., 1.)) as u8,
(255. * vec.z.clamp(0., 1.)) as u8,
)
}
}

// Needed if using rayon
impl iter::Sum<Vec3> for Vec3 {
impl iter::Sum<Self> for Vec3 {
#[inline(always)]
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self::zero(), Add::add)
Expand All @@ -596,10 +604,10 @@ mod tests {
use quickcheck::{Arbitrary, Gen, TestResult};

impl Arbitrary for Vec3 {
fn arbitrary(g: &mut Gen) -> Vec3 {
fn arbitrary(g: &mut Gen) -> Self {
let nan_to_default = |f: f32| if f.is_nan() { f32::default() } else { f };

Vec3::new(
Self::new(
nan_to_default(f32::arbitrary(g)),
nan_to_default(f32::arbitrary(g)),
nan_to_default(f32::arbitrary(g)),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
clippy::nursery,
clippy::cargo
)]
#![allow(clippy::inline_always)]

extern crate alloc;
#[cfg(feature = "std")]
Expand Down
2 changes: 1 addition & 1 deletion src/materials/dielectric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Dielectric {
impl Dielectric {
/// Constructs a new `Dielectric` material with the given refractive index.
///
/// # Panic
/// # Panics
/// Panics if `refractive_index < 1.`.
///
/// # Examples
Expand Down
4 changes: 2 additions & 2 deletions src/objects/constant_medium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub struct ConstantMedium<M: Material> {
}

impl<M: Material> ConstantMedium<M> {
/// Constructs a new ConstantMedium with the given boundary, density and phase function.
/// Constructs a new [`ConstantMedium`] with the given boundary, density and phase function.
///
/// # Panic
/// # Panics
/// Panics if `density == 0.`.
#[inline]
#[must_use]
Expand Down
2 changes: 1 addition & 1 deletion src/objects/rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ impl Hitable for RotateY {

#[must_use]
fn bounding_box(&self, _time_interval: (f32, f32)) -> Option<Aabb> {
self.hasbox.then(|| self.bbox)
self.hasbox.then_some(self.bbox)
}
}
2 changes: 1 addition & 1 deletion src/objects/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Sphere<M: Material> {
impl<M: Material> Sphere<M> {
/// Constructs a sphere from the given center, radius and material.
///
/// # Panic
/// # Panics
/// Panics if `radius <= 0.`.
///
/// # Examples
Expand Down
23 changes: 15 additions & 8 deletions src/textures/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use super::Texture;
use crate::vec::{Point3, Vec3};
use alloc::vec::Vec;

#[cfg(feature = "std")]
use anyhow::Result;

// For now the image only support RGB
#[derive(Debug)]
pub struct Image {
Expand All @@ -12,6 +15,8 @@ pub struct Image {
}

impl Image {
/// # Panics
/// Panics if the data length is not equal to `width * height * 3`.
#[inline]
#[must_use]
pub fn new(width: usize, height: usize, data: Vec<u8>) -> Self {
Expand All @@ -25,14 +30,16 @@ impl Image {
}

#[cfg(feature = "std")]
#[must_use]
pub fn load(filename: &str) -> Self {
// TODO: Handle error
let image_buffer = image::open(filename).unwrap().into_rgb8();
pub fn load(filename: &str) -> Result<Self> {
let image_buffer = image::open(filename)?.into_rgb8();
let width = image_buffer.width();
let height = image_buffer.height();

Self::new(width as usize, height as usize, image_buffer.into_raw())
Ok(Self::new(
width as usize,
height as usize,
image_buffer.into_raw(),
))
}
}

Expand All @@ -55,9 +62,9 @@ impl Texture for Image {
let color_scale = 1. / 255.;
let pixel = i * 3 + j * 3 * self.width;
Vec3::new(
color_scale * self.data[pixel] as f32,
color_scale * self.data[pixel + 1] as f32,
color_scale * self.data[pixel + 2] as f32,
color_scale * f32::from(self.data[pixel]),
color_scale * f32::from(self.data[pixel + 1]),
color_scale * f32::from(self.data[pixel + 2]),
)
}
}
Loading

0 comments on commit 1431815

Please sign in to comment.