2018-07-06 19:30:09 +02:00
|
|
|
use std::any::Any;
|
2018-12-08 09:35:23 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::cmp::PartialEq;
|
2019-06-21 03:52:38 +02:00
|
|
|
use std::iter::TrustedLen;
|
2019-11-30 04:30:49 +01:00
|
|
|
use std::mem;
|
|
|
|
use std::rc::{Rc, Weak};
|
2018-07-06 19:30:09 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uninhabited() {
|
|
|
|
enum Void {}
|
|
|
|
let mut a = Weak::<Void>::new();
|
|
|
|
a = a.clone();
|
|
|
|
assert!(a.upgrade().is_none());
|
|
|
|
|
2019-11-30 04:30:49 +01:00
|
|
|
let mut a: Weak<dyn Any> = a; // Unsizing
|
2018-07-06 19:30:09 +02:00
|
|
|
a = a.clone();
|
|
|
|
assert!(a.upgrade().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn slice() {
|
|
|
|
let a: Rc<[u32; 3]> = Rc::new([3, 2, 1]);
|
2019-11-30 04:30:49 +01:00
|
|
|
let a: Rc<[u32]> = a; // Unsizing
|
|
|
|
let b: Rc<[u32]> = Rc::from(&[3, 2, 1][..]); // Conversion
|
2018-07-06 19:30:09 +02:00
|
|
|
assert_eq!(a, b);
|
|
|
|
|
|
|
|
// Exercise is_dangling() with a DST
|
|
|
|
let mut a = Rc::downgrade(&a);
|
|
|
|
a = a.clone();
|
|
|
|
assert!(a.upgrade().is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trait_object() {
|
|
|
|
let a: Rc<u32> = Rc::new(4);
|
2019-11-30 04:30:49 +01:00
|
|
|
let a: Rc<dyn Any> = a; // Unsizing
|
2018-07-06 19:30:09 +02:00
|
|
|
|
|
|
|
// Exercise is_dangling() with a DST
|
|
|
|
let mut a = Rc::downgrade(&a);
|
|
|
|
a = a.clone();
|
|
|
|
assert!(a.upgrade().is_some());
|
|
|
|
|
|
|
|
let mut b = Weak::<u32>::new();
|
|
|
|
b = b.clone();
|
|
|
|
assert!(b.upgrade().is_none());
|
2019-11-30 04:30:49 +01:00
|
|
|
let mut b: Weak<dyn Any> = b; // Unsizing
|
2018-07-06 19:30:09 +02:00
|
|
|
b = b.clone();
|
|
|
|
assert!(b.upgrade().is_none());
|
|
|
|
}
|
2018-12-08 09:35:23 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn float_nan_ne() {
|
2020-04-06 22:44:04 +02:00
|
|
|
let x = Rc::new(f32::NAN);
|
2018-12-08 09:35:23 +01:00
|
|
|
assert!(x != x);
|
|
|
|
assert!(!(x == x));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partial_eq() {
|
2019-11-30 04:30:49 +01:00
|
|
|
struct TestPEq(RefCell<usize>);
|
2018-12-08 09:35:23 +01:00
|
|
|
impl PartialEq for TestPEq {
|
|
|
|
fn eq(&self, other: &TestPEq) -> bool {
|
|
|
|
*self.0.borrow_mut() += 1;
|
|
|
|
*other.0.borrow_mut() += 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let x = Rc::new(TestPEq(RefCell::new(0)));
|
|
|
|
assert!(x == x);
|
|
|
|
assert!(!(x != x));
|
|
|
|
assert_eq!(*x.0.borrow(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn eq() {
|
|
|
|
#[derive(Eq)]
|
2019-11-30 04:30:49 +01:00
|
|
|
struct TestEq(RefCell<usize>);
|
2018-12-08 09:35:23 +01:00
|
|
|
impl PartialEq for TestEq {
|
|
|
|
fn eq(&self, other: &TestEq) -> bool {
|
|
|
|
*self.0.borrow_mut() += 1;
|
|
|
|
*other.0.borrow_mut() += 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let x = Rc::new(TestEq(RefCell::new(0)));
|
|
|
|
assert!(x == x);
|
|
|
|
assert!(!(x != x));
|
|
|
|
assert_eq!(*x.0.borrow(), 0);
|
|
|
|
}
|
2019-06-21 03:52:38 +02:00
|
|
|
|
|
|
|
const SHARED_ITER_MAX: u16 = 100;
|
|
|
|
|
|
|
|
fn assert_trusted_len<I: TrustedLen>(_: &I) {}
|
|
|
|
|
|
|
|
#[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.
|
|
|
|
{
|
2020-01-02 09:56:12 +01:00
|
|
|
let iter = (0..SHARED_ITER_MAX).map(drop);
|
2019-06-21 03:52:38 +02:00
|
|
|
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.
|
2019-11-30 04:30:49 +01:00
|
|
|
let iter = (0..SHARED_ITER_MAX).map(|val| match val {
|
|
|
|
98 => panic!("I've almost got 99 problems."),
|
|
|
|
_ => Box::new(val),
|
|
|
|
});
|
2019-06-21 03:52:38 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 04:30:49 +01:00
|
|
|
let vec = vec![Some(Box::new(42)), Some(Box::new(24)), None, Some(Box::new(12))];
|
2019-06-21 03:52:38 +02:00
|
|
|
let iter = Iter(vec.into_iter());
|
|
|
|
assert_trusted_len(&iter);
|
2019-11-30 04:30:49 +01:00
|
|
|
assert_eq!(&[Box::new(42), Box::new(24)], &*iter.collect::<Rc<[_]>>());
|
2019-06-21 03:52:38 +02:00
|
|
|
}
|