From d3d1589cffa55008ce9312c0090c0cffd18132b6 Mon Sep 17 00:00:00 2001 From: Samuel Hurel Date: Sat, 29 Jan 2022 11:17:41 +0100 Subject: [PATCH] Fix clippy warnings --- .appveyor.yml | 1 + examples/export/main.rs | 2 +- gltf-json/src/accessor.rs | 6 ++--- gltf-json/src/animation.rs | 5 ++-- gltf-json/src/buffer.rs | 2 +- gltf-json/src/camera.rs | 2 +- gltf-json/src/extensions/mod.rs | 4 +-- gltf-json/src/extensions/scene.rs | 11 +++----- gltf-json/src/image.rs | 2 +- gltf-json/src/material.rs | 2 +- gltf-json/src/mesh.rs | 6 ++--- gltf-json/src/root.rs | 1 + gltf-json/src/texture.rs | 6 ++--- src/accessor/mod.rs | 2 +- src/accessor/sparse.rs | 15 +++-------- src/accessor/util.rs | 30 ++++++++++----------- src/animation/mod.rs | 23 +++++------------ src/binary.rs | 6 ++--- src/buffer.rs | 14 +++++----- src/camera.rs | 20 ++++++-------- src/image.rs | 8 +++--- src/import.rs | 35 ++++++++++++------------- src/khr_lights_punctual.rs | 4 +-- src/khr_materials_variants.rs | 2 +- src/lib.rs | 26 ++++++++++--------- src/material.rs | 43 ++++++++----------------------- src/math.rs | 8 ++---- src/mesh/iter.rs | 1 + src/mesh/mod.rs | 16 +++++------- src/scene/mod.rs | 24 +++++++---------- src/skin/mod.rs | 8 +++--- src/texture.rs | 23 +++++++---------- tests/roundtrip_binary_gltf.rs | 2 +- tests/test_wrapper.rs | 4 +-- 34 files changed, 151 insertions(+), 213 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 1268c28e..781ec848 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -14,3 +14,4 @@ before_test: test_script: - cargo test --all --all-features --release - cargo fmt --all -- --check + - cargo clippy --all-features diff --git a/examples/export/main.rs b/examples/export/main.rs index ce521cee..c3508417 100644 --- a/examples/export/main.rs +++ b/examples/export/main.rs @@ -179,7 +179,7 @@ fn export(output: Output) { align_to_multiple_of_four(&mut json_offset); let glb = gltf::binary::Glb { header: gltf::binary::Header { - magic: b"glTF".clone(), + magic: *b"glTF", version: 2, length: json_offset + buffer_length, }, diff --git a/gltf-json/src/accessor.rs b/gltf-json/src/accessor.rs index 6b3b18ad..9b67ca75 100644 --- a/gltf-json/src/accessor.rs +++ b/gltf-json/src/accessor.rs @@ -61,7 +61,7 @@ pub const UNSIGNED_INT: u32 = 5125; pub const FLOAT: u32 = 5126; /// All valid generic vertex attribute component types. -pub const VALID_COMPONENT_TYPES: &'static [u32] = &[ +pub const VALID_COMPONENT_TYPES: &[u32] = &[ BYTE, UNSIGNED_BYTE, SHORT, @@ -71,10 +71,10 @@ pub const VALID_COMPONENT_TYPES: &'static [u32] = &[ ]; /// All valid index component types. -pub const VALID_INDEX_TYPES: &'static [u32] = &[UNSIGNED_BYTE, UNSIGNED_SHORT, UNSIGNED_INT]; +pub const VALID_INDEX_TYPES: &[u32] = &[UNSIGNED_BYTE, UNSIGNED_SHORT, UNSIGNED_INT]; /// All valid accessor types. -pub const VALID_ACCESSOR_TYPES: &'static [&'static str] = +pub const VALID_ACCESSOR_TYPES: &[&str] = &["SCALAR", "VEC2", "VEC3", "VEC4", "MAT2", "MAT3", "MAT4"]; /// Contains data structures for sparse storage. diff --git a/gltf-json/src/animation.rs b/gltf-json/src/animation.rs index 4016ff0e..f928675c 100644 --- a/gltf-json/src/animation.rs +++ b/gltf-json/src/animation.rs @@ -6,11 +6,10 @@ use serde_derive::{Deserialize, Serialize}; use std::fmt; /// All valid animation interpolation algorithms. -pub const VALID_INTERPOLATIONS: &'static [&'static str] = &["LINEAR", "STEP", "CUBICSPLINE"]; +pub const VALID_INTERPOLATIONS: &[&str] = &["LINEAR", "STEP", "CUBICSPLINE"]; /// All valid animation property names. -pub const VALID_PROPERTIES: &'static [&'static str] = - &["translation", "rotation", "scale", "weights"]; +pub const VALID_PROPERTIES: &[&str] = &["translation", "rotation", "scale", "weights"]; /// Specifies an interpolation algorithm. #[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize)] diff --git a/gltf-json/src/buffer.rs b/gltf-json/src/buffer.rs index a5a6709b..a4b79fec 100644 --- a/gltf-json/src/buffer.rs +++ b/gltf-json/src/buffer.rs @@ -18,7 +18,7 @@ pub const MIN_BYTE_STRIDE: u32 = 4; pub const MAX_BYTE_STRIDE: u32 = 252; /// All valid GPU buffer targets. -pub const VALID_TARGETS: &'static [u32] = &[ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER]; +pub const VALID_TARGETS: &[u32] = &[ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER]; /// Specifies the target a GPU buffer should be bound to. #[derive(Clone, Copy, Debug, Eq, PartialEq)] diff --git a/gltf-json/src/camera.rs b/gltf-json/src/camera.rs index f2fdb28b..2b093b6c 100644 --- a/gltf-json/src/camera.rs +++ b/gltf-json/src/camera.rs @@ -6,7 +6,7 @@ use serde_derive::{Deserialize, Serialize}; use std::fmt; /// All valid camera types. -pub const VALID_CAMERA_TYPES: &'static [&'static str] = &["perspective", "orthographic"]; +pub const VALID_CAMERA_TYPES: &[&str] = &["perspective", "orthographic"]; /// Specifies the camera type. #[derive(Clone, Copy, Debug, Eq, PartialEq)] diff --git a/gltf-json/src/extensions/mod.rs b/gltf-json/src/extensions/mod.rs index 7367db1d..ac2f128e 100644 --- a/gltf-json/src/extensions/mod.rs +++ b/gltf-json/src/extensions/mod.rs @@ -37,7 +37,7 @@ pub mod texture; pub use self::root::Root; /// Names of glTF 2.0 extensions enabled by the user. -pub const ENABLED_EXTENSIONS: &'static [&'static str] = &[ +pub const ENABLED_EXTENSIONS: &[&str] = &[ #[cfg(feature = "KHR_lights_punctual")] "KHR_lights_punctual", #[cfg(feature = "KHR_materials_pbrSpecularGlossiness")] @@ -53,7 +53,7 @@ pub const ENABLED_EXTENSIONS: &'static [&'static str] = &[ ]; /// Names of glTF 2.0 extensions supported by the library. -pub const SUPPORTED_EXTENSIONS: &'static [&'static str] = &[ +pub const SUPPORTED_EXTENSIONS: &[&str] = &[ "KHR_lights_punctual", "KHR_materials_pbrSpecularGlossiness", "KHR_materials_unlit", diff --git a/gltf-json/src/extensions/scene.rs b/gltf-json/src/extensions/scene.rs index 953fe0ac..96c40a97 100644 --- a/gltf-json/src/extensions/scene.rs +++ b/gltf-json/src/extensions/scene.rs @@ -32,7 +32,7 @@ pub mod khr_lights_punctual { use std::fmt; /// All valid light types. - pub const VALID_TYPES: &'static [&'static str] = &["directional", "point", "spot"]; + pub const VALID_TYPES: &[&str] = &["directional", "point", "spot"]; #[derive(Clone, Debug, Deserialize, Serialize, Validate)] pub struct KhrLightsPunctual { @@ -151,7 +151,7 @@ pub mod khr_lights_punctual { } fn outer_cone_angle_default() -> f32 { - 0.7853981633974483 + std::f32::consts::FRAC_PI_4 } impl<'de> de::Deserialize<'de> for Checked { @@ -201,12 +201,9 @@ pub mod khr_lights_punctual { #[cfg(feature = "KHR_materials_variants")] pub mod khr_materials_variants { - use crate::validation::{Checked, Error, Validate}; - use crate::{Extras, Index, Path, Root}; - use gltf_derive::Validate; - use serde::{de, ser}; + use crate::validation::{Error, Validate}; + use crate::{Path, Root}; use serde_derive::{Deserialize, Serialize}; - use std::fmt; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Variant { diff --git a/gltf-json/src/image.rs b/gltf-json/src/image.rs index 9987964f..e1cd597c 100644 --- a/gltf-json/src/image.rs +++ b/gltf-json/src/image.rs @@ -4,7 +4,7 @@ use gltf_derive::Validate; use serde_derive::{Deserialize, Serialize}; /// All valid MIME types. -pub const VALID_MIME_TYPES: &'static [&'static str] = &["image/jpeg", "image/png"]; +pub const VALID_MIME_TYPES: &[&str] = &["image/jpeg", "image/png"]; /// Image data used to create a texture. #[derive(Clone, Debug, Deserialize, Serialize, Validate)] diff --git a/gltf-json/src/material.rs b/gltf-json/src/material.rs index 130990f4..8470b83a 100644 --- a/gltf-json/src/material.rs +++ b/gltf-json/src/material.rs @@ -6,7 +6,7 @@ use serde_derive::{Deserialize, Serialize}; use std::fmt; /// All valid alpha modes. -pub const VALID_ALPHA_MODES: &'static [&'static str] = &["OPAQUE", "MASK", "BLEND"]; +pub const VALID_ALPHA_MODES: &[&str] = &["OPAQUE", "MASK", "BLEND"]; /// The alpha rendering mode of a material. #[derive(Clone, Copy, Eq, PartialEq, Debug)] diff --git a/gltf-json/src/mesh.rs b/gltf-json/src/mesh.rs index 54df6585..bfa9523d 100644 --- a/gltf-json/src/mesh.rs +++ b/gltf-json/src/mesh.rs @@ -29,7 +29,7 @@ pub const TRIANGLE_STRIP: u32 = 5; pub const TRIANGLE_FAN: u32 = 6; /// All valid primitive rendering modes. -pub const VALID_MODES: &'static [u32] = &[ +pub const VALID_MODES: &[u32] = &[ POINTS, LINES, LINE_LOOP, @@ -40,7 +40,7 @@ pub const VALID_MODES: &'static [u32] = &[ ]; /// All valid semantic names for Morph targets. -pub const VALID_MORPH_TARGETS: &'static [&'static str] = &["POSITION", "NORMAL", "TANGENT"]; +pub const VALID_MORPH_TARGETS: &[&str] = &["POSITION", "NORMAL", "TANGENT"]; /// The type of primitives to render. #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)] @@ -308,7 +308,7 @@ impl Semantic { "POSITION" => Valid(Positions), "TANGENT" => Valid(Tangents), #[cfg(feature = "extras")] - _ if s.starts_with("_") => Valid(Extras(s[1..].to_string())), + _ if s.starts_with('_') => Valid(Extras(s[1..].to_string())), _ if s.starts_with("COLOR_") => match s["COLOR_".len()..].parse() { Ok(set) => Valid(Colors(set)), Err(_) => Invalid, diff --git a/gltf-json/src/root.rs b/gltf-json/src/root.rs index da6ac83c..7593e43a 100644 --- a/gltf-json/src/root.rs +++ b/gltf-json/src/root.rs @@ -127,6 +127,7 @@ impl Root { } /// Deserialize from a JSON string slice. + #[allow(clippy::should_implement_trait)] pub fn from_str(str_: &str) -> Result { serde_json::from_str(str_) } diff --git a/gltf-json/src/texture.rs b/gltf-json/src/texture.rs index 29a4e17b..d39268d6 100644 --- a/gltf-json/src/texture.rs +++ b/gltf-json/src/texture.rs @@ -33,10 +33,10 @@ pub const MIRRORED_REPEAT: u32 = 33_648; pub const REPEAT: u32 = 10_497; /// All valid magnification filters. -pub const VALID_MAG_FILTERS: &'static [u32] = &[NEAREST, LINEAR]; +pub const VALID_MAG_FILTERS: &[u32] = &[NEAREST, LINEAR]; /// All valid minification filters. -pub const VALID_MIN_FILTERS: &'static [u32] = &[ +pub const VALID_MIN_FILTERS: &[u32] = &[ NEAREST, LINEAR, NEAREST_MIPMAP_NEAREST, @@ -46,7 +46,7 @@ pub const VALID_MIN_FILTERS: &'static [u32] = &[ ]; /// All valid wrapping modes. -pub const VALID_WRAPPING_MODES: &'static [u32] = &[CLAMP_TO_EDGE, MIRRORED_REPEAT, REPEAT]; +pub const VALID_WRAPPING_MODES: &[u32] = &[CLAMP_TO_EDGE, MIRRORED_REPEAT, REPEAT]; /// Magnification filter. #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)] diff --git a/src/accessor/mod.rs b/src/accessor/mod.rs index 96c2a6c0..be7b9eb4 100644 --- a/src/accessor/mod.rs +++ b/src/accessor/mod.rs @@ -158,7 +158,7 @@ impl<'a> Accessor<'a> { #[cfg(feature = "names")] #[cfg_attr(docsrs, doc(cfg(feature = "names")))] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Specifies whether integer data values should be normalized. diff --git a/src/accessor/sparse.rs b/src/accessor/sparse.rs index 3b62f096..76e77964 100644 --- a/src/accessor/sparse.rs +++ b/src/accessor/sparse.rs @@ -25,10 +25,7 @@ pub struct Indices<'a> { impl<'a> Indices<'a> { /// Constructs `sparse::Indices`. pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Indices) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// Returns the buffer view containing the sparse indices. @@ -72,10 +69,7 @@ pub struct Sparse<'a> { impl<'a> Sparse<'a> { /// Constructs `Sparse`. pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Sparse) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// Returns the number of attributes encoded in this sparse accessor. @@ -114,10 +108,7 @@ pub struct Values<'a> { impl<'a> Values<'a> { /// Constructs `sparse::Values`. pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Values) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// Returns the buffer view containing the sparse values. diff --git a/src/accessor/util.rs b/src/accessor/util.rs index 42d9c8bd..a52b6542 100644 --- a/src/accessor/util.rs +++ b/src/accessor/util.rs @@ -28,15 +28,15 @@ impl<'a, T: Item> Iterator for Iter<'a, T> { fn next(&mut self) -> Option { match self { - &mut Iter::Standard(ref mut iter) => iter.next(), - &mut Iter::Sparse(ref mut iter) => iter.next(), + Iter::Standard(ref mut iter) => iter.next(), + Iter::Sparse(ref mut iter) => iter.next(), } } fn nth(&mut self, nth: usize) -> Option { match self { - &mut Iter::Standard(ref mut iter) => iter.nth(nth), - &mut Iter::Sparse(ref mut iter) => iter.nth(nth), + Iter::Standard(ref mut iter) => iter.nth(nth), + Iter::Sparse(ref mut iter) => iter.nth(nth), } } @@ -56,8 +56,8 @@ impl<'a, T: Item> Iterator for Iter<'a, T> { fn size_hint(&self) -> (usize, Option) { match self { - &Iter::Standard(ref iter) => iter.size_hint(), - &Iter::Sparse(ref iter) => iter.size_hint(), + Iter::Standard(ref iter) => iter.size_hint(), + Iter::Sparse(ref iter) => iter.size_hint(), } } } @@ -116,7 +116,7 @@ impl<'a, T: Item> SparseIter<'a, T> { SparseIter { base, indices: indices.peekable(), - values: values, + values, counter: 0, } } @@ -125,17 +125,13 @@ impl<'a, T: Item> SparseIter<'a, T> { impl<'a, T: Item> Iterator for SparseIter<'a, T> { type Item = T; fn next(&mut self) -> Option { - let next_base_value = self + let mut next_value = self .base .as_mut() .map(|iter| iter.next()) - .unwrap_or(Some(T::zero())); - if next_base_value.is_none() { - return None; - } + .unwrap_or_else(|| Some(T::zero()))?; - let mut next_value = next_base_value.unwrap(); - let next_sparse_index = self.indices.peek().clone(); + let next_sparse_index = self.indices.peek(); if let Some(index) = next_sparse_index { if *index == self.counter { self.indices.next(); // advance @@ -248,7 +244,7 @@ impl Item for [T; 3] { assert!(slice.len() >= 3 * mem::size_of::()); [ T::from_slice(slice), - T::from_slice(&slice[1 * mem::size_of::()..]), + T::from_slice(&slice[mem::size_of::()..]), T::from_slice(&slice[2 * mem::size_of::()..]), ] } @@ -262,7 +258,7 @@ impl Item for [T; 4] { assert!(slice.len() >= 4 * mem::size_of::()); [ T::from_slice(slice), - T::from_slice(&slice[1 * mem::size_of::()..]), + T::from_slice(&slice[mem::size_of::()..]), T::from_slice(&slice[2 * mem::size_of::()..]), T::from_slice(&slice[3 * mem::size_of::()..]), ] @@ -277,7 +273,7 @@ impl<'a, T: Item> ItemIter<'a, T> { pub fn new(slice: &'a [u8], stride: usize) -> Self { ItemIter { data: slice, - stride: stride, + stride, _phantom: PhantomData, } } diff --git a/src/animation/mod.rs b/src/animation/mod.rs index 7c4c0060..74429e44 100644 --- a/src/animation/mod.rs +++ b/src/animation/mod.rs @@ -68,9 +68,9 @@ impl<'a> Animation<'a> { json: &'a json::animation::Animation, ) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -97,7 +97,7 @@ impl<'a> Animation<'a> { /// Optional user-defined name for this object. #[cfg(feature = "names")] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Returns an `Iterator` over the animation samplers. @@ -115,10 +115,7 @@ impl<'a> Animation<'a> { impl<'a> Channel<'a> { /// Constructs a `Channel`. pub(crate) fn new(anim: Animation<'a>, json: &'a json::animation::Channel) -> Self { - Self { - anim: anim, - json: json, - } + Self { anim, json } } /// Returns the parent `Animation` struct. @@ -159,10 +156,7 @@ impl<'a> Channel<'a> { impl<'a> Target<'a> { /// Constructs a `Target`. pub(crate) fn new(anim: Animation<'a>, json: &'a json::animation::Target) -> Self { - Self { - anim: anim, - json: json, - } + Self { anim, json } } /// Returns the parent `Animation` struct. @@ -194,10 +188,7 @@ impl<'a> Target<'a> { impl<'a> Sampler<'a> { /// Constructs a `Sampler`. pub(crate) fn new(anim: Animation<'a>, json: &'a json::animation::Sampler) -> Self { - Self { - anim: anim, - json: json, - } + Self { anim, json } } /// Returns the parent `Animation` struct. diff --git a/src/binary.rs b/src/binary.rs index 4c1a4397..3c1c9fe5 100644 --- a/src/binary.rs +++ b/src/binary.rs @@ -118,7 +118,7 @@ fn align_to_multiple_of_four(n: &mut usize) { *n = (*n + 3) & !3; } -fn split_binary_gltf<'a>(mut data: &'a [u8]) -> Result<(&'a [u8], Option<&'a [u8]>), Error> { +fn split_binary_gltf(mut data: &[u8]) -> Result<(&[u8], Option<&[u8]>), Error> { let (json, mut data) = ChunkHeader::from_reader(&mut data) .and_then(|json_h| { if let ChunkType::Json = json_h.ty { @@ -142,7 +142,7 @@ fn split_binary_gltf<'a>(mut data: &'a [u8]) -> Result<(&'a [u8], Option<&'a [u8 // data.len(). .map(|json_h| data.split_at(json_h.length as usize))?; - let bin = if data.len() > 0 { + let bin = if !data.is_empty() { ChunkHeader::from_reader(&mut data) .and_then(|bin_h| { if let ChunkType::Bin = bin_h.ty { @@ -218,7 +218,7 @@ impl<'a> Glb<'a> { writer.write_u32::(length as u32)?; writer.write_all(&magic[..])?; - writer.write_all(&bin)?; + writer.write_all(bin)?; for _ in 0..padding { writer.write_u8(0)?; } diff --git a/src/buffer.rs b/src/buffer.rs index 02012e03..c8bd455d 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -9,6 +9,7 @@ pub use json::buffer::Target; #[derive(Clone, Debug)] pub struct Buffer<'a> { /// The parent `Document` struct. + #[allow(dead_code)] document: &'a Document, /// The corresponding JSON index. @@ -31,6 +32,7 @@ pub struct View<'a> { json: &'a json::buffer::View, /// The parent `Buffer`. + #[allow(dead_code)] parent: Buffer<'a>, } @@ -67,9 +69,9 @@ impl<'a> Buffer<'a> { json: &'a json::buffer::Buffer, ) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -80,7 +82,7 @@ impl<'a> Buffer<'a> { /// Returns the buffer data source. pub fn source(&self) -> Source<'a> { - if let Some(uri) = self.json.uri.as_ref().map(String::as_str) { + if let Some(uri) = self.json.uri.as_deref() { Source::Uri(uri) } else { Source::Bin @@ -96,7 +98,7 @@ impl<'a> Buffer<'a> { #[cfg(feature = "names")] #[cfg_attr(docsrs, doc(cfg(feature = "names")))] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Optional application specific data. @@ -158,7 +160,7 @@ impl<'a> View<'a> { #[cfg(feature = "names")] #[cfg_attr(docsrs, doc(cfg(feature = "names")))] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Optional target the buffer should be bound to. diff --git a/src/camera.rs b/src/camera.rs index a9ea1b28..55f7b776 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -28,6 +28,7 @@ pub struct Camera<'a> { #[derive(Clone, Debug)] pub struct Orthographic<'a> { /// The parent `Document` struct. + #[allow(dead_code)] document: &'a Document, /// The corresponding JSON struct. @@ -38,6 +39,7 @@ pub struct Orthographic<'a> { #[derive(Clone, Debug)] pub struct Perspective<'a> { /// The parent `Document` struct. + #[allow(dead_code)] document: &'a Document, /// The corresponding JSON struct. @@ -52,9 +54,9 @@ impl<'a> Camera<'a> { json: &'a json::camera::Camera, ) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -67,7 +69,7 @@ impl<'a> Camera<'a> { #[cfg(feature = "names")] #[cfg_attr(docsrs, doc(cfg(feature = "names")))] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Returns the camera's projection. @@ -93,10 +95,7 @@ impl<'a> Camera<'a> { impl<'a> Orthographic<'a> { /// Constructs a `Orthographic` camera projection. pub(crate) fn new(document: &'a Document, json: &'a json::camera::Orthographic) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// The horizontal magnification of the view. @@ -128,10 +127,7 @@ impl<'a> Orthographic<'a> { impl<'a> Perspective<'a> { /// Constructs a `Perspective` camera projection. pub(crate) fn new(document: &'a Document, json: &'a json::camera::Perspective) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// Aspect ratio of the field of view. diff --git a/src/image.rs b/src/image.rs index 33224768..409d0b68 100644 --- a/src/image.rs +++ b/src/image.rs @@ -97,9 +97,9 @@ impl<'a> Image<'a> { /// Constructs an `Image` from owned data. pub(crate) fn new(document: &'a Document, index: usize, json: &'a json::image::Image) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -112,7 +112,7 @@ impl<'a> Image<'a> { #[cfg(feature = "names")] #[cfg_attr(docsrs, doc(cfg(feature = "names")))] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Returns the image data source. diff --git a/src/import.rs b/src/import.rs index 15980dc2..cc02dd97 100644 --- a/src/import.rs +++ b/src/import.rs @@ -1,6 +1,5 @@ use crate::buffer; use crate::image; -use base64; use std::{fs, io}; use crate::{Document, Error, Gltf, Result}; @@ -29,22 +28,20 @@ enum Scheme<'a> { } impl<'a> Scheme<'a> { - fn parse<'s>(uri: &'s str) -> Scheme<'s> { - if uri.contains(":") { - if uri.starts_with("data:") { - let match0 = &uri["data:".len()..].split(";base64,").nth(0); - let match1 = &uri["data:".len()..].split(";base64,").nth(1); - if match1.is_some() { - Scheme::Data(Some(match0.unwrap()), match1.unwrap()) - } else if match0.is_some() { - Scheme::Data(None, match0.unwrap()) - } else { - Scheme::Unsupported + fn parse(uri: &str) -> Scheme<'_> { + if uri.contains(':') { + if let Some(rest) = uri.strip_prefix("data:") { + let mut it = rest.split(";base64,"); + + match (it.next(), it.next()) { + (match0_opt, Some(match1)) => Scheme::Data(match0_opt, match1), + (Some(match0), _) => Scheme::Data(None, match0), + _ => Scheme::Unsupported, } - } else if uri.starts_with("file://") { - Scheme::File(&uri["file://".len()..]) - } else if uri.starts_with("file:") { - Scheme::File(&uri["file:".len()..]) + } else if let Some(rest) = uri.strip_prefix("file://") { + Scheme::File(rest) + } else if let Some(rest) = uri.strip_prefix("file:") { + Scheme::File(rest) } else { Scheme::Unsupported } @@ -130,7 +127,7 @@ pub fn import_image_data( match Scheme::parse(uri) { Scheme::Data(Some(annoying_case), base64) => { let encoded_image = base64::decode(&base64).map_err(Error::Base64)?; - let encoded_format = match annoying_case.as_ref() { + let encoded_format = match annoying_case { "image/png" => Png, "image/jpeg" => Jpeg, _ => match guess_format(&encoded_image) { @@ -156,7 +153,7 @@ pub fn import_image_data( Some(format) => format, None => return Err(Error::UnsupportedImageEncoding), }, - None => match uri.rsplit(".").next() { + None => match uri.rsplit('.').next() { Some("png") => Png, Some("jpg") | Some("jpeg") => Jpeg, _ => match guess_format(&encoded_image) { @@ -201,7 +198,7 @@ fn import_impl(Gltf { document, blob }: Gltf, base: Option<&Path>) -> Result Result { - let base = path.parent().unwrap_or(Path::new("./")); + let base = path.parent().unwrap_or_else(|| Path::new("./")); let file = fs::File::open(path).map_err(Error::Io)?; let reader = io::BufReader::new(file); import_impl(Gltf::from_reader(reader)?, Some(base)) diff --git a/src/khr_lights_punctual.rs b/src/khr_lights_punctual.rs index 75ca22c9..2145673a 100644 --- a/src/khr_lights_punctual.rs +++ b/src/khr_lights_punctual.rs @@ -30,7 +30,7 @@ impl<'a> Light<'a> { /// Color of the light source. pub fn color(&self) -> [f32; 3] { - self.json.color.clone() + self.json.color } /// Returns the internal JSON index. @@ -41,7 +41,7 @@ impl<'a> Light<'a> { /// Optional user-defined name for this object. #[cfg(feature = "names")] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Optional application specific data. diff --git a/src/khr_materials_variants.rs b/src/khr_materials_variants.rs index 6d3b3718..682a03d9 100644 --- a/src/khr_materials_variants.rs +++ b/src/khr_materials_variants.rs @@ -1,5 +1,4 @@ use crate::{Document, Material}; -use gltf_json::Extras; /// A variant. pub struct Variant<'a> { @@ -8,6 +7,7 @@ pub struct Variant<'a> { document: &'a Document, /// The corresponding JSON index. + #[allow(dead_code)] index: usize, /// The corresponding JSON struct. diff --git a/src/lib.rs b/src/lib.rs index dcdeba21..1b8fd1dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -433,18 +433,20 @@ impl Document { #[cfg(feature = "KHR_lights_punctual")] #[cfg_attr(docsrs, doc(cfg(feature = "KHR_lights_punctual")))] pub fn lights(&self) -> Option { - if let Some(extensions) = self.0.extensions.as_ref() { - if let Some(khr_lights_punctual) = extensions.khr_lights_punctual.as_ref() { - Some(iter::Lights { - iter: khr_lights_punctual.lights.iter().enumerate(), - document: self, - }) - } else { - None - } - } else { - None - } + let iter = self + .0 + .extensions + .as_ref()? + .khr_lights_punctual + .as_ref()? + .lights + .iter() + .enumerate(); + + Some(iter::Lights { + iter, + document: self, + }) } /// Returns an `Iterator` that visits the variants of the glTF asset as defined by the diff --git a/src/material.rs b/src/material.rs index 1d97cfa7..cb277e65 100644 --- a/src/material.rs +++ b/src/material.rs @@ -27,16 +27,16 @@ impl<'a> Material<'a> { json: &'a json::material::Material, ) -> Self { Self { - document: document, + document, index: Some(index), - json: json, + json, } } /// Constructs the default `Material`. pub(crate) fn default(document: &'a Document) -> Self { Self { - document: document, + document, index: None, json: &DEFAULT_MATERIAL, } @@ -85,7 +85,7 @@ impl<'a> Material<'a> { #[cfg(feature = "names")] #[cfg_attr(docsrs, doc(cfg(feature = "names")))] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Parameter values that define the metallic-roughness material model from @@ -241,10 +241,7 @@ impl<'a> PbrMetallicRoughness<'a> { document: &'a Document, json: &'a json::material::PbrMetallicRoughness, ) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// Returns the material's base color factor. @@ -319,10 +316,7 @@ impl<'a> Transmission<'a> { document: &'a Document, json: &'a json::extensions::material::Transmission, ) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// Returns the material's transmission factor. @@ -365,10 +359,7 @@ impl<'a> Volume<'a> { document: &'a Document, json: &'a json::extensions::material::Volume, ) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// The thickness of the volume beneath the surface. The value is @@ -427,10 +418,7 @@ impl<'a> Specular<'a> { document: &'a Document, json: &'a json::extensions::material::Specular, ) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// The strength of the specular reflection. @@ -489,10 +477,7 @@ impl<'a> PbrSpecularGlossiness<'a> { document: &'a Document, json: &'a json::extensions::material::PbrSpecularGlossiness, ) -> Self { - Self { - document: document, - json: json, - } + Self { document, json } } /// Returns the material's base color factor. @@ -561,10 +546,7 @@ impl<'a> NormalTexture<'a> { texture: texture::Texture<'a>, json: &'a json::material::NormalTexture, ) -> Self { - Self { - texture: texture, - json: json, - } + Self { texture, json } } /// Returns the scalar multiplier applied to each normal vector of the texture. @@ -603,10 +585,7 @@ impl<'a> OcclusionTexture<'a> { texture: texture::Texture<'a>, json: &'a json::material::OcclusionTexture, ) -> Self { - Self { - texture: texture, - json: json, - } + Self { texture, json } } /// Returns the scalar multiplier controlling the amount of occlusion applied. diff --git a/src/math.rs b/src/math.rs index 5c13ba4c..f1ad60e8 100644 --- a/src/math.rs +++ b/src/math.rs @@ -19,7 +19,6 @@ use std::ops; #[cfg(test)] mod test { use super::*; - use approx; impl approx::AbsDiffEq for Vector4 { type Epsilon = f32; @@ -129,11 +128,6 @@ impl Vector3 { pub fn normalize(self) -> Vector3 { self * (1.0 / self.magnitude()) } - - #[cfg(test)] - pub fn from_array([x, y, z]: [f32; 3]) -> Self { - Self { x, y, z } - } } impl ops::Mul for Vector3 { @@ -206,6 +200,7 @@ pub struct Matrix3 { impl Matrix3 { #[rustfmt::skip] + #[allow(clippy::too_many_arguments)] pub fn new( c0r0: f32, c0r1: f32, c0r2: f32, c1r0: f32, c1r1: f32, c1r2: f32, @@ -240,6 +235,7 @@ pub struct Matrix4 { impl Matrix4 { #[rustfmt::skip] + #[allow(clippy::too_many_arguments)] pub fn new( c0r0: f32, c0r1: f32, c0r2: f32, c0r3: f32, c1r0: f32, c1r1: f32, c1r2: f32, c1r3: f32, diff --git a/src/mesh/iter.rs b/src/mesh/iter.rs index dbffd5e8..13809ff4 100644 --- a/src/mesh/iter.rs +++ b/src/mesh/iter.rs @@ -20,6 +20,7 @@ pub struct Attributes<'a> { pub(crate) document: &'a Document, /// The parent `Primitive` struct. + #[allow(dead_code)] pub(crate) prim: Primitive<'a>, /// The internal attribute iterator. diff --git a/src/mesh/mod.rs b/src/mesh/mod.rs index db899432..87031930 100644 --- a/src/mesh/mod.rs +++ b/src/mesh/mod.rs @@ -133,9 +133,9 @@ impl<'a> Mesh<'a> { /// Constructs a `Mesh`. pub(crate) fn new(document: &'a Document, index: usize, json: &'a json::mesh::Mesh) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -153,7 +153,7 @@ impl<'a> Mesh<'a> { #[cfg(feature = "names")] #[cfg_attr(docsrs, doc(cfg(feature = "names")))] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Defines the geometry to be renderered with a material. @@ -166,18 +166,14 @@ impl<'a> Mesh<'a> { /// Defines the weights to be applied to the morph targets. pub fn weights(&self) -> Option<&'a [f32]> { - self.json.weights.as_ref().map(Vec::as_slice) + self.json.weights.as_deref() } } impl<'a> Primitive<'a> { /// Constructs a `Primitive`. pub(crate) fn new(mesh: Mesh<'a>, index: usize, json: &'a json::mesh::Primitive) -> Self { - Self { - mesh: mesh, - index: index, - json: json, - } + Self { mesh, index, json } } /// Returns the bounds of the `POSITION` vertex attribute. diff --git a/src/scene/mod.rs b/src/scene/mod.rs index 785e3bea..63bfb578 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -115,9 +115,9 @@ impl<'a> Node<'a> { /// Constructs a `Node`. pub(crate) fn new(document: &'a Document, index: usize, json: &'a json::scene::Node) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -174,7 +174,7 @@ impl<'a> Node<'a> { /// Optional user-defined name for this object. #[cfg(feature = "names")] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Returns the node's transform. @@ -191,11 +191,7 @@ impl<'a> Node<'a> { } else { Transform::Decomposed { translation: self.json.translation.unwrap_or_else(|| [0.0, 0.0, 0.0]), - rotation: self - .json - .rotation - .unwrap_or_else(json::scene::UnitQuaternion::default) - .0, + rotation: self.json.rotation.unwrap_or_default().0, scale: self.json.scale.unwrap_or_else(|| [1.0, 1.0, 1.0]), } } @@ -211,7 +207,7 @@ impl<'a> Node<'a> { /// Returns the weights of the instantiated morph target. pub fn weights(&self) -> Option<&'a [f32]> { - self.json.weights.as_ref().map(Vec::as_slice) + self.json.weights.as_deref() } } @@ -219,9 +215,9 @@ impl<'a> Scene<'a> { /// Constructs a `Scene`. pub(crate) fn new(document: &'a Document, index: usize, json: &'a json::scene::Scene) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -238,7 +234,7 @@ impl<'a> Scene<'a> { /// Optional user-defined name for this object. #[cfg(feature = "names")] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Returns an `Iterator` that visits each root node of the scene. diff --git a/src/skin/mod.rs b/src/skin/mod.rs index 7710b8fa..f8a964b4 100644 --- a/src/skin/mod.rs +++ b/src/skin/mod.rs @@ -32,9 +32,9 @@ impl<'a> Skin<'a> { /// Constructs a `Skin`. pub(crate) fn new(document: &'a Document, index: usize, json: &'a json::skin::Skin) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -85,7 +85,7 @@ impl<'a> Skin<'a> { #[cfg(feature = "names")] #[cfg_attr(docsrs, doc(cfg(feature = "names")))] pub fn name(&self) -> Option<&'a str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Returns the node used as the skeleton root. When `None`, joints diff --git a/src/texture.rs b/src/texture.rs index d152ab63..14e49a43 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -51,16 +51,16 @@ impl<'a> Sampler<'a> { json: &'a json::texture::Sampler, ) -> Self { Self { - document: document, + document, index: Some(index), - json: json, + json, } } /// Constructs the default `Sampler`. pub(crate) fn default(document: &'a Document) -> Self { Self { - document: document, + document, index: None, json: &DEFAULT_SAMPLER, } @@ -86,7 +86,7 @@ impl<'a> Sampler<'a> { /// Optional user-defined name for this object. #[cfg(feature = "names")] pub fn name(&self) -> Option<&str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// `s` wrapping mode. @@ -113,9 +113,9 @@ impl<'a> Texture<'a> { json: &'a json::texture::Texture, ) -> Self { Self { - document: document, - index: index, - json: json, + document, + index, + json, } } @@ -127,7 +127,7 @@ impl<'a> Texture<'a> { /// Optional user-defined name for this object. #[cfg(feature = "names")] pub fn name(&self) -> Option<&str> { - self.json.name.as_ref().map(String::as_str) + self.json.name.as_deref() } /// Returns the sampler used by this texture. @@ -161,10 +161,7 @@ impl<'a> Texture<'a> { impl<'a> Info<'a> { /// Constructs a reference to a `Texture`. pub(crate) fn new(texture: Texture<'a>, json: &'a json::texture::Info) -> Self { - Self { - texture: texture, - json: json, - } + Self { texture, json } } /// The set index of the texture's `TEXCOORD` attribute. @@ -186,7 +183,7 @@ impl<'a> Info<'a> { .as_ref()? .texture_transform .as_ref() - .map(|x| TextureTransform::new(x)) + .map(TextureTransform::new) } /// Optional application specific data. diff --git a/tests/roundtrip_binary_gltf.rs b/tests/roundtrip_binary_gltf.rs index ba2b9979..ea56e1aa 100644 --- a/tests/roundtrip_binary_gltf.rs +++ b/tests/roundtrip_binary_gltf.rs @@ -74,7 +74,7 @@ fn test(path: &path::Path) -> Result<(), boxed::Box> { /// Return true if the test passes, and false otherwise. fn sparse_accessor_without_buffer_view_test() -> bool { let path = path::Path::new("tests/box_sparse.glb"); - if let Err(err) = test(&path) { + if let Err(err) = test(path) { println!("{:?}: error: {:?}", path, err); false } else { diff --git a/tests/test_wrapper.rs b/tests/test_wrapper.rs index e59108f5..8de7ccb7 100644 --- a/tests/test_wrapper.rs +++ b/tests/test_wrapper.rs @@ -11,8 +11,8 @@ fn test_accessor_bounds() { let mut buffer = vec![]; reader.read_to_end(&mut buffer).unwrap(); let gltf = gltf::Gltf::from_slice(&buffer).unwrap(); - let mesh = &gltf.meshes().nth(0).unwrap(); - let prim = mesh.primitives().nth(0).unwrap(); + let mesh = &gltf.meshes().next().unwrap(); + let prim = mesh.primitives().next().unwrap(); let bounds = prim.bounding_box(); assert_eq!( bounds,