Skip to content

Commit 3d07108

Browse files
committed
Add tests for weak into/from raw
1 parent 9d9903c commit 3d07108

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

library/alloc/src/rc/tests.rs

+42
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,48 @@ fn test_into_from_raw_unsized() {
190190
assert_eq!(rc2.to_string(), "123");
191191
}
192192

193+
#[test]
194+
fn into_from_weak_raw() {
195+
let x = Rc::new(box "hello");
196+
let y = Rc::downgrade(&x);
197+
198+
let y_ptr = Weak::into_raw(y);
199+
unsafe {
200+
assert_eq!(**y_ptr, "hello");
201+
202+
let y = Weak::from_raw(y_ptr);
203+
let y_up = Weak::upgrade(&y).unwrap();
204+
assert_eq!(**y_up, "hello");
205+
drop(y_up);
206+
207+
assert_eq!(Rc::try_unwrap(x).map(|x| *x), Ok("hello"));
208+
}
209+
}
210+
211+
#[test]
212+
fn test_into_from_weak_raw_unsized() {
213+
use std::fmt::Display;
214+
use std::string::ToString;
215+
216+
let arc: Rc<str> = Rc::from("foo");
217+
let weak: Weak<str> = Rc::downgrade(&arc);
218+
219+
let ptr = Weak::into_raw(weak.clone());
220+
let weak2 = unsafe { Weak::from_raw(ptr) };
221+
222+
assert_eq!(unsafe { &*ptr }, "foo");
223+
assert!(weak.ptr_eq(&weak2));
224+
225+
let arc: Rc<dyn Display> = Rc::new(123);
226+
let weak: Weak<dyn Display> = Rc::downgrade(&arc);
227+
228+
let ptr = Weak::into_raw(weak.clone());
229+
let weak2 = unsafe { Weak::from_raw(ptr) };
230+
231+
assert_eq!(unsafe { &*ptr }.to_string(), "123");
232+
assert!(weak.ptr_eq(&weak2));
233+
}
234+
193235
#[test]
194236
fn get_mut() {
195237
let mut x = Rc::new(3);

library/alloc/src/sync/tests.rs

+42
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,48 @@ fn test_into_from_raw_unsized() {
140140
assert_eq!(arc2.to_string(), "123");
141141
}
142142

143+
#[test]
144+
fn into_from_weak_raw() {
145+
let x = Arc::new(box "hello");
146+
let y = Arc::downgrade(&x);
147+
148+
let y_ptr = Weak::into_raw(y);
149+
unsafe {
150+
assert_eq!(**y_ptr, "hello");
151+
152+
let y = Weak::from_raw(y_ptr);
153+
let y_up = Weak::upgrade(&y).unwrap();
154+
assert_eq!(**y_up, "hello");
155+
drop(y_up);
156+
157+
assert_eq!(Arc::try_unwrap(x).map(|x| *x), Ok("hello"));
158+
}
159+
}
160+
161+
#[test]
162+
fn test_into_from_weak_raw_unsized() {
163+
use std::fmt::Display;
164+
use std::string::ToString;
165+
166+
let arc: Arc<str> = Arc::from("foo");
167+
let weak: Weak<str> = Arc::downgrade(&arc);
168+
169+
let ptr = Weak::into_raw(weak.clone());
170+
let weak2 = unsafe { Weak::from_raw(ptr) };
171+
172+
assert_eq!(unsafe { &*ptr }, "foo");
173+
assert!(weak.ptr_eq(&weak2));
174+
175+
let arc: Arc<dyn Display> = Arc::new(123);
176+
let weak: Weak<dyn Display> = Arc::downgrade(&arc);
177+
178+
let ptr = Weak::into_raw(weak.clone());
179+
let weak2 = unsafe { Weak::from_raw(ptr) };
180+
181+
assert_eq!(unsafe { &*ptr }.to_string(), "123");
182+
assert!(weak.ptr_eq(&weak2));
183+
}
184+
143185
#[test]
144186
fn test_cowarc_clone_make_mut() {
145187
let mut cow0 = Arc::new(75);

0 commit comments

Comments
 (0)