1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::iter::TrustedLen;
use std::mem;
use std::sync::{Arc, Weak};
#[test]
fn uninhabited() {
enum Void {}
let mut a = Weak::<Void>::new();
a = a.clone();
assert!(a.upgrade().is_none());
let mut a: Weak<dyn Any> = a; // Unsizing
a = a.clone();
assert!(a.upgrade().is_none());
}
#[test]
fn slice() {
let a: Arc<[u32; 3]> = Arc::new([3, 2, 1]);
let a: Arc<[u32]> = a; // Unsizing
let b: Arc<[u32]> = Arc::from(&[3, 2, 1][..]); // Conversion
assert_eq!(a, b);
// Exercise is_dangling() with a DST
let mut a = Arc::downgrade(&a);
a = a.clone();
assert!(a.upgrade().is_some());
}
#[test]
fn trait_object() {
let a: Arc<u32> = Arc::new(4);
let a: Arc<dyn Any> = a; // Unsizing
// Exercise is_dangling() with a DST
let mut a = Arc::downgrade(&a);
a = a.clone();
assert!(a.upgrade().is_some());
let mut b = Weak::<u32>::new();
b = b.clone();
assert!(b.upgrade().is_none());
let mut b: Weak<dyn Any> = b; // Unsizing
b = b.clone();
assert!(b.upgrade().is_none());
}
#[test]
fn float_nan_ne() {
let x = Arc::new(f32::NAN);
assert!(x != x);
assert!(!(x == x));
}
#[test]
fn partial_eq() {
struct TestPEq(RefCell<usize>);
impl PartialEq for TestPEq {
fn eq(&self, other: &TestPEq) -> bool {
*self.0.borrow_mut() += 1;
*other.0.borrow_mut() += 1;
true
}
}
let x = Arc::new(TestPEq(RefCell::new(0)));
assert!(x == x);
assert!(!(x != x));
assert_eq!(*x.0.borrow(), 4);
}
#[test]
fn eq() {
#[derive(Eq)]
struct TestEq(RefCell<usize>);
impl PartialEq for TestEq {
fn eq(&self, other: &TestEq) -> bool {
*self.0.borrow_mut() += 1;
*other.0.borrow_mut() += 1;
true
}
}
let x = Arc::new(TestEq(RefCell::new(0)));
assert!(x == x);
assert!(!(x != x));
assert_eq!(*x.0.borrow(), 0);
}
// The test code below is identical to that in `rc.rs`.
// For better maintainability we therefore define this type alias.
type Rc<T, A = std::alloc::Global> = Arc<T, A>;
const SHARED_ITER_MAX: u16 = 100;
fn assert_trusted_len<I: TrustedLen>(_: &I) {}
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
#[test]
fn shared_from_iter_normal() {
// Exercise the base implementation for non-`TrustedLen` iterators.
{
// `Filter` is never `TrustedLen` since we don't
// know statically how many elements will be kept:
let iter = (0..SHARED_ITER_MAX).filter(|x| x % 2 == 0).map(Box::new);
// Collecting into a `Vec<T>` or `Rc<[T]>` should make no difference:
let vec = iter.clone().collect::<Vec<_>>();
let rc = iter.collect::<Rc<[_]>>();
assert_eq!(&*vec, &*rc);
// Clone a bit and let these get dropped.
{
let _rc_2 = rc.clone();
let _rc_3 = rc.clone();
let _rc_4 = Rc::downgrade(&_rc_3);
}
} // Drop what hasn't been here.
}
#[test]
fn shared_from_iter_trustedlen_normal() {
// Exercise the `TrustedLen` implementation under normal circumstances
// where `size_hint()` matches `(_, Some(exact_len))`.
{
let iter = (0..SHARED_ITER_MAX).map(Box::new);
assert_trusted_len(&iter);
// Collecting into a `Vec<T>` or `Rc<[T]>` should make no difference:
let vec = iter.clone().collect::<Vec<_>>();
let rc = iter.collect::<Rc<[_]>>();
assert_eq!(&*vec, &*rc);
assert_eq!(mem::size_of::<Box<u16>>() * SHARED_ITER_MAX as usize, mem::size_of_val(&*rc));
// Clone a bit and let these get dropped.
{
let _rc_2 = rc.clone();
let _rc_3 = rc.clone();
let _rc_4 = Rc::downgrade(&_rc_3);
}
} // Drop what hasn't been here.
// Try a ZST to make sure it is handled well.
{
let iter = (0..SHARED_ITER_MAX).map(drop);
let vec = iter.clone().collect::<Vec<_>>();
let rc = iter.collect::<Rc<[_]>>();
assert_eq!(&*vec, &*rc);
assert_eq!(0, mem::size_of_val(&*rc));
{
let _rc_2 = rc.clone();
let _rc_3 = rc.clone();
let _rc_4 = Rc::downgrade(&_rc_3);
}
}
}
#[test]
#[should_panic = "I've almost got 99 problems."]
fn shared_from_iter_trustedlen_panic() {
// Exercise the `TrustedLen` implementation when `size_hint()` matches
// `(_, Some(exact_len))` but where `.next()` drops before the last iteration.
let iter = (0..SHARED_ITER_MAX).map(|val| match val {
98 => panic!("I've almost got 99 problems."),
_ => Box::new(val),
});
assert_trusted_len(&iter);
let _ = iter.collect::<Rc<[_]>>();
panic!("I am unreachable.");
}
#[test]
fn shared_from_iter_trustedlen_no_fuse() {
// Exercise the `TrustedLen` implementation when `size_hint()` matches
// `(_, Some(exact_len))` but where the iterator does not behave in a fused manner.
struct Iter(std::vec::IntoIter<Option<Box<u8>>>);
unsafe impl TrustedLen for Iter {}
impl Iterator for Iter {
fn size_hint(&self) -> (usize, Option<usize>) {
(2, Some(2))
}
type Item = Box<u8>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().flatten()
}
}
let vec = vec![Some(Box::new(42)), Some(Box::new(24)), None, Some(Box::new(12))];
let iter = Iter(vec.into_iter());
assert_trusted_len(&iter);
assert_eq!(&[Box::new(42), Box::new(24)], &*iter.collect::<Rc<[_]>>());
}
#[test]
fn weak_may_dangle() {
fn hmm<'a>(val: &'a mut Weak<&'a str>) -> Weak<&'a str> {
val.clone()
}
// Without #[may_dangle] we get:
let mut val = Weak::new();
hmm(&mut val);
// ~~~~~~~~ borrowed value does not live long enough
//
// `val` dropped here while still borrowed
// borrow might be used here, when `val` is dropped and runs the `Drop` code for type `std::sync::Weak`
}
/// Test that a panic from a destructor does not leak the allocation.
#[test]
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
fn panic_no_leak() {
use std::alloc::{AllocError, Allocator, Global, Layout};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::ptr::NonNull;
struct AllocCount(Cell<i32>);
unsafe impl Allocator for AllocCount {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.0.set(self.0.get() + 1);
Global.allocate(layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
self.0.set(self.0.get() - 1);
unsafe { Global.deallocate(ptr, layout) }
}
}
struct PanicOnDrop;
impl Drop for PanicOnDrop {
fn drop(&mut self) {
panic!("PanicOnDrop");
}
}
let alloc = AllocCount(Cell::new(0));
let rc = Rc::new_in(PanicOnDrop, &alloc);
assert_eq!(alloc.0.get(), 1);
let panic_message = catch_unwind(AssertUnwindSafe(|| drop(rc))).unwrap_err();
assert_eq!(*panic_message.downcast_ref::<&'static str>().unwrap(), "PanicOnDrop");
assert_eq!(alloc.0.get(), 0);
}
/// This is similar to the doc-test for `Arc::make_mut()`, but on an unsized type (slice).
#[test]
fn make_mut_unsized() {
use alloc::sync::Arc;
let mut data: Arc<[i32]> = Arc::new([10, 20, 30]);
Arc::make_mut(&mut data)[0] += 1; // Won't clone anything
let mut other_data = Arc::clone(&data); // Won't clone inner data
Arc::make_mut(&mut data)[1] += 1; // Clones inner data
Arc::make_mut(&mut data)[2] += 1; // Won't clone anything
Arc::make_mut(&mut other_data)[0] *= 10; // Won't clone anything
// Now `data` and `other_data` point to different allocations.
assert_eq!(*data, [11, 21, 31]);
assert_eq!(*other_data, [110, 20, 30]);
}
#[allow(unused)]
mod pin_coerce_unsized {
use alloc::sync::Arc;
use core::pin::Pin;
pub trait MyTrait {}
impl MyTrait for String {}
// Pin coercion should work for Arc
pub fn pin_arc(arg: Pin<Arc<String>>) -> Pin<Arc<dyn MyTrait>> {
arg
}
}
|