feat: from_array for ExactSlice
This commit is contained in:
		
							parent
							
								
									620f926fdd
								
							
						
					
					
						commit
						50bca2ae86
					
				| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
# This file is automatically @generated by Cargo.
 | 
			
		||||
# It is not intended for manual editing.
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "nslice"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
							
								
								
									
										34
									
								
								src/lib.rs
								
								
								
								
							
							
						
						
									
										34
									
								
								src/lib.rs
								
								
								
								
							| 
						 | 
				
			
			@ -82,6 +82,16 @@ impl<T, const N: usize> MinSlice<T, N> {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
impl<T, const N: usize> ExactSlice<T, N> {
 | 
			
		||||
    /// Produce a `&ExactSlice` from an array of `N` `T`s.
 | 
			
		||||
    pub fn from_array(array: &[T; N]) -> &ExactSlice<T, N> {
 | 
			
		||||
        unsafe { Self::from_slice_unchecked(&array[..]) }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Produce a `&mut ExactSlice` from an array of `N` `T`s.
 | 
			
		||||
    pub fn from_mut_array(array: &mut [T; N]) -> &mut ExactSlice<T, N> {
 | 
			
		||||
        unsafe { Self::from_mut_unchecked(&mut array[..]) }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Produce an `&ExactSlice` from a slice of `T`s.
 | 
			
		||||
    /// Returns `None` if there are not the correct number of elements in `slice`.
 | 
			
		||||
    pub fn from_slice(slice: &[T]) -> Option<&ExactSlice<T, N>> {
 | 
			
		||||
| 
						 | 
				
			
			@ -142,7 +152,6 @@ fn basic_min_success() {
 | 
			
		|||
    assert_eq!(minslice.tail[2], 6);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn basic_min_failure() {
 | 
			
		||||
    let slice = &[1, 2, 3, 4, 5, 6];
 | 
			
		||||
| 
						 | 
				
			
			@ -150,3 +159,26 @@ fn basic_min_failure() {
 | 
			
		|||
    assert!(minslice.is_none());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn basic_exact_success() {
 | 
			
		||||
    let slice = &[1, 2, 3, 4, 5, 6];
 | 
			
		||||
    let exactslice: &ExactSlice<_, 6> = ExactSlice::from_slice(slice).unwrap();
 | 
			
		||||
    assert_eq!(exactslice.head[0], 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn basic_exact_failure() {
 | 
			
		||||
    let slice = &[1, 2, 3, 4, 5, 6];
 | 
			
		||||
    let exactslice: Option<&ExactSlice<_, 3>> = ExactSlice::from_slice(slice);
 | 
			
		||||
    assert!(exactslice.is_none());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn array_exact_success() {
 | 
			
		||||
    let array = [1, 2, 3, 4, 5, 6];
 | 
			
		||||
    let exactslice: &ExactSlice<_, 6> = ExactSlice::from_array(&array);
 | 
			
		||||
    assert_eq!(exactslice.head[0], 1);
 | 
			
		||||
    assert_eq!(exactslice.head[5], 6);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue