@@ -825,7 +825,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
825
825
unsafe { self . tail . as_mut ( ) . map ( |node| & mut node. as_mut ( ) . element ) }
826
826
}
827
827
828
- /// Adds an element first in the list.
828
+ /// Adds an element to the front of the list.
829
829
///
830
830
/// This operation should compute in *O*(1) time.
831
831
///
@@ -844,11 +844,34 @@ impl<T, A: Allocator> LinkedList<T, A> {
844
844
/// ```
845
845
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
846
846
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 {
847
869
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) ) ;
849
871
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
850
872
unsafe {
851
873
self . push_front_node ( node_ptr) ;
874
+ & mut node_ptr. as_mut ( ) . element
852
875
}
853
876
}
854
877
@@ -876,7 +899,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
876
899
self . pop_front_node ( ) . map ( Node :: into_element)
877
900
}
878
901
879
- /// Appends an element to the back of a list.
902
+ /// Adds an element to the back of the list.
880
903
///
881
904
/// This operation should compute in *O*(1) time.
882
905
///
@@ -893,11 +916,34 @@ impl<T, A: Allocator> LinkedList<T, A> {
893
916
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
894
917
#[ rustc_confusables( "push" , "append" ) ]
895
918
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 {
896
941
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) ) ;
898
943
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
899
944
unsafe {
900
945
self . push_back_node ( node_ptr) ;
946
+ & mut node_ptr. as_mut ( ) . element
901
947
}
902
948
}
903
949
0 commit comments