From 02d0a3b67a62d41a08d168685d69e8006af41cc0 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 21 Oct 2024 00:16:50 +0100 Subject: [PATCH] Better to/from slice operations --- codegen/templates/vec.rs.tera | 5 ++--- src/f32/coresimd/vec3a.rs | 5 ++--- src/f32/coresimd/vec4.rs | 6 ++---- src/f32/neon/vec3a.rs | 5 ++--- src/f32/neon/vec4.rs | 1 + src/f32/scalar/vec3a.rs | 5 ++--- src/f32/scalar/vec4.rs | 6 ++---- src/f32/sse2/vec3a.rs | 5 ++--- src/f32/sse2/vec4.rs | 1 + src/f32/vec2.rs | 4 ++-- src/f32/vec3.rs | 5 ++--- src/f32/wasm32/vec3a.rs | 5 ++--- src/f32/wasm32/vec4.rs | 6 ++---- src/f64/dvec2.rs | 4 ++-- src/f64/dvec3.rs | 5 ++--- src/f64/dvec4.rs | 6 ++---- src/i16/i16vec2.rs | 4 ++-- src/i16/i16vec3.rs | 5 ++--- src/i16/i16vec4.rs | 6 ++---- src/i32/ivec2.rs | 4 ++-- src/i32/ivec3.rs | 5 ++--- src/i32/ivec4.rs | 6 ++---- src/i64/i64vec2.rs | 4 ++-- src/i64/i64vec3.rs | 5 ++--- src/i64/i64vec4.rs | 6 ++---- src/u16/u16vec2.rs | 4 ++-- src/u16/u16vec3.rs | 5 ++--- src/u16/u16vec4.rs | 6 ++---- src/u32/uvec2.rs | 4 ++-- src/u32/uvec3.rs | 5 ++--- src/u32/uvec4.rs | 6 ++---- src/u64/u64vec2.rs | 4 ++-- src/u64/u64vec3.rs | 5 ++--- src/u64/u64vec4.rs | 6 ++---- 34 files changed, 66 insertions(+), 98 deletions(-) diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index 7d35cdca..f62ce7d3 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -483,6 +483,7 @@ impl {{ self_t }} { #[inline] #[must_use] pub const fn from_slice(slice: &[{{ scalar_t }}]) -> Self { + assert!(slice.len() >= {{ dim }}); Self::new( {% for c in components %} slice[{{ loop.index0 }}], @@ -504,9 +505,7 @@ impl {{ self_t }} { assert!(slice.len() >= 4); unsafe { vst1q_f32(slice.as_mut_ptr(), self.0); } {% else %} - {% for c in components %} - slice[{{ loop.index0 }}] = self.{{ c }}; - {%- endfor %} + slice.copy_from_slice(&self.to_array()); {% endif %} } diff --git a/src/f32/coresimd/vec3a.rs b/src/f32/coresimd/vec3a.rs index 790e755e..64809362 100644 --- a/src/f32/coresimd/vec3a.rs +++ b/src/f32/coresimd/vec3a.rs @@ -132,6 +132,7 @@ impl Vec3A { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -142,9 +143,7 @@ impl Vec3A { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`. diff --git a/src/f32/coresimd/vec4.rs b/src/f32/coresimd/vec4.rs index 9eee8f88..2976a30e 100644 --- a/src/f32/coresimd/vec4.rs +++ b/src/f32/coresimd/vec4.rs @@ -134,6 +134,7 @@ impl Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -144,10 +145,7 @@ impl Vec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/f32/neon/vec3a.rs b/src/f32/neon/vec3a.rs index f9ba7f0a..2b585d71 100644 --- a/src/f32/neon/vec3a.rs +++ b/src/f32/neon/vec3a.rs @@ -137,6 +137,7 @@ impl Vec3A { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -147,9 +148,7 @@ impl Vec3A { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`. diff --git a/src/f32/neon/vec4.rs b/src/f32/neon/vec4.rs index ef897fe3..a740fe56 100644 --- a/src/f32/neon/vec4.rs +++ b/src/f32/neon/vec4.rs @@ -139,6 +139,7 @@ impl Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } diff --git a/src/f32/scalar/vec3a.rs b/src/f32/scalar/vec3a.rs index 601e9bbd..a682a819 100644 --- a/src/f32/scalar/vec3a.rs +++ b/src/f32/scalar/vec3a.rs @@ -139,6 +139,7 @@ impl Vec3A { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -149,9 +150,7 @@ impl Vec3A { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`. diff --git a/src/f32/scalar/vec4.rs b/src/f32/scalar/vec4.rs index 9246387f..f4c7c5fb 100644 --- a/src/f32/scalar/vec4.rs +++ b/src/f32/scalar/vec4.rs @@ -158,6 +158,7 @@ impl Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -168,10 +169,7 @@ impl Vec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/f32/sse2/vec3a.rs b/src/f32/sse2/vec3a.rs index 23181e22..a4ac444b 100644 --- a/src/f32/sse2/vec3a.rs +++ b/src/f32/sse2/vec3a.rs @@ -145,6 +145,7 @@ impl Vec3A { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -155,9 +156,7 @@ impl Vec3A { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`. diff --git a/src/f32/sse2/vec4.rs b/src/f32/sse2/vec4.rs index c1421823..3d8112bc 100644 --- a/src/f32/sse2/vec4.rs +++ b/src/f32/sse2/vec4.rs @@ -147,6 +147,7 @@ impl Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } diff --git a/src/f32/vec2.rs b/src/f32/vec2.rs index ed945388..45f3bd1a 100644 --- a/src/f32/vec2.rs +++ b/src/f32/vec2.rs @@ -123,6 +123,7 @@ impl Vec2 { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 2); Self::new(slice[0], slice[1]) } @@ -133,8 +134,7 @@ impl Vec2 { /// Panics if `slice` is less than 2 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from `self` and the given `z` value. diff --git a/src/f32/vec3.rs b/src/f32/vec3.rs index cf56153c..248cb34a 100644 --- a/src/f32/vec3.rs +++ b/src/f32/vec3.rs @@ -130,6 +130,7 @@ impl Vec3 { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -140,9 +141,7 @@ impl Vec3 { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Internal method for creating a 3D vector from a 4D vector, discarding `w`. diff --git a/src/f32/wasm32/vec3a.rs b/src/f32/wasm32/vec3a.rs index 44d93d95..3c4158c7 100644 --- a/src/f32/wasm32/vec3a.rs +++ b/src/f32/wasm32/vec3a.rs @@ -131,6 +131,7 @@ impl Vec3A { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -141,9 +142,7 @@ impl Vec3A { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`. diff --git a/src/f32/wasm32/vec4.rs b/src/f32/wasm32/vec4.rs index b950c0e0..1fbb7e2c 100644 --- a/src/f32/wasm32/vec4.rs +++ b/src/f32/wasm32/vec4.rs @@ -133,6 +133,7 @@ impl Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[f32]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -143,10 +144,7 @@ impl Vec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/f64/dvec2.rs b/src/f64/dvec2.rs index 66d87675..25f1c4e9 100644 --- a/src/f64/dvec2.rs +++ b/src/f64/dvec2.rs @@ -123,6 +123,7 @@ impl DVec2 { #[inline] #[must_use] pub const fn from_slice(slice: &[f64]) -> Self { + assert!(slice.len() >= 2); Self::new(slice[0], slice[1]) } @@ -133,8 +134,7 @@ impl DVec2 { /// Panics if `slice` is less than 2 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f64]) { - slice[0] = self.x; - slice[1] = self.y; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from `self` and the given `z` value. diff --git a/src/f64/dvec3.rs b/src/f64/dvec3.rs index 23698185..80a7bb32 100644 --- a/src/f64/dvec3.rs +++ b/src/f64/dvec3.rs @@ -130,6 +130,7 @@ impl DVec3 { #[inline] #[must_use] pub const fn from_slice(slice: &[f64]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -140,9 +141,7 @@ impl DVec3 { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f64]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Internal method for creating a 3D vector from a 4D vector, discarding `w`. diff --git a/src/f64/dvec4.rs b/src/f64/dvec4.rs index a99b0ab1..6447a787 100644 --- a/src/f64/dvec4.rs +++ b/src/f64/dvec4.rs @@ -149,6 +149,7 @@ impl DVec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[f64]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -159,10 +160,7 @@ impl DVec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [f64]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/i16/i16vec2.rs b/src/i16/i16vec2.rs index a3758c98..4ff4ad44 100644 --- a/src/i16/i16vec2.rs +++ b/src/i16/i16vec2.rs @@ -115,6 +115,7 @@ impl I16Vec2 { #[inline] #[must_use] pub const fn from_slice(slice: &[i16]) -> Self { + assert!(slice.len() >= 2); Self::new(slice[0], slice[1]) } @@ -125,8 +126,7 @@ impl I16Vec2 { /// Panics if `slice` is less than 2 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i16]) { - slice[0] = self.x; - slice[1] = self.y; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from `self` and the given `z` value. diff --git a/src/i16/i16vec3.rs b/src/i16/i16vec3.rs index e7dcbd5c..41a46071 100644 --- a/src/i16/i16vec3.rs +++ b/src/i16/i16vec3.rs @@ -122,6 +122,7 @@ impl I16Vec3 { #[inline] #[must_use] pub const fn from_slice(slice: &[i16]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -132,9 +133,7 @@ impl I16Vec3 { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i16]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Internal method for creating a 3D vector from a 4D vector, discarding `w`. diff --git a/src/i16/i16vec4.rs b/src/i16/i16vec4.rs index 70bba937..8f2898a0 100644 --- a/src/i16/i16vec4.rs +++ b/src/i16/i16vec4.rs @@ -141,6 +141,7 @@ impl I16Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[i16]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -151,10 +152,7 @@ impl I16Vec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i16]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index 140c33ac..82c89181 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -115,6 +115,7 @@ impl IVec2 { #[inline] #[must_use] pub const fn from_slice(slice: &[i32]) -> Self { + assert!(slice.len() >= 2); Self::new(slice[0], slice[1]) } @@ -125,8 +126,7 @@ impl IVec2 { /// Panics if `slice` is less than 2 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i32]) { - slice[0] = self.x; - slice[1] = self.y; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from `self` and the given `z` value. diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index afd0252c..91569257 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -122,6 +122,7 @@ impl IVec3 { #[inline] #[must_use] pub const fn from_slice(slice: &[i32]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -132,9 +133,7 @@ impl IVec3 { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Internal method for creating a 3D vector from a 4D vector, discarding `w`. diff --git a/src/i32/ivec4.rs b/src/i32/ivec4.rs index f76b89f9..184574a9 100644 --- a/src/i32/ivec4.rs +++ b/src/i32/ivec4.rs @@ -141,6 +141,7 @@ impl IVec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[i32]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -151,10 +152,7 @@ impl IVec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/i64/i64vec2.rs b/src/i64/i64vec2.rs index a6d70399..e406ad79 100644 --- a/src/i64/i64vec2.rs +++ b/src/i64/i64vec2.rs @@ -115,6 +115,7 @@ impl I64Vec2 { #[inline] #[must_use] pub const fn from_slice(slice: &[i64]) -> Self { + assert!(slice.len() >= 2); Self::new(slice[0], slice[1]) } @@ -125,8 +126,7 @@ impl I64Vec2 { /// Panics if `slice` is less than 2 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i64]) { - slice[0] = self.x; - slice[1] = self.y; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from `self` and the given `z` value. diff --git a/src/i64/i64vec3.rs b/src/i64/i64vec3.rs index ee261753..056fd26f 100644 --- a/src/i64/i64vec3.rs +++ b/src/i64/i64vec3.rs @@ -122,6 +122,7 @@ impl I64Vec3 { #[inline] #[must_use] pub const fn from_slice(slice: &[i64]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -132,9 +133,7 @@ impl I64Vec3 { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i64]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Internal method for creating a 3D vector from a 4D vector, discarding `w`. diff --git a/src/i64/i64vec4.rs b/src/i64/i64vec4.rs index 50da7ebd..231c1838 100644 --- a/src/i64/i64vec4.rs +++ b/src/i64/i64vec4.rs @@ -141,6 +141,7 @@ impl I64Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[i64]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -151,10 +152,7 @@ impl I64Vec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [i64]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/u16/u16vec2.rs b/src/u16/u16vec2.rs index 6df5f710..6ac6b746 100644 --- a/src/u16/u16vec2.rs +++ b/src/u16/u16vec2.rs @@ -106,6 +106,7 @@ impl U16Vec2 { #[inline] #[must_use] pub const fn from_slice(slice: &[u16]) -> Self { + assert!(slice.len() >= 2); Self::new(slice[0], slice[1]) } @@ -116,8 +117,7 @@ impl U16Vec2 { /// Panics if `slice` is less than 2 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u16]) { - slice[0] = self.x; - slice[1] = self.y; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from `self` and the given `z` value. diff --git a/src/u16/u16vec3.rs b/src/u16/u16vec3.rs index e83180a0..64a65292 100644 --- a/src/u16/u16vec3.rs +++ b/src/u16/u16vec3.rs @@ -110,6 +110,7 @@ impl U16Vec3 { #[inline] #[must_use] pub const fn from_slice(slice: &[u16]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -120,9 +121,7 @@ impl U16Vec3 { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u16]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Internal method for creating a 3D vector from a 4D vector, discarding `w`. diff --git a/src/u16/u16vec4.rs b/src/u16/u16vec4.rs index 3e74dd4b..bb8762da 100644 --- a/src/u16/u16vec4.rs +++ b/src/u16/u16vec4.rs @@ -126,6 +126,7 @@ impl U16Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[u16]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -136,10 +137,7 @@ impl U16Vec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u16]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/u32/uvec2.rs b/src/u32/uvec2.rs index edce7d88..24d74314 100644 --- a/src/u32/uvec2.rs +++ b/src/u32/uvec2.rs @@ -106,6 +106,7 @@ impl UVec2 { #[inline] #[must_use] pub const fn from_slice(slice: &[u32]) -> Self { + assert!(slice.len() >= 2); Self::new(slice[0], slice[1]) } @@ -116,8 +117,7 @@ impl UVec2 { /// Panics if `slice` is less than 2 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u32]) { - slice[0] = self.x; - slice[1] = self.y; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from `self` and the given `z` value. diff --git a/src/u32/uvec3.rs b/src/u32/uvec3.rs index 1ef55022..d449c32a 100644 --- a/src/u32/uvec3.rs +++ b/src/u32/uvec3.rs @@ -110,6 +110,7 @@ impl UVec3 { #[inline] #[must_use] pub const fn from_slice(slice: &[u32]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -120,9 +121,7 @@ impl UVec3 { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Internal method for creating a 3D vector from a 4D vector, discarding `w`. diff --git a/src/u32/uvec4.rs b/src/u32/uvec4.rs index 394d4202..d329e086 100644 --- a/src/u32/uvec4.rs +++ b/src/u32/uvec4.rs @@ -126,6 +126,7 @@ impl UVec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[u32]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -136,10 +137,7 @@ impl UVec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u32]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. diff --git a/src/u64/u64vec2.rs b/src/u64/u64vec2.rs index 9b208ebe..3177e917 100644 --- a/src/u64/u64vec2.rs +++ b/src/u64/u64vec2.rs @@ -106,6 +106,7 @@ impl U64Vec2 { #[inline] #[must_use] pub const fn from_slice(slice: &[u64]) -> Self { + assert!(slice.len() >= 2); Self::new(slice[0], slice[1]) } @@ -116,8 +117,7 @@ impl U64Vec2 { /// Panics if `slice` is less than 2 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u64]) { - slice[0] = self.x; - slice[1] = self.y; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from `self` and the given `z` value. diff --git a/src/u64/u64vec3.rs b/src/u64/u64vec3.rs index d2e3f814..1df4c433 100644 --- a/src/u64/u64vec3.rs +++ b/src/u64/u64vec3.rs @@ -110,6 +110,7 @@ impl U64Vec3 { #[inline] #[must_use] pub const fn from_slice(slice: &[u64]) -> Self { + assert!(slice.len() >= 3); Self::new(slice[0], slice[1], slice[2]) } @@ -120,9 +121,7 @@ impl U64Vec3 { /// Panics if `slice` is less than 3 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u64]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; + slice.copy_from_slice(&self.to_array()); } /// Internal method for creating a 3D vector from a 4D vector, discarding `w`. diff --git a/src/u64/u64vec4.rs b/src/u64/u64vec4.rs index b6374507..28e61449 100644 --- a/src/u64/u64vec4.rs +++ b/src/u64/u64vec4.rs @@ -126,6 +126,7 @@ impl U64Vec4 { #[inline] #[must_use] pub const fn from_slice(slice: &[u64]) -> Self { + assert!(slice.len() >= 4); Self::new(slice[0], slice[1], slice[2], slice[3]) } @@ -136,10 +137,7 @@ impl U64Vec4 { /// Panics if `slice` is less than 4 elements long. #[inline] pub fn write_to_slice(self, slice: &mut [u64]) { - slice[0] = self.x; - slice[1] = self.y; - slice[2] = self.z; - slice[3] = self.w; + slice.copy_from_slice(&self.to_array()); } /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.