@@ -1356,6 +1356,33 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
1356
1356
ptr
1357
1357
}
1358
1358
1359
+ /// Consumes the `Rc`, returning the wrapped pointer and allocator.
1360
+ ///
1361
+ /// To avoid a memory leak the pointer must be converted back to an `Rc` using
1362
+ /// [`Rc::from_raw_in`].
1363
+ ///
1364
+ /// # Examples
1365
+ ///
1366
+ /// ```
1367
+ /// #![feature(allocator_api)]
1368
+ /// use std::rc::Rc;
1369
+ /// use std::alloc::System;
1370
+ ///
1371
+ /// let x = Rc::new_in("hello".to_owned(), System);
1372
+ /// let (ptr, alloc) = Rc::into_raw_with_allocator(x);
1373
+ /// assert_eq!(unsafe { &*ptr }, "hello");
1374
+ /// let x = unsafe { Rc::from_raw_in(ptr, alloc) };
1375
+ /// assert_eq!(&*x, "hello");
1376
+ /// ```
1377
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1378
+ pub fn into_raw_with_allocator ( this : Self ) -> ( * const T , A ) {
1379
+ let this = mem:: ManuallyDrop :: new ( this) ;
1380
+ let ptr = Self :: as_ptr ( & this) ;
1381
+ // Safety: `this` is ManuallyDrop so the allocator will not be double-dropped
1382
+ let alloc = unsafe { ptr:: read ( & this. alloc ) } ;
1383
+ ( ptr, alloc)
1384
+ }
1385
+
1359
1386
/// Provides a raw pointer to the data.
1360
1387
///
1361
1388
/// The counts are not affected in any way and the `Rc` is not consumed. The pointer is valid
@@ -3024,39 +3051,42 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
3024
3051
result
3025
3052
}
3026
3053
3027
- /// Consumes the `Weak<T>` and turns it into a raw pointer .
3054
+ /// Consumes the `Weak<T>`, returning the wrapped pointer and allocator .
3028
3055
///
3029
3056
/// This converts the weak pointer into a raw pointer, while still preserving the ownership of
3030
3057
/// one weak reference (the weak count is not modified by this operation). It can be turned
3031
- /// back into the `Weak<T>` with [`from_raw `].
3058
+ /// back into the `Weak<T>` with [`from_raw_in `].
3032
3059
///
3033
3060
/// The same restrictions of accessing the target of the pointer as with
3034
3061
/// [`as_ptr`] apply.
3035
3062
///
3036
3063
/// # Examples
3037
3064
///
3038
3065
/// ```
3066
+ /// #![feature(allocator_api)]
3039
3067
/// use std::rc::{Rc, Weak};
3068
+ /// use std::alloc::System;
3040
3069
///
3041
- /// let strong = Rc::new ("hello".to_owned());
3070
+ /// let strong = Rc::new_in ("hello".to_owned(), System );
3042
3071
/// let weak = Rc::downgrade(&strong);
3043
- /// let raw = weak.into_raw ();
3072
+ /// let ( raw, alloc) = weak.into_raw_with_allocator ();
3044
3073
///
3045
3074
/// assert_eq!(1, Rc::weak_count(&strong));
3046
3075
/// assert_eq!("hello", unsafe { &*raw });
3047
3076
///
3048
- /// drop(unsafe { Weak::from_raw (raw) });
3077
+ /// drop(unsafe { Weak::from_raw_in (raw, alloc ) });
3049
3078
/// assert_eq!(0, Rc::weak_count(&strong));
3050
3079
/// ```
3051
3080
///
3052
- /// [`from_raw `]: Weak::from_raw
3081
+ /// [`from_raw_in `]: Weak::from_raw_in
3053
3082
/// [`as_ptr`]: Weak::as_ptr
3054
3083
#[ inline]
3055
3084
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
3056
- pub fn into_raw_and_alloc ( self ) -> ( * const T , A ) {
3057
- let rc = mem:: ManuallyDrop :: new ( self ) ;
3058
- let result = rc. as_ptr ( ) ;
3059
- let alloc = unsafe { ptr:: read ( & rc. alloc ) } ;
3085
+ pub fn into_raw_with_allocator ( self ) -> ( * const T , A ) {
3086
+ let this = mem:: ManuallyDrop :: new ( self ) ;
3087
+ let result = this. as_ptr ( ) ;
3088
+ // Safety: `this` is ManuallyDrop so the allocator will not be double-dropped
3089
+ let alloc = unsafe { ptr:: read ( & this. alloc ) } ;
3060
3090
( result, alloc)
3061
3091
}
3062
3092
0 commit comments