diff --git a/alloy/src/math/vec/vec2.rs b/alloy/src/math/vec/vec2.rs index 0ea25da..6bf4995 100644 --- a/alloy/src/math/vec/vec2.rs +++ b/alloy/src/math/vec/vec2.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::math::vec::{Vec2i, Vec2u}; use crate::cmp::{AlmostEq, Near}; @@ -132,10 +133,8 @@ impl Vec2 { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError> { - if slice.len() == 2 { - // SAFETY: Vec2 is transparent, and implemented directly in terms of a - // slice of f32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(2, slice.len())) @@ -174,10 +173,8 @@ impl Vec2 { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError> { - if slice.len() == 2 { - // SAFETY: Vec2 is transparent, and implemented directly in terms of a - // slice of f32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(2, slice.len())) @@ -888,13 +885,11 @@ impl Vector2 { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[f32]) -> Result { - if slice.len() != 2 { - Err(VecError::new(2, slice.len())) + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements. + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - }) + Err(VecError::new(2, slice.len())) } } diff --git a/alloy/src/math/vec/vec2i.rs b/alloy/src/math/vec/vec2i.rs index 3b199d4..e896267 100644 --- a/alloy/src/math/vec/vec2i.rs +++ b/alloy/src/math/vec/vec2i.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::ops::Dot; use std::borrow::{Borrow, BorrowMut}; @@ -125,10 +126,8 @@ impl Vec2i { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[i32]) -> Result<&Self, VecError> { - if slice.len() == 2 { - // SAFETY: Vec2 is transparent, and implemented directly in terms of a - // slice of i32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(2, slice.len())) @@ -167,10 +166,8 @@ impl Vec2i { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [i32]) -> Result<&mut Self, VecError> { - if slice.len() == 2 { - // SAFETY: Vec2 is transparent, and implemented directly in terms of a - // slice of i32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(2, slice.len())) @@ -698,13 +695,11 @@ impl Vector2i { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[i32]) -> Result { - if slice.len() != 2 { - Err(VecError::new(2, slice.len())) + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - }) + Err(VecError::new(2, slice.len())) } } diff --git a/alloy/src/math/vec/vec2u.rs b/alloy/src/math/vec/vec2u.rs index f9579c4..d483d92 100644 --- a/alloy/src/math/vec/vec2u.rs +++ b/alloy/src/math/vec/vec2u.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::ops::Dot; use std::borrow::{Borrow, BorrowMut}; @@ -125,10 +126,8 @@ impl Vec2u { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[u32]) -> Result<&Self, VecError> { - if slice.len() == 2 { - // SAFETY: Vec2 is transparent, and implemented directly in terms of a - // slice of u32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(2, slice.len())) @@ -167,10 +166,8 @@ impl Vec2u { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [u32]) -> Result<&mut Self, VecError> { - if slice.len() == 2 { - // SAFETY: Vec2 is transparent, and implemented directly in terms of a - // slice of u32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(2, slice.len())) @@ -668,13 +665,11 @@ impl Vector2u { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[u32]) -> Result { - if slice.len() != 2 { - Err(VecError::new(2, slice.len())) + if hint::likely(slice.len() == 2) { + // SAFETY: slice is checked to have exactly 2 elements + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - }) + Err(VecError::new(2, slice.len())) } } diff --git a/alloy/src/math/vec/vec3.rs b/alloy/src/math/vec/vec3.rs index a90bc22..01d58af 100644 --- a/alloy/src/math/vec/vec3.rs +++ b/alloy/src/math/vec/vec3.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::math::vec::{Vec2, Vec3i, Vec3u}; use crate::cmp::{AlmostEq, Near}; @@ -134,10 +135,8 @@ impl Vec3 { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError> { - if slice.len() == 3 { - // SAFETY: Vec3 is transparent, and implemented directly in terms of a - // slice of f32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have exactly 3 elements Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(3, slice.len())) @@ -176,10 +175,8 @@ impl Vec3 { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError> { - if slice.len() == 3 { - // SAFETY: Vec3 is transparent, and implemented directly in terms of a - // slice of f32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have exactly 3 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(3, slice.len())) @@ -979,14 +976,11 @@ impl Vector3 { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[f32]) -> Result { - if slice.len() != 3 { - Err(VecError::new(3, slice.len())) + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have exactly 3 elements + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - z: slice[3], - }) + Err(VecError::new(3, slice.len())) } } diff --git a/alloy/src/math/vec/vec3i.rs b/alloy/src/math/vec/vec3i.rs index 9300650..50dacf2 100644 --- a/alloy/src/math/vec/vec3i.rs +++ b/alloy/src/math/vec/vec3i.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::math::vec::Vec2i; use std::borrow::{Borrow, BorrowMut}; @@ -127,10 +128,8 @@ impl Vec3i { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[i32]) -> Result<&Self, VecError> { - if slice.len() == 3 { - // SAFETY: Vec3 is transparent, and implemented directly in terms of a - // slice of i32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have exactly 3 elements Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(3, slice.len())) @@ -169,10 +168,8 @@ impl Vec3i { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [i32]) -> Result<&mut Self, VecError> { - if slice.len() == 3 { - // SAFETY: Vec3 is transparent, and implemented directly in terms of a - // slice of i32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have exactly 3 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(3, slice.len())) @@ -793,14 +790,11 @@ impl Vector3i { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[i32]) -> Result { - if slice.len() != 3 { - Err(VecError::new(3, slice.len())) + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have exactly 3 elements + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - z: slice[3], - }) + Err(VecError::new(3, slice.len())) } } diff --git a/alloy/src/math/vec/vec3u.rs b/alloy/src/math/vec/vec3u.rs index c93a076..a4d60a4 100644 --- a/alloy/src/math/vec/vec3u.rs +++ b/alloy/src/math/vec/vec3u.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::math::vec::Vec2u; use std::borrow::{Borrow, BorrowMut}; @@ -126,10 +127,8 @@ impl Vec3u { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[u32]) -> Result<&Self, VecError> { - if slice.len() == 3 { - // SAFETY: Vec3 is transparent, and implemented directly in terms of a - // slice of u32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have 3 elements. Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(3, slice.len())) @@ -168,10 +167,8 @@ impl Vec3u { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [u32]) -> Result<&mut Self, VecError> { - if slice.len() == 3 { - // SAFETY: Vec3 is transparent, and implemented directly in terms of a - // slice of u32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have 3 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(3, slice.len())) @@ -753,14 +750,11 @@ impl Vector3u { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[u32]) -> Result { - if slice.len() != 3 { - Err(VecError::new(3, slice.len())) + if hint::likely(slice.len() == 3) { + // SAFETY: slice is checked to have 3 elements. + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - z: slice[3], - }) + Err(VecError::new(3, slice.len())) } } diff --git a/alloy/src/math/vec/vec4.rs b/alloy/src/math/vec/vec4.rs index e5be162..0a1ccaf 100644 --- a/alloy/src/math/vec/vec4.rs +++ b/alloy/src/math/vec/vec4.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::math::vec::{Vec2, Vec3, Vec4i, Vec4u}; use crate::cmp::{AlmostEq, Near}; @@ -138,10 +139,8 @@ impl Vec4 { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError> { - if slice.len() == 4 { - // SAFETY: Vec4 is transparent, and implemented directly in terms of a - // slice of f32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(4, slice.len())) @@ -180,10 +179,8 @@ impl Vec4 { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError> { - if slice.len() == 4 { - // SAFETY: Vec4 is transparent, and implemented directly in terms of a - // slice of f32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(4, slice.len())) @@ -1146,15 +1143,11 @@ impl Vector4 { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[f32]) -> Result { - if slice.len() != 4 { - Err(VecError::new(4, slice.len())) + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - z: slice[3], - w: slice[4], - }) + Err(VecError::new(4, slice.len())) } } diff --git a/alloy/src/math/vec/vec4i.rs b/alloy/src/math/vec/vec4i.rs index ade0caf..dccba4b 100644 --- a/alloy/src/math/vec/vec4i.rs +++ b/alloy/src/math/vec/vec4i.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::math::vec::{Vec2i, Vec3i}; use std::borrow::{Borrow, BorrowMut}; @@ -129,10 +130,8 @@ impl Vec4i { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[i32]) -> Result<&Self, VecError> { - if slice.len() == 4 { - // SAFETY: Vec4 is transparent, and implemented directly in terms of a - // slice of i32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(4, slice.len())) @@ -171,10 +170,8 @@ impl Vec4i { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [i32]) -> Result<&mut Self, VecError> { - if slice.len() == 4 { - // SAFETY: Vec4 is transparent, and implemented directly in terms of a - // slice of i32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(4, slice.len())) @@ -926,15 +923,11 @@ impl Vector4i { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[i32]) -> Result { - if slice.len() != 4 { - Err(VecError::new(4, slice.len())) + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - z: slice[3], - w: slice[4], - }) + Err(VecError::new(4, slice.len())) } } diff --git a/alloy/src/math/vec/vec4u.rs b/alloy/src/math/vec/vec4u.rs index 66cea68..2532dbb 100644 --- a/alloy/src/math/vec/vec4u.rs +++ b/alloy/src/math/vec/vec4u.rs @@ -1,4 +1,5 @@ use super::errors::VecError; +use crate::core::hint; use crate::math::vec::{Vec2u, Vec3u}; use std::borrow::{Borrow, BorrowMut}; @@ -129,10 +130,8 @@ impl Vec4u { /// assert!(vec.is_err()); /// ``` pub const fn from_slice(slice: &[u32]) -> Result<&Self, VecError> { - if slice.len() == 4 { - // SAFETY: Vec4 is transparent, and implemented directly in terms of a - // slice of u32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { Err(VecError::new(4, slice.len())) @@ -171,10 +170,8 @@ impl Vec4u { /// assert!(vec.is_err()); /// ``` pub fn from_mut_slice(slice: &mut [u32]) -> Result<&mut Self, VecError> { - if slice.len() == 4 { - // SAFETY: Vec4 is transparent, and implemented directly in terms of a - // slice of u32s. The representation is the same, and thus valid. - // This is implemented symmetrically to `OsStr`. + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements Ok(unsafe { Self::from_mut_slice_unchecked(slice) }) } else { Err(VecError::new(4, slice.len())) @@ -880,15 +877,11 @@ impl Vector4u { /// /// * `slice` - the slice to read from pub const fn from_slice(slice: &[u32]) -> Result { - if slice.len() != 4 { - Err(VecError::new(4, slice.len())) + if hint::likely(slice.len() == 4) { + // SAFETY: slice is checked to have exactly 4 elements + Ok(unsafe { Self::from_slice_unchecked(slice) }) } else { - Ok(Self { - x: slice[0], - y: slice[1], - z: slice[3], - w: slice[4], - }) + Err(VecError::new(4, slice.len())) } }