Skip to main content

mdmath_core/vector/
slice.rs

1use super::*;
2
3impl< E > Collection for [ E ]
4{
5  type Scalar = E;
6}
7
8// Converted implementation using unwrap_or_else with panic! to avoid the Debug requirement
9impl< E, const N : usize > IntoArray< E, N > for &[ E ]
10where
11  [ E ; N ] : for< 'data > TryFrom< &'data [ E ] >
12{
13  #[ inline ]
14  fn into_array( self ) -> [ E ; N ]
15  {
16    self.try_into().unwrap_or_else
17    (
18      | _ | panic!( "Slice length does not match array length : {} != {}", self.len(), N )
19    )
20  }
21}
22
23impl< E, const N : usize > ArrayRef< E, N > for [ E ]
24{
25  #[ inline( always ) ]
26  fn array_ref( &self ) -> &[ E ; N ]
27  {
28    assert!( self.len() >= N, "Slice must have at least {} element", N );
29    // SAFETY: This is safe if the slice has at least 1 element.
30    #[ allow( unsafe_code ) ]
31    unsafe { &*( self.as_ptr() as *const [ E ; N ] ) }
32  }
33}
34
35impl< E, const N : usize > ArrayMut< E, N > for [ E ]
36{
37  #[ inline( always ) ]
38  fn vector_mut( &mut self ) -> &mut [ E ; N ]
39  {
40    assert!( self.len() >= N, "Slice must have at least {} element", N );
41    // SAFETY: This is safe if the slice has at least N element.
42    #[ allow( unsafe_code ) ]
43    unsafe { &mut *( self.as_ptr() as *mut [ E ; N ] ) }
44  }
45}
46
47impl< E, const N : usize > VectorIter< E, N > for [ E ]
48{
49  fn vector_iter< 'data >( &'data self ) -> impl VectorIteratorRef< 'data, &'data E >
50  where
51    E : 'data,
52  {
53    assert!( self.len() >= N, "Slice must have at least {} elements", N );
54    <[ E ]>::iter( self ).take( N )
55  }
56}
57
58impl< E, const N : usize > VectorIterMut< E, N > for [ E ]
59{
60  fn vector_iter_mut< 'data >( &'data mut self ) -> impl VectorIterator< 'data, &'data mut E >
61  where
62    E : 'data,
63  {
64    assert!( self.len() >= N, "Slice must have at least {} elements", N );
65    <[ E ]>::iter_mut( self )
66  }
67}