From 85f557e99cd44e21c53f0055739549a75d799942 Mon Sep 17 00:00:00 2001 From: simensgreen Date: Mon, 19 Apr 2021 20:06:12 +0300 Subject: [PATCH 01/10] Added TryInto for VertexAttributeValues --- crates/bevy_render/src/mesh/mesh.rs | 80 +--- .../bevy_render/src/mesh/mesh/conversions.rs | 362 ++++++++++++++++++ 2 files changed, 364 insertions(+), 78 deletions(-) create mode 100644 crates/bevy_render/src/mesh/mesh/conversions.rs diff --git a/crates/bevy_render/src/mesh/mesh.rs b/crates/bevy_render/src/mesh/mesh.rs index aece08891876f..0258d6b51115f 100644 --- a/crates/bevy_render/src/mesh/mesh.rs +++ b/crates/bevy_render/src/mesh/mesh.rs @@ -1,3 +1,5 @@ +mod conversions; + use crate::{ pipeline::{IndexFormat, PrimitiveTopology, RenderPipelines, VertexFormat}, renderer::{BufferInfo, BufferUsage, RenderResourceContext, RenderResourceId}, @@ -107,84 +109,6 @@ impl From<&VertexAttributeValues> for VertexFormat { } } -impl From> for VertexAttributeValues { - fn from(vec: Vec) -> Self { - VertexAttributeValues::Float(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec) -> Self { - VertexAttributeValues::Int(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec) -> Self { - VertexAttributeValues::Uint(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[f32; 2]>) -> Self { - VertexAttributeValues::Float2(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[i32; 2]>) -> Self { - VertexAttributeValues::Int2(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[u32; 2]>) -> Self { - VertexAttributeValues::Uint2(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[f32; 3]>) -> Self { - VertexAttributeValues::Float3(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[i32; 3]>) -> Self { - VertexAttributeValues::Int3(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[u32; 3]>) -> Self { - VertexAttributeValues::Uint3(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[f32; 4]>) -> Self { - VertexAttributeValues::Float4(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[i32; 4]>) -> Self { - VertexAttributeValues::Int4(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[u32; 4]>) -> Self { - VertexAttributeValues::Uint4(vec) - } -} - -impl From> for VertexAttributeValues { - fn from(vec: Vec<[u8; 4]>) -> Self { - VertexAttributeValues::Uchar4Norm(vec) - } -} - /// An array of indices into the VertexAttributeValues for a mesh. /// /// It describes the order in which the vertex attributes should be joined into faces. diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs new file mode 100644 index 0000000000000..06fe8c022c425 --- /dev/null +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -0,0 +1,362 @@ +//! This implementations allows you +//! convert std::vec::Vec to VertexAttributeValues::T and back. +//! +//! # Examples +//! +//! ```rust +//! use std::convert::TryInto; +//! use bevy_render::mesh::VertexAttributeValues; +//! +//! // creating vector of values +//! let before = vec![[0_u32; 4]; 10]; +//! let values = VertexAttributeValues::from(before.clone()); +//! let after: Vec<[u32; 4]> = values.try_into().unwrap(); +//! +//! assert_eq!(before, after); +//! ``` + +use crate::mesh::VertexAttributeValues; +use std::convert::TryInto; + +const CANT_CONVERT: &str = "can't convert to "; + +impl From> for VertexAttributeValues { + fn from(vec: Vec) -> Self { + VertexAttributeValues::Float(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec) -> Self { + VertexAttributeValues::Int(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec) -> Self { + VertexAttributeValues::Uint(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[f32; 2]>) -> Self { + VertexAttributeValues::Float2(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[i32; 2]>) -> Self { + VertexAttributeValues::Int2(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[u32; 2]>) -> Self { + VertexAttributeValues::Uint2(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[f32; 3]>) -> Self { + VertexAttributeValues::Float3(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[i32; 3]>) -> Self { + VertexAttributeValues::Int3(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[u32; 3]>) -> Self { + VertexAttributeValues::Uint3(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[f32; 4]>) -> Self { + VertexAttributeValues::Float4(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[i32; 4]>) -> Self { + VertexAttributeValues::Int4(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[u32; 4]>) -> Self { + VertexAttributeValues::Uint4(vec) + } +} + +impl From> for VertexAttributeValues { + fn from(vec: Vec<[u8; 4]>) -> Self { + VertexAttributeValues::Uchar4Norm(vec) + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Uchar4Norm(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[u8; 4]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Uint4(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 4]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Int4(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 4]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Float4(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 4]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Uint3(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 3]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Int3(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 3]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Float3(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 3]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Uint2(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 2]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Int2(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 2]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Float2(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 2]>") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Uint(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Int(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec") + } + } +} + +impl TryInto> for VertexAttributeValues { + type Error = String; + + fn try_into(self) -> Result, Self::Error> { + match self { + VertexAttributeValues::Float(value) => Ok(value), + _ => Err(CANT_CONVERT.to_string() + "Vec") + } + } +} + +#[test] +fn f32() +{ + let buffer = vec![0.0; 10]; + let result: Vec = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn i32() +{ + let buffer = vec![0; 10]; + let result: Vec = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn u32() +{ + let buffer = vec![0_u32; 10]; + let result: Vec = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn f32_2() +{ + let buffer = vec![[0.0; 2]; 10]; + let result: Vec<[f32; 2]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn i32_2() +{ + let buffer = vec![[0; 2]; 10]; + let result: Vec<[i32; 2]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn u32_2() +{ + let buffer = vec![[0_u32; 2]; 10]; + let result: Vec<[u32; 2]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn f32_3() +{ + let buffer = vec![[0.0; 3]; 10]; + let result: Vec<[f32; 3]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn i32_3() +{ + let buffer = vec![[0; 3]; 10]; + let result: Vec<[i32; 3]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn u32_3() +{ + let buffer = vec![[0_u32; 3]; 10]; + let result: Vec<[u32; 3]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn f32_4() +{ + let buffer = vec![[0.0; 4]; 10]; + let result: Vec<[f32; 4]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn i32_4() +{ + let buffer = vec![[0; 4]; 10]; + let result: Vec<[i32; 4]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} + +#[test] +fn u32_4() +{ + let buffer = vec![[0_u32; 4]; 10]; + let result: Vec<[u32; 4]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); + let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); + assert_eq!(buffer, result); + assert!(error.is_err()); +} From ab1e5f78837ae3bf5f6ccff632b301f6d7e6eb39 Mon Sep 17 00:00:00 2001 From: simensgreen Date: Tue, 20 Apr 2021 12:23:15 +0300 Subject: [PATCH 02/10] TryInto converted to TryFrom --- .../bevy_render/src/mesh/mesh/conversions.rs | 79 ++++++++++--------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs index 06fe8c022c425..31c842994595a 100644 --- a/crates/bevy_render/src/mesh/mesh/conversions.rs +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -17,6 +17,7 @@ use crate::mesh::VertexAttributeValues; use std::convert::TryInto; +use std::convert::TryFrom; const CANT_CONVERT: &str = "can't convert to "; @@ -98,143 +99,143 @@ impl From> for VertexAttributeValues { } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[u8; 4]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Uchar4Norm(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[u8; 4]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[u32; 4]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Uint4(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 4]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[i32; 4]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Int4(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 4]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[f32; 4]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Float4(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 4]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[u32; 3]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Uint3(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 3]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[i32; 3]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Int3(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 3]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[f32; 3]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Float3(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 3]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[u32; 2]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Uint2(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 2]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[i32; 2]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Int2(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 2]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec<[f32; 2]> { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Float2(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 2]>") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Uint(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Int(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec") } } } -impl TryInto> for VertexAttributeValues { +impl TryFrom for Vec { type Error = String; - fn try_into(self) -> Result, Self::Error> { - match self { + fn try_from(value: VertexAttributeValues) -> Result { + match value { VertexAttributeValues::Float(value) => Ok(value), _ => Err(CANT_CONVERT.to_string() + "Vec") } From 444495b839913c9860cdd24fc67ec172985b9d7b Mon Sep 17 00:00:00 2001 From: simensgreen Date: Tue, 20 Apr 2021 12:40:36 +0300 Subject: [PATCH 03/10] Improve tests and docs --- .../bevy_render/src/mesh/mesh/conversions.rs | 133 ++++++++++++------ 1 file changed, 89 insertions(+), 44 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs index 31c842994595a..a42996d3edb09 100644 --- a/crates/bevy_render/src/mesh/mesh/conversions.rs +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -4,20 +4,29 @@ //! # Examples //! //! ```rust -//! use std::convert::TryInto; //! use bevy_render::mesh::VertexAttributeValues; +//! use std::convert::{ TryInto, TryFrom }; //! -//! // creating vector of values -//! let before = vec![[0_u32; 4]; 10]; -//! let values = VertexAttributeValues::from(before.clone()); -//! let after: Vec<[u32; 4]> = values.try_into().unwrap(); +//! // creating std::vec::Vec +//! let buffer = vec![[0_u32; 4]; 10]; //! -//! assert_eq!(before, after); +//! // converting std::vec::Vec to bevy_render::mesh::VertexAttributeValues +//! let values = VertexAttributeValues::from(buffer.clone()).clone(); +//! +//! // converting bevy_render::mesh::VertexAttributeValues to std::vec::Vec with two ways +//! let result_into: Vec<[u32; 4]> = values.clone().try_into().unwrap(); +//! let result_from: Vec<[u32; 4]> = Vec::try_from(values.clone()).unwrap(); +//! +//! // getting an error when trying to convert incorrectly +//! let error: Result, _> = values.try_into(); +//! +//! assert_eq!(buffer, result_into); +//! assert_eq!(buffer, result_from); +//! assert!(error.is_err()); //! ``` use crate::mesh::VertexAttributeValues; -use std::convert::TryInto; -use std::convert::TryFrom; +use std::convert::{ TryInto, TryFrom }; const CANT_CONVERT: &str = "can't convert to "; @@ -246,9 +255,12 @@ impl TryFrom for Vec { fn f32() { let buffer = vec![0.0; 10]; - let result: Vec = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec = values.clone().try_into().unwrap(); + let result_from: Vec = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -256,9 +268,12 @@ fn f32() fn i32() { let buffer = vec![0; 10]; - let result: Vec = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec = values.clone().try_into().unwrap(); + let result_from: Vec = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -266,9 +281,12 @@ fn i32() fn u32() { let buffer = vec![0_u32; 10]; - let result: Vec = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec = values.clone().try_into().unwrap(); + let result_from: Vec = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -276,9 +294,12 @@ fn u32() fn f32_2() { let buffer = vec![[0.0; 2]; 10]; - let result: Vec<[f32; 2]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[f32; 2]> = values.clone().try_into().unwrap(); + let result_from: Vec<[f32; 2]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -286,9 +307,12 @@ fn f32_2() fn i32_2() { let buffer = vec![[0; 2]; 10]; - let result: Vec<[i32; 2]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[i32; 2]> = values.clone().try_into().unwrap(); + let result_from: Vec<[i32; 2]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -296,9 +320,12 @@ fn i32_2() fn u32_2() { let buffer = vec![[0_u32; 2]; 10]; - let result: Vec<[u32; 2]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[u32; 2]> = values.clone().try_into().unwrap(); + let result_from: Vec<[u32; 2]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -306,9 +333,12 @@ fn u32_2() fn f32_3() { let buffer = vec![[0.0; 3]; 10]; - let result: Vec<[f32; 3]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[f32; 3]> = values.clone().try_into().unwrap(); + let result_from: Vec<[f32; 3]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -316,9 +346,12 @@ fn f32_3() fn i32_3() { let buffer = vec![[0; 3]; 10]; - let result: Vec<[i32; 3]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[i32; 3]> = values.clone().try_into().unwrap(); + let result_from: Vec<[i32; 3]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -326,9 +359,12 @@ fn i32_3() fn u32_3() { let buffer = vec![[0_u32; 3]; 10]; - let result: Vec<[u32; 3]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[u32; 3]> = values.clone().try_into().unwrap(); + let result_from: Vec<[u32; 3]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -336,9 +372,12 @@ fn u32_3() fn f32_4() { let buffer = vec![[0.0; 4]; 10]; - let result: Vec<[f32; 4]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[f32; 4]> = values.clone().try_into().unwrap(); + let result_from: Vec<[f32; 4]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -346,9 +385,12 @@ fn f32_4() fn i32_4() { let buffer = vec![[0; 4]; 10]; - let result: Vec<[i32; 4]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[i32; 4]> = values.clone().try_into().unwrap(); + let result_from: Vec<[i32; 4]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } @@ -356,8 +398,11 @@ fn i32_4() fn u32_4() { let buffer = vec![[0_u32; 4]; 10]; - let result: Vec<[u32; 4]> = VertexAttributeValues::from(buffer.clone()).try_into().unwrap(); - let error: Result, _> = VertexAttributeValues::from(buffer.clone()).try_into(); - assert_eq!(buffer, result); + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[u32; 4]> = values.clone().try_into().unwrap(); + let result_from: Vec<[u32; 4]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); assert!(error.is_err()); } From e7df24f10ec1bb9605138a206c1bfc5f35ea9164 Mon Sep 17 00:00:00 2001 From: simensgreen Date: Tue, 20 Apr 2021 13:46:18 +0300 Subject: [PATCH 04/10] Informative error messages --- .../bevy_render/src/mesh/mesh/conversions.rs | 101 +++++++++++++----- 1 file changed, 74 insertions(+), 27 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs index a42996d3edb09..902225344a0fe 100644 --- a/crates/bevy_render/src/mesh/mesh/conversions.rs +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -27,8 +27,39 @@ use crate::mesh::VertexAttributeValues; use std::convert::{ TryInto, TryFrom }; +use std::error::Error; +use std::fmt; -const CANT_CONVERT: &str = "can't convert to "; +#[derive(Debug, Clone)] +pub struct FromVertexAttributeError +{ + from: VertexAttributeValues, + into: &'static str +} + +impl Error for FromVertexAttributeError {} + +impl fmt::Display for FromVertexAttributeError +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let value_name = match self.from { + VertexAttributeValues::Float(..) => "VertexAttributeValues::Float", + VertexAttributeValues::Int(..) => "VertexAttributeValues::Int", + VertexAttributeValues::Uint(..) => "VertexAttributeValues::Uint", + VertexAttributeValues::Float2(..) => "VertexAttributeValues::Float2", + VertexAttributeValues::Int2(..) => "VertexAttributeValues::Int2", + VertexAttributeValues::Uint2(..) => "VertexAttributeValues::Uint2", + VertexAttributeValues::Float3(..) => "VertexAttributeValues::Float3", + VertexAttributeValues::Int3(..) => "VertexAttributeValues::Int3", + VertexAttributeValues::Uint3(..) => "VertexAttributeValues::Uint3", + VertexAttributeValues::Float4(..) => "VertexAttributeValues::Float4", + VertexAttributeValues::Int4(..) => "VertexAttributeValues::Int4", + VertexAttributeValues::Uint4(..) => "VertexAttributeValues::Uint4", + VertexAttributeValues::Uchar4Norm(..) => "VertexAttributeValues::Uchar4Norm", + }; + write!(f, "can't convert `{}` to `{}`", value_name, self.into) + } +} impl From> for VertexAttributeValues { fn from(vec: Vec) -> Self { @@ -109,144 +140,144 @@ impl From> for VertexAttributeValues { } impl TryFrom for Vec<[u8; 4]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uchar4Norm(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[u8; 4]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u8; 4]]>" }) } } } impl TryFrom for Vec<[u32; 4]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint4(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 4]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 4]]>" }) } } } impl TryFrom for Vec<[i32; 4]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int4(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 4]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 4]]>" }) } } } impl TryFrom for Vec<[f32; 4]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float4(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 4]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 4]]>" }) } } } impl TryFrom for Vec<[u32; 3]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint3(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 3]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 3]]>" }) } } } impl TryFrom for Vec<[i32; 3]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int3(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 3]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 3]]>" }) } } } impl TryFrom for Vec<[f32; 3]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float3(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 3]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 3]]>" }) } } } impl TryFrom for Vec<[u32; 2]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint2(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[u32; 2]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 2]]>" }) } } } impl TryFrom for Vec<[i32; 2]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int2(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[i32; 2]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 2]]>" }) } } } impl TryFrom for Vec<[f32; 2]> { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float2(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec<[f32; 2]>") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 2]]>" }) } } } impl TryFrom for Vec { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec" }) } } } impl TryFrom for Vec { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec" }) } } } impl TryFrom for Vec { - type Error = String; + type Error = FromVertexAttributeError; fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float(value) => Ok(value), - _ => Err(CANT_CONVERT.to_string() + "Vec") + _ => Err(FromVertexAttributeError{ from: value, into: "Vec" }) } } } @@ -406,3 +437,19 @@ fn u32_4() assert_eq!(buffer, result_from); assert!(error.is_err()); } + +#[test] +fn correct_message() +{ + let buffer = vec![[0_u32; 4]; 3]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let error_result: Result, _> = values.try_into(); + let error = match error_result + { + Ok(..) => unreachable!(), + Err(error) => error + }; + assert_eq!(format!("{}", error), "can't convert `VertexAttributeValues::Uint4` to `Vec`"); + assert_eq!(format!("{:?}", error), + "FromVertexAttributeError { from: Uint4([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]), into: \"Vec\" }"); +} From 49438a606bd0ef7aa6e93cb04252083d048de13d Mon Sep 17 00:00:00 2001 From: aloucks Date: Wed, 14 Apr 2021 23:21:53 +0000 Subject: [PATCH 05/10] Add additional vertex formats (#1878) - `Short2` - `Short2Norm` - `Ushort2` - `Ushort2Norm` - `Short4` - `Short4Norm` - `Ushort4` - `Ushort4Norm` - `Char2` - `Char2Norm` - `Uchar2` - `Uchar2Norm` - `Char4` - `Char4Norm` - `Uchar4` --- crates/bevy_render/src/mesh/mesh.rs | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/crates/bevy_render/src/mesh/mesh.rs b/crates/bevy_render/src/mesh/mesh.rs index 0258d6b51115f..9cab04b854374 100644 --- a/crates/bevy_render/src/mesh/mesh.rs +++ b/crates/bevy_render/src/mesh/mesh.rs @@ -38,6 +38,21 @@ pub enum VertexAttributeValues { Float4(Vec<[f32; 4]>), Int4(Vec<[i32; 4]>), Uint4(Vec<[u32; 4]>), + Short2(Vec<[i16; 2]>), + Short2Norm(Vec<[i16; 2]>), + Ushort2(Vec<[u16; 2]>), + Ushort2Norm(Vec<[u16; 2]>), + Short4(Vec<[i16; 4]>), + Short4Norm(Vec<[i16; 4]>), + Ushort4(Vec<[u16; 4]>), + Ushort4Norm(Vec<[u16; 4]>), + Char2(Vec<[i8; 2]>), + Char2Norm(Vec<[i8; 2]>), + Uchar2(Vec<[u8; 2]>), + Uchar2Norm(Vec<[u8; 2]>), + Char4(Vec<[i8; 4]>), + Char4Norm(Vec<[i8; 4]>), + Uchar4(Vec<[u8; 4]>), Uchar4Norm(Vec<[u8; 4]>), } @@ -58,6 +73,21 @@ impl VertexAttributeValues { VertexAttributeValues::Float4(ref values) => values.len(), VertexAttributeValues::Int4(ref values) => values.len(), VertexAttributeValues::Uint4(ref values) => values.len(), + VertexAttributeValues::Short2(ref values) => values.len(), + VertexAttributeValues::Short2Norm(ref values) => values.len(), + VertexAttributeValues::Ushort2(ref values) => values.len(), + VertexAttributeValues::Ushort2Norm(ref values) => values.len(), + VertexAttributeValues::Short4(ref values) => values.len(), + VertexAttributeValues::Short4Norm(ref values) => values.len(), + VertexAttributeValues::Ushort4(ref values) => values.len(), + VertexAttributeValues::Ushort4Norm(ref values) => values.len(), + VertexAttributeValues::Char2(ref values) => values.len(), + VertexAttributeValues::Char2Norm(ref values) => values.len(), + VertexAttributeValues::Uchar2(ref values) => values.len(), + VertexAttributeValues::Uchar2Norm(ref values) => values.len(), + VertexAttributeValues::Char4(ref values) => values.len(), + VertexAttributeValues::Char4Norm(ref values) => values.len(), + VertexAttributeValues::Uchar4(ref values) => values.len(), VertexAttributeValues::Uchar4Norm(ref values) => values.len(), } } @@ -84,6 +114,21 @@ impl VertexAttributeValues { VertexAttributeValues::Float4(values) => values.as_slice().as_bytes(), VertexAttributeValues::Int4(values) => values.as_slice().as_bytes(), VertexAttributeValues::Uint4(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Short2(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Short2Norm(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Ushort2(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Ushort2Norm(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Short4(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Short4Norm(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Ushort4(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Ushort4Norm(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Char2(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Char2Norm(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Uchar2(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Uchar2Norm(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Char4(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Char4Norm(values) => values.as_slice().as_bytes(), + VertexAttributeValues::Uchar4(values) => values.as_slice().as_bytes(), VertexAttributeValues::Uchar4Norm(values) => values.as_slice().as_bytes(), } } @@ -104,6 +149,21 @@ impl From<&VertexAttributeValues> for VertexFormat { VertexAttributeValues::Float4(_) => VertexFormat::Float4, VertexAttributeValues::Int4(_) => VertexFormat::Int4, VertexAttributeValues::Uint4(_) => VertexFormat::Uint4, + VertexAttributeValues::Short2(_) => VertexFormat::Short2, + VertexAttributeValues::Short2Norm(_) => VertexFormat::Short2Norm, + VertexAttributeValues::Ushort2(_) => VertexFormat::Ushort2, + VertexAttributeValues::Ushort2Norm(_) => VertexFormat::Ushort2Norm, + VertexAttributeValues::Short4(_) => VertexFormat::Short4, + VertexAttributeValues::Short4Norm(_) => VertexFormat::Short4Norm, + VertexAttributeValues::Ushort4(_) => VertexFormat::Ushort4, + VertexAttributeValues::Ushort4Norm(_) => VertexFormat::Ushort4Norm, + VertexAttributeValues::Char2(_) => VertexFormat::Char2, + VertexAttributeValues::Char2Norm(_) => VertexFormat::Char2Norm, + VertexAttributeValues::Uchar2(_) => VertexFormat::Uchar2, + VertexAttributeValues::Uchar2Norm(_) => VertexFormat::Uchar2Norm, + VertexAttributeValues::Char4(_) => VertexFormat::Char4, + VertexAttributeValues::Char4Norm(_) => VertexFormat::Char4Norm, + VertexAttributeValues::Uchar4(_) => VertexFormat::Uchar4, VertexAttributeValues::Uchar4Norm(_) => VertexFormat::Uchar4Norm, } } From aafead93552d2e87b7767682ac085703a04c8eb5 Mon Sep 17 00:00:00 2001 From: simensgreen Date: Tue, 20 Apr 2021 16:15:29 +0300 Subject: [PATCH 06/10] New implements after adding new vertex formats --- .../bevy_render/src/mesh/mesh/conversions.rs | 129 ++++++++++++++++-- 1 file changed, 116 insertions(+), 13 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs index 902225344a0fe..71889a4f36532 100644 --- a/crates/bevy_render/src/mesh/mesh/conversions.rs +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -26,9 +26,12 @@ //! ``` use crate::mesh::VertexAttributeValues; -use std::convert::{ TryInto, TryFrom }; -use std::error::Error; -use std::fmt; +use std:: +{ + fmt, + error::Error, + convert::{ TryInto, TryFrom }, +}; #[derive(Debug, Clone)] pub struct FromVertexAttributeError @@ -55,6 +58,21 @@ impl fmt::Display for FromVertexAttributeError VertexAttributeValues::Float4(..) => "VertexAttributeValues::Float4", VertexAttributeValues::Int4(..) => "VertexAttributeValues::Int4", VertexAttributeValues::Uint4(..) => "VertexAttributeValues::Uint4", + VertexAttributeValues::Short2(..) => "VertexAttributeValues::Short2", + VertexAttributeValues::Short2Norm(..) => "VertexAttributeValues::Short2Norm", + VertexAttributeValues::Ushort2(..) => "VertexAttributeValues::Ushort2", + VertexAttributeValues::Ushort2Norm(..) => "VertexAttributeValues::Ushort2Norm", + VertexAttributeValues::Short4(..) => "VertexAttributeValues::Short4", + VertexAttributeValues::Short4Norm(..) => "VertexAttributeValues::Short4Norm", + VertexAttributeValues::Ushort4(..) => "VertexAttributeValues::Ushort4", + VertexAttributeValues::Ushort4Norm(..) => "VertexAttributeValues::Ushort4Norm", + VertexAttributeValues::Char2(..) => "VertexAttributeValues::Char2", + VertexAttributeValues::Char2Norm(..) => "VertexAttributeValues::Char2Norm", + VertexAttributeValues::Uchar2(..) => "VertexAttributeValues::Uchar2", + VertexAttributeValues::Uchar2Norm(..) => "VertexAttributeValues::Uchar2Norm", + VertexAttributeValues::Char4(..) => "VertexAttributeValues::Char4", + VertexAttributeValues::Char4Norm(..) => "VertexAttributeValues::Char4Norm", + VertexAttributeValues::Uchar4(..) => "VertexAttributeValues::Uchar4", VertexAttributeValues::Uchar4Norm(..) => "VertexAttributeValues::Uchar4Norm", }; write!(f, "can't convert `{}` to `{}`", value_name, self.into) @@ -144,8 +162,93 @@ impl TryFrom for Vec<[u8; 4]> { fn try_from(value: VertexAttributeValues) -> Result { match value { + VertexAttributeValues::Uchar4(value) => Ok(value), VertexAttributeValues::Uchar4Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u8; 4]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u8; 4]>" }) + } + } +} + +impl TryFrom for Vec<[i8; 4]> { + type Error = FromVertexAttributeError; + + fn try_from(value: VertexAttributeValues) -> Result { + match value { + VertexAttributeValues::Char4(value) => Ok(value), + VertexAttributeValues::Char4Norm(value) => Ok(value), + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i8; 4]>" }) + } + } +} + +impl TryFrom for Vec<[u8; 2]> { + type Error = FromVertexAttributeError; + + fn try_from(value: VertexAttributeValues) -> Result { + match value { + VertexAttributeValues::Uchar2(value) => Ok(value), + VertexAttributeValues::Uchar2Norm(value) => Ok(value), + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u8; 2]>" }) + } + } +} + +impl TryFrom for Vec<[i8; 2]> { + type Error = FromVertexAttributeError; + + fn try_from(value: VertexAttributeValues) -> Result { + match value { + VertexAttributeValues::Char2(value) => Ok(value), + VertexAttributeValues::Char2Norm(value) => Ok(value), + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i8; 2]>" }) + } + } +} + +impl TryFrom for Vec<[i16; 4]> { + type Error = FromVertexAttributeError; + + fn try_from(value: VertexAttributeValues) -> Result { + match value { + VertexAttributeValues::Short4(value) => Ok(value), + VertexAttributeValues::Short4Norm(value) => Ok(value), + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i16; 4]>" }) + } + } +} + +impl TryFrom for Vec<[u16; 4]> { + type Error = FromVertexAttributeError; + + fn try_from(value: VertexAttributeValues) -> Result { + match value { + VertexAttributeValues::Ushort4(value) => Ok(value), + VertexAttributeValues::Ushort4Norm(value) => Ok(value), + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u16; 4]>" }) + } + } +} + +impl TryFrom for Vec<[u16; 2]> { + type Error = FromVertexAttributeError; + + fn try_from(value: VertexAttributeValues) -> Result { + match value { + VertexAttributeValues::Ushort2(value) => Ok(value), + VertexAttributeValues::Ushort2Norm(value) => Ok(value), + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i16; 2]>" }) + } + } +} + +impl TryFrom for Vec<[i16; 2]> { + type Error = FromVertexAttributeError; + + fn try_from(value: VertexAttributeValues) -> Result { + match value { + VertexAttributeValues::Short2(value) => Ok(value), + VertexAttributeValues::Short2Norm(value) => Ok(value), + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i16; 2]>" }) } } } @@ -156,7 +259,7 @@ impl TryFrom for Vec<[u32; 4]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint4(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 4]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 4]>" }) } } } @@ -167,7 +270,7 @@ impl TryFrom for Vec<[i32; 4]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int4(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 4]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 4]>" }) } } } @@ -178,7 +281,7 @@ impl TryFrom for Vec<[f32; 4]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float4(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 4]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 4]>" }) } } } @@ -189,7 +292,7 @@ impl TryFrom for Vec<[u32; 3]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint3(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 3]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 3]>" }) } } } @@ -200,7 +303,7 @@ impl TryFrom for Vec<[i32; 3]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int3(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 3]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 3]>" }) } } } @@ -211,7 +314,7 @@ impl TryFrom for Vec<[f32; 3]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float3(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 3]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 3]>" }) } } } @@ -222,7 +325,7 @@ impl TryFrom for Vec<[u32; 2]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint2(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 2]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 2]>" }) } } } @@ -233,7 +336,7 @@ impl TryFrom for Vec<[i32; 2]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int2(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 2]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 2]>" }) } } } @@ -244,7 +347,7 @@ impl TryFrom for Vec<[f32; 2]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float2(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 2]]>" }) + _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 2]>" }) } } } From 7fac3fe527893ff24596569a54374f614eaafbe0 Mon Sep 17 00:00:00 2001 From: simens_green <34134129+simensgreen@users.noreply.github.com> Date: Tue, 20 Apr 2021 16:49:17 +0300 Subject: [PATCH 07/10] =?UTF-8?q?=D0=A1orrection=20of=20a=20mistake=20in?= =?UTF-8?q?=20a=20word?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alice Cecile --- crates/bevy_render/src/mesh/mesh/conversions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs index 71889a4f36532..fd64eae4cb74a 100644 --- a/crates/bevy_render/src/mesh/mesh/conversions.rs +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -1,4 +1,4 @@ -//! This implementations allows you +//! These implementations allows you //! convert std::vec::Vec to VertexAttributeValues::T and back. //! //! # Examples From bd993f8c941dfbe1ac29ffd1226dfdc462fbdbdb Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 20 Apr 2021 13:29:58 -0700 Subject: [PATCH 08/10] fmt --- .../bevy_render/src/mesh/mesh/conversions.rs | 448 ++++++++++-------- 1 file changed, 248 insertions(+), 200 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs index fd64eae4cb74a..d849ccb64dfa7 100644 --- a/crates/bevy_render/src/mesh/mesh/conversions.rs +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -26,24 +26,17 @@ //! ``` use crate::mesh::VertexAttributeValues; -use std:: -{ - fmt, - error::Error, - convert::{ TryInto, TryFrom }, -}; +use std::{convert::TryFrom, error::Error, fmt}; #[derive(Debug, Clone)] -pub struct FromVertexAttributeError -{ +pub struct FromVertexAttributeError { from: VertexAttributeValues, - into: &'static str + into: &'static str, } impl Error for FromVertexAttributeError {} -impl fmt::Display for FromVertexAttributeError -{ +impl fmt::Display for FromVertexAttributeError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let value_name = match self.from { VertexAttributeValues::Float(..) => "VertexAttributeValues::Float", @@ -164,7 +157,10 @@ impl TryFrom for Vec<[u8; 4]> { match value { VertexAttributeValues::Uchar4(value) => Ok(value), VertexAttributeValues::Uchar4Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u8; 4]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[u8; 4]>", + }), } } } @@ -176,7 +172,10 @@ impl TryFrom for Vec<[i8; 4]> { match value { VertexAttributeValues::Char4(value) => Ok(value), VertexAttributeValues::Char4Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i8; 4]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[i8; 4]>", + }), } } } @@ -188,7 +187,10 @@ impl TryFrom for Vec<[u8; 2]> { match value { VertexAttributeValues::Uchar2(value) => Ok(value), VertexAttributeValues::Uchar2Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u8; 2]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[u8; 2]>", + }), } } } @@ -200,7 +202,10 @@ impl TryFrom for Vec<[i8; 2]> { match value { VertexAttributeValues::Char2(value) => Ok(value), VertexAttributeValues::Char2Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i8; 2]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[i8; 2]>", + }), } } } @@ -212,7 +217,10 @@ impl TryFrom for Vec<[i16; 4]> { match value { VertexAttributeValues::Short4(value) => Ok(value), VertexAttributeValues::Short4Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i16; 4]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[i16; 4]>", + }), } } } @@ -224,7 +232,10 @@ impl TryFrom for Vec<[u16; 4]> { match value { VertexAttributeValues::Ushort4(value) => Ok(value), VertexAttributeValues::Ushort4Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u16; 4]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[u16; 4]>", + }), } } } @@ -236,7 +247,10 @@ impl TryFrom for Vec<[u16; 2]> { match value { VertexAttributeValues::Ushort2(value) => Ok(value), VertexAttributeValues::Ushort2Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i16; 2]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[i16; 2]>", + }), } } } @@ -248,7 +262,10 @@ impl TryFrom for Vec<[i16; 2]> { match value { VertexAttributeValues::Short2(value) => Ok(value), VertexAttributeValues::Short2Norm(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i16; 2]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[i16; 2]>", + }), } } } @@ -259,7 +276,10 @@ impl TryFrom for Vec<[u32; 4]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint4(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 4]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[u32; 4]>", + }), } } } @@ -270,7 +290,10 @@ impl TryFrom for Vec<[i32; 4]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int4(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 4]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[i32; 4]>", + }), } } } @@ -281,7 +304,10 @@ impl TryFrom for Vec<[f32; 4]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float4(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 4]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[f32; 4]>", + }), } } } @@ -292,7 +318,10 @@ impl TryFrom for Vec<[u32; 3]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint3(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 3]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[u32; 3]>", + }), } } } @@ -303,7 +332,10 @@ impl TryFrom for Vec<[i32; 3]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int3(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 3]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[i32; 3]>", + }), } } } @@ -314,7 +346,10 @@ impl TryFrom for Vec<[f32; 3]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float3(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 3]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[f32; 3]>", + }), } } } @@ -325,7 +360,10 @@ impl TryFrom for Vec<[u32; 2]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint2(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[u32; 2]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[u32; 2]>", + }), } } } @@ -336,7 +374,10 @@ impl TryFrom for Vec<[i32; 2]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int2(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[i32; 2]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[i32; 2]>", + }), } } } @@ -347,7 +388,10 @@ impl TryFrom for Vec<[f32; 2]> { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float2(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec<[f32; 2]>" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec<[f32; 2]>", + }), } } } @@ -358,7 +402,10 @@ impl TryFrom for Vec { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Uint(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec", + }), } } } @@ -369,7 +416,10 @@ impl TryFrom for Vec { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Int(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec", + }), } } } @@ -380,179 +430,177 @@ impl TryFrom for Vec { fn try_from(value: VertexAttributeValues) -> Result { match value { VertexAttributeValues::Float(value) => Ok(value), - _ => Err(FromVertexAttributeError{ from: value, into: "Vec" }) + _ => Err(FromVertexAttributeError { + from: value, + into: "Vec", + }), } } } -#[test] -fn f32() -{ - let buffer = vec![0.0; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec = values.clone().try_into().unwrap(); - let result_from: Vec = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn i32() -{ - let buffer = vec![0; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec = values.clone().try_into().unwrap(); - let result_from: Vec = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn u32() -{ - let buffer = vec![0_u32; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec = values.clone().try_into().unwrap(); - let result_from: Vec = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn f32_2() -{ - let buffer = vec![[0.0; 2]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[f32; 2]> = values.clone().try_into().unwrap(); - let result_from: Vec<[f32; 2]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn i32_2() -{ - let buffer = vec![[0; 2]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[i32; 2]> = values.clone().try_into().unwrap(); - let result_from: Vec<[i32; 2]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn u32_2() -{ - let buffer = vec![[0_u32; 2]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[u32; 2]> = values.clone().try_into().unwrap(); - let result_from: Vec<[u32; 2]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn f32_3() -{ - let buffer = vec![[0.0; 3]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[f32; 3]> = values.clone().try_into().unwrap(); - let result_from: Vec<[f32; 3]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn i32_3() -{ - let buffer = vec![[0; 3]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[i32; 3]> = values.clone().try_into().unwrap(); - let result_from: Vec<[i32; 3]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn u32_3() -{ - let buffer = vec![[0_u32; 3]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[u32; 3]> = values.clone().try_into().unwrap(); - let result_from: Vec<[u32; 3]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn f32_4() -{ - let buffer = vec![[0.0; 4]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[f32; 4]> = values.clone().try_into().unwrap(); - let result_from: Vec<[f32; 4]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn i32_4() -{ - let buffer = vec![[0; 4]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[i32; 4]> = values.clone().try_into().unwrap(); - let result_from: Vec<[i32; 4]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn u32_4() -{ - let buffer = vec![[0_u32; 4]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let result_into: Vec<[u32; 4]> = values.clone().try_into().unwrap(); - let result_from: Vec<[u32; 4]> = Vec::try_from(values.clone()).unwrap(); - let error: Result, _> = values.try_into(); - assert_eq!(buffer, result_into); - assert_eq!(buffer, result_from); - assert!(error.is_err()); -} - -#[test] -fn correct_message() -{ - let buffer = vec![[0_u32; 4]; 3]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); - let error_result: Result, _> = values.try_into(); - let error = match error_result - { - Ok(..) => unreachable!(), - Err(error) => error - }; - assert_eq!(format!("{}", error), "can't convert `VertexAttributeValues::Uint4` to `Vec`"); - assert_eq!(format!("{:?}", error), +#[cfg(test)] +mod tests { + use super::VertexAttributeValues; + use std::convert::{TryFrom, TryInto}; + + #[test] + fn f32() { + let buffer = vec![0.0; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec = values.clone().try_into().unwrap(); + let result_from: Vec = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn i32() { + let buffer = vec![0; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec = values.clone().try_into().unwrap(); + let result_from: Vec = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn u32() { + let buffer = vec![0_u32; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec = values.clone().try_into().unwrap(); + let result_from: Vec = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn f32_2() { + let buffer = vec![[0.0; 2]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[f32; 2]> = values.clone().try_into().unwrap(); + let result_from: Vec<[f32; 2]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn i32_2() { + let buffer = vec![[0; 2]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[i32; 2]> = values.clone().try_into().unwrap(); + let result_from: Vec<[i32; 2]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn u32_2() { + let buffer = vec![[0_u32; 2]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[u32; 2]> = values.clone().try_into().unwrap(); + let result_from: Vec<[u32; 2]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn f32_3() { + let buffer = vec![[0.0; 3]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[f32; 3]> = values.clone().try_into().unwrap(); + let result_from: Vec<[f32; 3]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn i32_3() { + let buffer = vec![[0; 3]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[i32; 3]> = values.clone().try_into().unwrap(); + let result_from: Vec<[i32; 3]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn u32_3() { + let buffer = vec![[0_u32; 3]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[u32; 3]> = values.clone().try_into().unwrap(); + let result_from: Vec<[u32; 3]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn f32_4() { + let buffer = vec![[0.0; 4]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[f32; 4]> = values.clone().try_into().unwrap(); + let result_from: Vec<[f32; 4]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn i32_4() { + let buffer = vec![[0; 4]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[i32; 4]> = values.clone().try_into().unwrap(); + let result_from: Vec<[i32; 4]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn u32_4() { + let buffer = vec![[0_u32; 4]; 10]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let result_into: Vec<[u32; 4]> = values.clone().try_into().unwrap(); + let result_from: Vec<[u32; 4]> = Vec::try_from(values.clone()).unwrap(); + let error: Result, _> = values.try_into(); + assert_eq!(buffer, result_into); + assert_eq!(buffer, result_from); + assert!(error.is_err()); + } + + #[test] + fn correct_message() { + let buffer = vec![[0_u32; 4]; 3]; + let values = VertexAttributeValues::from(buffer.clone()).clone(); + let error_result: Result, _> = values.try_into(); + let error = match error_result { + Ok(..) => unreachable!(), + Err(error) => error, + }; + assert_eq!( + format!("{}", error), + "can't convert `VertexAttributeValues::Uint4` to `Vec`" + ); + assert_eq!(format!("{:?}", error), "FromVertexAttributeError { from: Uint4([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]), into: \"Vec\" }"); + } } From a32ad7b4d3b160adb4615ebf8897bc87b60e6232 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 20 Apr 2021 13:30:33 -0700 Subject: [PATCH 09/10] Update crates/bevy_render/src/mesh/mesh/conversions.rs Co-authored-by: MinerSebas <66798382+MinerSebas@users.noreply.github.com> --- crates/bevy_render/src/mesh/mesh/conversions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs index d849ccb64dfa7..6eb1e3c1bc59e 100644 --- a/crates/bevy_render/src/mesh/mesh/conversions.rs +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -1,4 +1,4 @@ -//! These implementations allows you +//! These implementations allow you to //! convert std::vec::Vec to VertexAttributeValues::T and back. //! //! # Examples From 10742025d2eec89f00d691d725f7aab63ef7eac8 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 20 Apr 2021 13:40:12 -0700 Subject: [PATCH 10/10] clippy --- .../bevy_render/src/mesh/mesh/conversions.rs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/conversions.rs b/crates/bevy_render/src/mesh/mesh/conversions.rs index 6eb1e3c1bc59e..1326cd27fdfdd 100644 --- a/crates/bevy_render/src/mesh/mesh/conversions.rs +++ b/crates/bevy_render/src/mesh/mesh/conversions.rs @@ -11,7 +11,7 @@ //! let buffer = vec![[0_u32; 4]; 10]; //! //! // converting std::vec::Vec to bevy_render::mesh::VertexAttributeValues -//! let values = VertexAttributeValues::from(buffer.clone()).clone(); +//! let values = VertexAttributeValues::from(buffer.clone()); //! //! // converting bevy_render::mesh::VertexAttributeValues to std::vec::Vec with two ways //! let result_into: Vec<[u32; 4]> = values.clone().try_into().unwrap(); @@ -446,7 +446,7 @@ mod tests { #[test] fn f32() { let buffer = vec![0.0; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec = values.clone().try_into().unwrap(); let result_from: Vec = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -458,7 +458,7 @@ mod tests { #[test] fn i32() { let buffer = vec![0; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec = values.clone().try_into().unwrap(); let result_from: Vec = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -470,7 +470,7 @@ mod tests { #[test] fn u32() { let buffer = vec![0_u32; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec = values.clone().try_into().unwrap(); let result_from: Vec = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -482,7 +482,7 @@ mod tests { #[test] fn f32_2() { let buffer = vec![[0.0; 2]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[f32; 2]> = values.clone().try_into().unwrap(); let result_from: Vec<[f32; 2]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -494,7 +494,7 @@ mod tests { #[test] fn i32_2() { let buffer = vec![[0; 2]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[i32; 2]> = values.clone().try_into().unwrap(); let result_from: Vec<[i32; 2]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -506,7 +506,7 @@ mod tests { #[test] fn u32_2() { let buffer = vec![[0_u32; 2]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[u32; 2]> = values.clone().try_into().unwrap(); let result_from: Vec<[u32; 2]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -518,7 +518,7 @@ mod tests { #[test] fn f32_3() { let buffer = vec![[0.0; 3]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[f32; 3]> = values.clone().try_into().unwrap(); let result_from: Vec<[f32; 3]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -530,7 +530,7 @@ mod tests { #[test] fn i32_3() { let buffer = vec![[0; 3]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[i32; 3]> = values.clone().try_into().unwrap(); let result_from: Vec<[i32; 3]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -542,7 +542,7 @@ mod tests { #[test] fn u32_3() { let buffer = vec![[0_u32; 3]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[u32; 3]> = values.clone().try_into().unwrap(); let result_from: Vec<[u32; 3]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -554,7 +554,7 @@ mod tests { #[test] fn f32_4() { let buffer = vec![[0.0; 4]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[f32; 4]> = values.clone().try_into().unwrap(); let result_from: Vec<[f32; 4]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -566,7 +566,7 @@ mod tests { #[test] fn i32_4() { let buffer = vec![[0; 4]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[i32; 4]> = values.clone().try_into().unwrap(); let result_from: Vec<[i32; 4]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -578,7 +578,7 @@ mod tests { #[test] fn u32_4() { let buffer = vec![[0_u32; 4]; 10]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer.clone()); let result_into: Vec<[u32; 4]> = values.clone().try_into().unwrap(); let result_from: Vec<[u32; 4]> = Vec::try_from(values.clone()).unwrap(); let error: Result, _> = values.try_into(); @@ -590,7 +590,7 @@ mod tests { #[test] fn correct_message() { let buffer = vec![[0_u32; 4]; 3]; - let values = VertexAttributeValues::from(buffer.clone()).clone(); + let values = VertexAttributeValues::from(buffer); let error_result: Result, _> = values.try_into(); let error = match error_result { Ok(..) => unreachable!(),