@@ -168,41 +168,6 @@ pub type ArrayN<T, const N: usize> = Array<T, <[T; N] as AssocArraySize>::Size>;
168168///
169169/// let arr: Array<u8, U3> = Array([1, 2, 3]);
170170/// ```
171- ///
172- /// ## [`Borrow`] impls
173- ///
174- /// The [`Array`] type has impls of the [`Borrow`] trait from `core` for both `[T]` and `[T; N]`,
175- /// which should make it usable anywhere with e.g. `Borrow<[T]>` bounds.
176- ///
177- /// ### `Borrow<Array<T, U>>` for `[T; N]`
178- ///
179- /// This crate provides a `Borrow` impl for `[T; N]` which makes it possible to write APIs in terms
180- /// of `[T; N]` rather than `Array`, so the caller doesn't need to import `Array` at all:
181- ///
182- /// ```
183- /// use std::borrow::Borrow;
184- /// use hybrid_array::{Array, ArraySize, AssocArraySize, ArrayN, sizes::U3};
185- ///
186- /// pub fn getn_hybrid<T, U: ArraySize>(arr: &Array<T, U>, n: usize) -> &T {
187- /// &arr[2]
188- /// }
189- ///
190- /// pub fn getn_generic<T, const N: usize>(arr: &[T; N], n: usize) -> &T
191- /// where
192- /// [T; N]: AssocArraySize + Borrow<ArrayN<T, N>>
193- /// {
194- /// getn_hybrid(arr.borrow(), n)
195- /// }
196- ///
197- /// let array = [0u8, 1, 2, 3];
198- /// let x = getn_generic(&array, 2);
199- /// assert_eq!(x, &2);
200- /// ```
201- ///
202- /// Note that the [`AssocArraySize`] trait can be used to determine the appropriate
203- /// [`Array`] size for a given `[T; N]`, and the [`ArrayN`] trait (which internally uses
204- /// [`AssocArraySize`]) can be used to determine the specific [`Array`] type for a given
205- /// const generic size.
206171#[ repr( transparent) ]
207172pub struct Array < T , U : ArraySize > ( pub U :: ArrayType < T > ) ;
208173
@@ -537,16 +502,6 @@ where
537502 }
538503}
539504
540- impl < T , U > BorrowMut < [ T ] > for Array < T , U >
541- where
542- U : ArraySize ,
543- {
544- #[ inline]
545- fn borrow_mut ( & mut self ) -> & mut [ T ] {
546- self . 0 . as_mut ( )
547- }
548- }
549-
550505impl < T , U , const N : usize > Borrow < [ T ; N ] > for Array < T , U >
551506where
552507 U : ArraySize < ArrayType < T > = [ T ; N ] > ,
@@ -557,33 +512,23 @@ where
557512 }
558513}
559514
560- impl < T , U , const N : usize > BorrowMut < [ T ; N ] > for Array < T , U >
561- where
562- U : ArraySize < ArrayType < T > = [ T ; N ] > ,
563- {
564- #[ inline]
565- fn borrow_mut ( & mut self ) -> & mut [ T ; N ] {
566- & mut self . 0
567- }
568- }
569-
570- impl < T , U , const N : usize > Borrow < Array < T , U > > for [ T ; N ]
515+ impl < T , U > BorrowMut < [ T ] > for Array < T , U >
571516where
572- U : ArraySize < ArrayType < T > = [ T ; N ] > ,
517+ U : ArraySize ,
573518{
574519 #[ inline]
575- fn borrow ( & self ) -> & Array < T , U > {
576- self . into ( )
520+ fn borrow_mut ( & mut self ) -> & mut [ T ] {
521+ self . 0 . as_mut ( )
577522 }
578523}
579524
580- impl < T , U , const N : usize > BorrowMut < Array < T , U > > for [ T ; N ]
525+ impl < T , U , const N : usize > BorrowMut < [ T ; N ] > for Array < T , U >
581526where
582527 U : ArraySize < ArrayType < T > = [ T ; N ] > ,
583528{
584529 #[ inline]
585- fn borrow_mut ( & mut self ) -> & mut Array < T , U > {
586- self . into ( )
530+ fn borrow_mut ( & mut self ) -> & mut [ T ; N ] {
531+ & mut self . 0
587532 }
588533}
589534
0 commit comments