pub struct Vec4(/* private fields */);Expand description
A 4-component non-owning view of a Euclidean vector.
§Guarantees
This type has the strict requirement that it can only reference a
4-component slice of f32 values. It is guaranteed to never refer to
more or less than 4 entries.
§Relation to Vector4
Vec4 is a non-owning equivalent of [Vector2]. This enables non-vector
types to either Deref or provide conversion-related utilities into
Vec4 types to be able to access and benefit from vector operations.
A basic example of where this can be useful is in something like a Point2
type, where it may not be implemented directly in terms of a Vector4,
but can convert into a Vec4 to benefit from operations on vectors.
Implementations§
source§impl Vec4
impl Vec4
sourcepub const fn from_array(array: &[f32; 4]) -> &Self
pub const fn from_array(array: &[f32; 4]) -> &Self
Forms a reference to a Vec4 from a 4-component f32 array.
This function is identical to from_slice_unchecked, except it is not
marked unsafe.
§Parameters
array- an array containing 4f32values.
§Examples
Basic use:
let array: [f32; 4] = [0.1, 0.42, 3.14, 0.9];
let vec = Vec4::from_array(&array);
assert_eq!(vec.as_ptr(), array.as_ptr());
assert_eq!(vec.x(), array[0]);
assert_eq!(vec.y(), array[1]);
assert_eq!(vec.z(), array[2]);
assert_eq!(vec.w(), array[3]);sourcepub fn from_mut_array(array: &mut [f32; 4]) -> &mut Self
pub fn from_mut_array(array: &mut [f32; 4]) -> &mut Self
Forms a mutable reference to a Vec4 from a 4-component f32 array.
This function is identical to from_mut_slice_unchecked, except it is
not marked unsafe.
§Parameters
array- an array containing 4f32values.
§Examples
Basic use:
let mut array: [f32; 4] = [0.1, 42.0, 3.14, 0.9];
let vec = Vec4::from_mut_array(&mut array);
assert_eq!(vec.as_ptr(), array.as_ptr());sourcepub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError>
pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError>
Forms a reference to a Vec4 from a slice of f32.
This requires that slice.len() == 4, otherwise this returns VecError.
§Parameters
slice- the slice off32s.
§Examples
Basic use:
let slice = &[0.1, 0.42, 3.14, 0.9];
let vec = Vec4::from_slice(slice).unwrap();
assert_eq!(vec.as_ptr(), slice.as_ptr());
assert_eq!(vec.x(), slice[0]);
assert_eq!(vec.y(), slice[1]);
assert_eq!(vec.z(), slice[2]);
assert_eq!(vec.w(), slice[3]);Invalid size:
let slice = &[0.1];
let vec = Vec4::from_slice(slice);
assert!(vec.is_err());sourcepub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError>
pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError>
Forms a mutable reference to a Vec4 from a mutable slice of f32.
This requires that slice.len() == 4, otherwise this returns VecError.
§Parameters
slice- the mutable slice off32s.
§Examples
Basic use:
let slice = &mut [0.1, 0.42, 3.14, 0.9];
let vec = Vec4::from_mut_slice(slice).unwrap();
assert_eq!(vec.as_ptr(), slice.as_ptr());Invalid size:
let slice = &mut [0.1];
let vec = Vec4::from_mut_slice(slice);
assert!(vec.is_err());sourcepub const unsafe fn from_slice_unchecked(slice: &[f32]) -> &Self
pub const unsafe fn from_slice_unchecked(slice: &[f32]) -> &Self
sourcepub unsafe fn from_mut_slice_unchecked(slice: &mut [f32]) -> &mut Self
pub unsafe fn from_mut_slice_unchecked(slice: &mut [f32]) -> &mut Self
sourcepub const unsafe fn from_ptr_unchecked<'a>(ptr: *const f32) -> &'a Vec4
pub const unsafe fn from_ptr_unchecked<'a>(ptr: *const f32) -> &'a Vec4
source§impl Vec4
impl Vec4
sourcepub unsafe fn from_mut_ptr_unchecked<'a>(ptr: *mut f32) -> &'a mut Vec4
pub unsafe fn from_mut_ptr_unchecked<'a>(ptr: *mut f32) -> &'a mut Vec4
sourcepub fn as_mut_slice(&mut self) -> &mut [f32]
pub fn as_mut_slice(&mut self) -> &mut [f32]
sourcepub fn as_mut_ptr(&mut self) -> *mut f32
pub fn as_mut_ptr(&mut self) -> *mut f32
Returns this vector as a mutable ptr of f32.
source§impl Vec4
impl Vec4
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut f32>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut f32>
Returns a mutable iterator over the vector.
sourcepub fn x_mut(&mut self) -> &mut f32
pub fn x_mut(&mut self) -> &mut f32
Returns a mutable reference to the X-coordinate of this 3-component vector.
sourcepub fn y_mut(&mut self) -> &mut f32
pub fn y_mut(&mut self) -> &mut f32
Returns a mutable reference to the Y-coordinate of this 3-component vector.
sourcepub fn z_mut(&mut self) -> &mut f32
pub fn z_mut(&mut self) -> &mut f32
Returns a mutable reference to the Z-coordinate of this 3-component vector.
sourcepub fn w_mut(&mut self) -> &mut f32
pub fn w_mut(&mut self) -> &mut f32
Returns a mutable reference to the W-coordinate of this 3-component vector.
sourcepub fn xy_mut(&mut self) -> &mut Vec2
pub fn xy_mut(&mut self) -> &mut Vec2
Returns a mutable reference to the xy coordinates of this vector.
sourcepub fn yz_mut(&mut self) -> &mut Vec2
pub fn yz_mut(&mut self) -> &mut Vec2
Returns a mutable reference to the yz coordinates of this vector.
sourcepub fn zw_mut(&mut self) -> &mut Vec2
pub fn zw_mut(&mut self) -> &mut Vec2
Returns a mutable reference to the zw coordinates of this vector.
sourcepub fn xyz_mut(&mut self) -> &mut Vec3
pub fn xyz_mut(&mut self) -> &mut Vec3
Returns a mutable reference to the yz coordinates of this vector.
sourcepub fn yzw_mut(&mut self) -> &mut Vec3
pub fn yzw_mut(&mut self) -> &mut Vec3
Returns a mutable reference to the zw coordinates of this vector.
sourcepub fn square_magnitude(&self) -> f32
pub fn square_magnitude(&self) -> f32
Computes the square magnitude of this two-component vector.
sourcepub fn magnitude(&self) -> f32
pub fn magnitude(&self) -> f32
Computes the magnitude of this vector.
Where possible, consider using Vec4::square_magnitude as this will
avoid the need to compute the square-root.
sourcepub fn is_normalized(&self) -> bool
pub fn is_normalized(&self) -> bool
Queries whether this vector is normalized (e.g. has a magnitude of 1).
sourcepub fn normalized(&self) -> Vector4
pub fn normalized(&self) -> Vector4
Returns normalized vector.
sourcepub fn is_finite(&self) -> bool
pub fn is_finite(&self) -> bool
Returns whether all components of this Vec are finite.
§Example
Basic use:
let finite = Vector4::UNIT_X;
let not_finite = Vector4::new(f32::INFINITY, 3.0, 0.0, -1.0);
assert!(finite.is_finite());
assert!(!not_finite.is_finite());sourcepub fn is_infinite(&self) -> bool
pub fn is_infinite(&self) -> bool
Trait Implementations§
source§impl AddAssign<&Vec4> for Vec4
impl AddAssign<&Vec4> for Vec4
source§fn add_assign(&mut self, rhs: &Vec4)
fn add_assign(&mut self, rhs: &Vec4)
+= operation. Read moresource§impl AddAssign<&Vec4> for Vector4
impl AddAssign<&Vec4> for Vector4
source§fn add_assign(&mut self, rhs: &Vec4)
fn add_assign(&mut self, rhs: &Vec4)
+= operation. Read moresource§impl AddAssign<&Vector4> for Vec4
impl AddAssign<&Vector4> for Vec4
source§fn add_assign(&mut self, rhs: &Vector4)
fn add_assign(&mut self, rhs: &Vector4)
+= operation. Read moresource§impl AddAssign<Vector4> for Vec4
impl AddAssign<Vector4> for Vec4
source§fn add_assign(&mut self, rhs: Vector4)
fn add_assign(&mut self, rhs: Vector4)
+= operation. Read moresource§impl BorrowMut<[f32]> for Vec4
impl BorrowMut<[f32]> for Vec4
source§fn borrow_mut(&mut self) -> &mut [f32]
fn borrow_mut(&mut self) -> &mut [f32]
source§impl BorrowMut<Vec4> for Color
impl BorrowMut<Vec4> for Color
source§fn borrow_mut(&mut self) -> &mut Vec4
fn borrow_mut(&mut self) -> &mut Vec4
source§impl BorrowMut<Vec4> for Vector4
impl BorrowMut<Vec4> for Vector4
source§fn borrow_mut(&mut self) -> &mut Vec4
fn borrow_mut(&mut self) -> &mut Vec4
source§impl DivAssign<f32> for Vec4
impl DivAssign<f32> for Vec4
source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/= operation. Read moresource§impl Dot<Quaternion> for Vec4
impl Dot<Quaternion> for Vec4
source§impl Dot<Vec4> for Quaternion
impl Dot<Vec4> for Quaternion
source§impl From<&Vec4> for Quaternion
impl From<&Vec4> for Quaternion
source§impl MulAssign<f32> for Vec4
impl MulAssign<f32> for Vec4
source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*= operation. Read moresource§impl PartialOrd for Vec4
impl PartialOrd for Vec4
source§impl RemAssign<f32> for Vec4
impl RemAssign<f32> for Vec4
source§fn rem_assign(&mut self, rhs: f32)
fn rem_assign(&mut self, rhs: f32)
%= operation. Read moresource§impl SubAssign<&Vec4> for Vec4
impl SubAssign<&Vec4> for Vec4
source§fn sub_assign(&mut self, rhs: &Vec4)
fn sub_assign(&mut self, rhs: &Vec4)
-= operation. Read moresource§impl SubAssign<&Vec4> for Vector4
impl SubAssign<&Vec4> for Vector4
source§fn sub_assign(&mut self, rhs: &Vec4)
fn sub_assign(&mut self, rhs: &Vec4)
-= operation. Read moresource§impl SubAssign<&Vector4> for Vec4
impl SubAssign<&Vector4> for Vec4
source§fn sub_assign(&mut self, rhs: &Vector4)
fn sub_assign(&mut self, rhs: &Vector4)
-= operation. Read moresource§impl SubAssign<Vector4> for Vec4
impl SubAssign<Vector4> for Vec4
source§fn sub_assign(&mut self, rhs: Vector4)
fn sub_assign(&mut self, rhs: Vector4)
-= operation. Read more