@@ -825,7 +825,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
825825 unsafe { self . tail . as_mut ( ) . map ( |node| & mut node. as_mut ( ) . element ) }
826826 }
827827
828- /// Adds an element first in the list.
828+ /// Adds an element to the front of the list.
829829 ///
830830 /// This operation should compute in *O*(1) time.
831831 ///
@@ -844,11 +844,34 @@ impl<T, A: Allocator> LinkedList<T, A> {
844844 /// ```
845845 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
846846 pub fn push_front ( & mut self , elt : T ) {
847+ let _ = self . push_front_mut ( elt) ;
848+ }
849+
850+ /// Adds an element to the front of the list, returning a reference to it.
851+ ///
852+ /// This operation should compute in *O*(1) time.
853+ ///
854+ /// # Examples
855+ ///
856+ /// ```
857+ /// #![feature(push_mut)]
858+ /// use std::collections::LinkedList;
859+ ///
860+ /// let mut dl = LinkedList::from([1, 2, 3]);
861+ ///
862+ /// let ptr = dl.push_front_mut(2);
863+ /// *ptr += 4;
864+ /// assert_eq!(dl.front().unwrap(), &6);
865+ /// ```
866+ #[ unstable( feature = "push_mut" , issue = "135974" ) ]
867+ #[ must_use = "if you don't need a reference to the value, use `LinkedList::push_front` instead" ]
868+ pub fn push_front_mut ( & mut self , elt : T ) -> & mut T {
847869 let node = Box :: new_in ( Node :: new ( elt) , & self . alloc ) ;
848- let node_ptr = NonNull :: from ( Box :: leak ( node) ) ;
870+ let mut node_ptr = NonNull :: from ( Box :: leak ( node) ) ;
849871 // SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
850872 unsafe {
851873 self . push_front_node ( node_ptr) ;
874+ & mut node_ptr. as_mut ( ) . element
852875 }
853876 }
854877
@@ -876,7 +899,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
876899 self . pop_front_node ( ) . map ( Node :: into_element)
877900 }
878901
879- /// Appends an element to the back of a list.
902+ /// Adds an element to the back of the list.
880903 ///
881904 /// This operation should compute in *O*(1) time.
882905 ///
@@ -893,11 +916,34 @@ impl<T, A: Allocator> LinkedList<T, A> {
893916 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
894917 #[ rustc_confusables( "push" , "append" ) ]
895918 pub fn push_back ( & mut self , elt : T ) {
919+ let _ = self . push_back_mut ( elt) ;
920+ }
921+
922+ /// Adds an element to the back of the list, returning a reference to it.
923+ ///
924+ /// This operation should compute in *O*(1) time.
925+ ///
926+ /// # Examples
927+ ///
928+ /// ```
929+ /// #![feature(push_mut)]
930+ /// use std::collections::LinkedList;
931+ ///
932+ /// let mut dl = LinkedList::from([1, 2, 3]);
933+ ///
934+ /// let ptr = dl.push_back_mut(2);
935+ /// *ptr += 4;
936+ /// assert_eq!(dl.back().unwrap(), &6);
937+ /// ```
938+ #[ unstable( feature = "push_mut" , issue = "135974" ) ]
939+ #[ must_use = "if you don't need a reference to the value, use `LinkedList::push_back` instead" ]
940+ pub fn push_back_mut ( & mut self , elt : T ) -> & mut T {
896941 let node = Box :: new_in ( Node :: new ( elt) , & self . alloc ) ;
897- let node_ptr = NonNull :: from ( Box :: leak ( node) ) ;
942+ let mut node_ptr = NonNull :: from ( Box :: leak ( node) ) ;
898943 // SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
899944 unsafe {
900945 self . push_back_node ( node_ptr) ;
946+ & mut node_ptr. as_mut ( ) . element
901947 }
902948 }
903949
0 commit comments