2020-09-25 16:10:24 -05:00
|
|
|
use core::cell::Cell;
|
2019-12-06 22:18:12 -06:00
|
|
|
use core::clone::Clone;
|
|
|
|
use core::mem;
|
2019-04-01 14:08:19 -05:00
|
|
|
use core::ops::DerefMut;
|
2019-12-06 22:18:12 -06:00
|
|
|
use core::option::*;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_ptr() {
|
|
|
|
unsafe {
|
2022-05-28 17:40:47 -05:00
|
|
|
let x: Box<_> = Box::new(0);
|
2015-03-25 19:06:52 -05:00
|
|
|
let addr_x: *const isize = mem::transmute(&*x);
|
2014-06-28 15:57:36 -05:00
|
|
|
let opt = Some(x);
|
|
|
|
let y = opt.unwrap();
|
2015-03-25 19:06:52 -05:00
|
|
|
let addr_y: *const isize = mem::transmute(&*y);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(addr_x, addr_y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_str() {
|
|
|
|
let x = "test".to_string();
|
2014-11-27 12:12:30 -06:00
|
|
|
let addr_x = x.as_ptr();
|
2014-06-28 15:57:36 -05:00
|
|
|
let opt = Some(x);
|
|
|
|
let y = opt.unwrap();
|
2014-11-27 12:12:30 -06:00
|
|
|
let addr_y = y.as_ptr();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(addr_x, addr_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_resource() {
|
|
|
|
use core::cell::RefCell;
|
2019-12-06 22:18:12 -06:00
|
|
|
use std::rc::Rc;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
struct R {
|
2019-12-06 22:18:12 -06:00
|
|
|
i: Rc<RefCell<isize>>,
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
2019-12-06 22:18:12 -06:00
|
|
|
impl Drop for R {
|
|
|
|
fn drop(&mut self) {
|
2014-06-28 15:57:36 -05:00
|
|
|
let ii = &*self.i;
|
|
|
|
let i = *ii.borrow();
|
|
|
|
*ii.borrow_mut() = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn r(i: Rc<RefCell<isize>>) -> R {
|
2019-12-06 22:18:12 -06:00
|
|
|
R { i }
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let i = Rc::new(RefCell::new(0));
|
2014-06-28 15:57:36 -05:00
|
|
|
{
|
|
|
|
let x = r(i.clone());
|
|
|
|
let opt = Some(x);
|
|
|
|
let _y = opt.unwrap();
|
|
|
|
}
|
|
|
|
assert_eq!(*i.borrow(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-10-07 10:59:39 -05:00
|
|
|
#[cfg_attr(not(bootstrap), allow(for_loops_over_fallibles))]
|
2014-06-28 15:57:36 -05:00
|
|
|
fn test_option_dance() {
|
|
|
|
let x = Some(());
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut y = Some(5);
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut y2 = 0;
|
2015-06-10 11:22:20 -05:00
|
|
|
for _x in x {
|
2014-08-18 19:52:38 -05:00
|
|
|
y2 = y.take().unwrap();
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
assert_eq!(y2, 5);
|
|
|
|
assert!(y.is_none());
|
|
|
|
}
|
|
|
|
|
2019-12-06 22:18:12 -06:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
2014-06-28 15:57:36 -05:00
|
|
|
fn test_option_too_much_dance() {
|
2015-06-10 15:33:52 -05:00
|
|
|
struct A;
|
|
|
|
let mut y = Some(A);
|
2014-08-18 19:52:38 -05:00
|
|
|
let _y2 = y.take().unwrap();
|
|
|
|
let _y3 = y.take().unwrap();
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_and() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(x.and(Some(2)), Some(2));
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(x.and(None::<isize>), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(x.and(Some(2)), None);
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(x.and(None::<isize>), None);
|
2021-12-14 11:58:01 -06:00
|
|
|
|
|
|
|
const FOO: Option<isize> = Some(1);
|
|
|
|
const A: Option<isize> = FOO.and(Some(2));
|
|
|
|
const B: Option<isize> = FOO.and(None);
|
|
|
|
assert_eq!(A, Some(2));
|
|
|
|
assert_eq!(B, None);
|
|
|
|
|
|
|
|
const BAR: Option<isize> = None;
|
|
|
|
const C: Option<isize> = BAR.and(Some(2));
|
|
|
|
const D: Option<isize> = BAR.and(None);
|
|
|
|
assert_eq!(C, None);
|
|
|
|
assert_eq!(D, None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_and_then() {
|
2021-12-14 11:58:01 -06:00
|
|
|
const fn plus_one(x: isize) -> Option<isize> {
|
|
|
|
Some(x + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn none(_: isize) -> Option<isize> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2021-12-14 11:58:01 -06:00
|
|
|
assert_eq!(x.and_then(plus_one), Some(2));
|
|
|
|
assert_eq!(x.and_then(none), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2021-12-14 11:58:01 -06:00
|
|
|
assert_eq!(x.and_then(plus_one), None);
|
|
|
|
assert_eq!(x.and_then(none), None);
|
|
|
|
|
|
|
|
const FOO: Option<isize> = Some(1);
|
|
|
|
const A: Option<isize> = FOO.and_then(plus_one);
|
|
|
|
const B: Option<isize> = FOO.and_then(none);
|
|
|
|
assert_eq!(A, Some(2));
|
|
|
|
assert_eq!(B, None);
|
|
|
|
|
|
|
|
const BAR: Option<isize> = None;
|
|
|
|
const C: Option<isize> = BAR.and_then(plus_one);
|
|
|
|
const D: Option<isize> = BAR.and_then(none);
|
|
|
|
assert_eq!(C, None);
|
|
|
|
assert_eq!(D, None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_or() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.or(Some(2)), Some(1));
|
|
|
|
assert_eq!(x.or(None), Some(1));
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.or(Some(2)), Some(2));
|
|
|
|
assert_eq!(x.or(None), None);
|
2021-12-14 11:58:01 -06:00
|
|
|
|
|
|
|
const FOO: Option<isize> = Some(1);
|
|
|
|
const A: Option<isize> = FOO.or(Some(2));
|
|
|
|
const B: Option<isize> = FOO.or(None);
|
|
|
|
assert_eq!(A, Some(1));
|
|
|
|
assert_eq!(B, Some(1));
|
|
|
|
|
|
|
|
const BAR: Option<isize> = None;
|
|
|
|
const C: Option<isize> = BAR.or(Some(2));
|
|
|
|
const D: Option<isize> = BAR.or(None);
|
|
|
|
assert_eq!(C, Some(2));
|
|
|
|
assert_eq!(D, None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_or_else() {
|
2021-12-14 11:58:01 -06:00
|
|
|
const fn two() -> Option<isize> {
|
|
|
|
Some(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn none() -> Option<isize> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2021-12-14 11:58:01 -06:00
|
|
|
assert_eq!(x.or_else(two), Some(1));
|
|
|
|
assert_eq!(x.or_else(none), Some(1));
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2021-12-14 11:58:01 -06:00
|
|
|
assert_eq!(x.or_else(two), Some(2));
|
|
|
|
assert_eq!(x.or_else(none), None);
|
|
|
|
|
|
|
|
const FOO: Option<isize> = Some(1);
|
|
|
|
const A: Option<isize> = FOO.or_else(two);
|
|
|
|
const B: Option<isize> = FOO.or_else(none);
|
|
|
|
assert_eq!(A, Some(1));
|
|
|
|
assert_eq!(B, Some(1));
|
|
|
|
|
|
|
|
const BAR: Option<isize> = None;
|
|
|
|
const C: Option<isize> = BAR.or_else(two);
|
|
|
|
const D: Option<isize> = BAR.or_else(none);
|
|
|
|
assert_eq!(C, Some(2));
|
|
|
|
assert_eq!(D, None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unwrap() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(Some(1).unwrap(), 1);
|
2014-06-28 15:57:36 -05:00
|
|
|
let s = Some("hello".to_string()).unwrap();
|
2014-11-27 12:12:30 -06:00
|
|
|
assert_eq!(s, "hello");
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-31 17:08:25 -06:00
|
|
|
#[should_panic]
|
2014-10-09 14:17:22 -05:00
|
|
|
fn test_unwrap_panic1() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
x.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-31 17:08:25 -06:00
|
|
|
#[should_panic]
|
2014-10-09 14:17:22 -05:00
|
|
|
fn test_unwrap_panic2() {
|
2014-06-28 15:57:36 -05:00
|
|
|
let x: Option<String> = None;
|
|
|
|
x.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unwrap_or() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.unwrap_or(2), 1);
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.unwrap_or(2), 2);
|
2021-12-14 11:58:01 -06:00
|
|
|
|
|
|
|
const A: isize = Some(1).unwrap_or(2);
|
|
|
|
const B: isize = None.unwrap_or(2);
|
|
|
|
assert_eq!(A, 1);
|
|
|
|
assert_eq!(B, 2);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unwrap_or_else() {
|
2021-12-14 11:58:01 -06:00
|
|
|
const fn two() -> isize {
|
|
|
|
2
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2021-12-14 11:58:01 -06:00
|
|
|
assert_eq!(x.unwrap_or_else(two), 1);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2021-12-14 11:58:01 -06:00
|
|
|
assert_eq!(x.unwrap_or_else(two), 2);
|
|
|
|
|
|
|
|
const A: isize = Some(1).unwrap_or_else(two);
|
|
|
|
const B: isize = None.unwrap_or_else(two);
|
|
|
|
assert_eq!(A, 1);
|
|
|
|
assert_eq!(B, 2);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
2021-01-10 04:36:45 -06:00
|
|
|
#[test]
|
|
|
|
fn test_unwrap_unchecked() {
|
|
|
|
assert_eq!(unsafe { Some(1).unwrap_unchecked() }, 1);
|
|
|
|
let s = unsafe { Some("hello".to_string()).unwrap_unchecked() };
|
|
|
|
assert_eq!(s, "hello");
|
|
|
|
}
|
|
|
|
|
2014-06-28 15:57:36 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iter() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let val = 5;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
let x = Some(val);
|
|
|
|
let mut it = x.iter();
|
|
|
|
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(it.next(), Some(&val));
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert!(it.next().is_none());
|
2015-08-27 01:37:40 -05:00
|
|
|
|
|
|
|
let mut it = (&x).into_iter();
|
|
|
|
assert_eq!(it.next(), Some(&val));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mut_iter() {
|
2015-08-27 01:37:40 -05:00
|
|
|
let mut val = 5;
|
2015-01-25 15:05:03 -06:00
|
|
|
let new_val = 11;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
let mut x = Some(val);
|
|
|
|
{
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut it = x.iter_mut();
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
|
|
|
|
match it.next() {
|
|
|
|
Some(interior) => {
|
|
|
|
assert_eq!(*interior, val);
|
|
|
|
*interior = new_val;
|
|
|
|
}
|
|
|
|
None => assert!(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
assert_eq!(x, Some(new_val));
|
2015-08-27 01:37:40 -05:00
|
|
|
|
|
|
|
let mut y = Some(val);
|
|
|
|
let mut it = (&mut y).into_iter();
|
|
|
|
assert_eq!(it.next(), Some(&mut val));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ord() {
|
|
|
|
let small = Some(1.0f64);
|
|
|
|
let big = Some(5.0f64);
|
2019-12-06 22:18:12 -06:00
|
|
|
let nan = Some(0.0f64 / 0.0);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(!(nan < big));
|
|
|
|
assert!(!(nan > big));
|
|
|
|
assert!(small < big);
|
|
|
|
assert!(None < big);
|
|
|
|
assert!(big > None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_collect() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == Some(vec![]));
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == Some(vec![0, 1, 2]));
|
|
|
|
|
2019-12-06 22:18:12 -06:00
|
|
|
let v: Option<Vec<isize>> = (0..3).map(|x| if x > 1 { None } else { Some(x) }).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == None);
|
|
|
|
|
|
|
|
// test that it does not take more elements than it needs
|
2018-07-13 00:25:22 -05:00
|
|
|
let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
|
2022-05-28 17:40:47 -05:00
|
|
|
[Box::new(|| Some(())), Box::new(|| None), Box::new(|| panic!())];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2014-10-15 01:05:01 -05:00
|
|
|
let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
assert!(v == None);
|
|
|
|
}
|
2015-04-28 23:23:28 -05:00
|
|
|
|
2018-12-05 07:52:23 -06:00
|
|
|
#[test]
|
|
|
|
fn test_copied() {
|
|
|
|
let val = 1;
|
|
|
|
let val_ref = &val;
|
|
|
|
let opt_none: Option<&'static u32> = None;
|
|
|
|
let opt_ref = Some(&val);
|
|
|
|
let opt_ref_ref = Some(&val_ref);
|
|
|
|
|
|
|
|
// None works
|
|
|
|
assert_eq!(opt_none.clone(), None);
|
|
|
|
assert_eq!(opt_none.copied(), None);
|
|
|
|
|
|
|
|
// Immutable ref works
|
|
|
|
assert_eq!(opt_ref.clone(), Some(&val));
|
|
|
|
assert_eq!(opt_ref.copied(), Some(1));
|
|
|
|
|
|
|
|
// Double Immutable ref works
|
|
|
|
assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
|
|
|
|
assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
|
|
|
|
assert_eq!(opt_ref_ref.copied().copied(), Some(1));
|
|
|
|
}
|
2014-11-07 13:05:50 -06:00
|
|
|
|
2014-11-17 19:59:46 -06:00
|
|
|
#[test]
|
2014-11-07 13:05:50 -06:00
|
|
|
fn test_cloned() {
|
2016-03-10 21:06:36 -06:00
|
|
|
let val = 1;
|
2015-04-28 23:22:06 -05:00
|
|
|
let val_ref = &val;
|
2014-11-17 19:59:46 -06:00
|
|
|
let opt_none: Option<&'static u32> = None;
|
2015-04-28 23:22:06 -05:00
|
|
|
let opt_ref = Some(&val);
|
|
|
|
let opt_ref_ref = Some(&val_ref);
|
2014-11-17 19:59:46 -06:00
|
|
|
|
|
|
|
// None works
|
|
|
|
assert_eq!(opt_none.clone(), None);
|
|
|
|
assert_eq!(opt_none.cloned(), None);
|
|
|
|
|
|
|
|
// Immutable ref works
|
2015-04-28 23:22:06 -05:00
|
|
|
assert_eq!(opt_ref.clone(), Some(&val));
|
2016-03-10 21:06:36 -06:00
|
|
|
assert_eq!(opt_ref.cloned(), Some(1));
|
2014-11-17 19:59:46 -06:00
|
|
|
|
|
|
|
// Double Immutable ref works
|
2015-04-28 23:22:06 -05:00
|
|
|
assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
|
|
|
|
assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
|
2016-03-10 21:06:36 -06:00
|
|
|
assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
|
2014-11-27 12:12:30 -06:00
|
|
|
}
|
2017-06-07 22:52:13 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_try() {
|
|
|
|
fn try_option_some() -> Option<u8> {
|
|
|
|
let val = Some(1)?;
|
|
|
|
Some(val)
|
|
|
|
}
|
|
|
|
assert_eq!(try_option_some(), Some(1));
|
|
|
|
|
|
|
|
fn try_option_none() -> Option<u8> {
|
|
|
|
let val = None?;
|
|
|
|
Some(val)
|
|
|
|
}
|
|
|
|
assert_eq!(try_option_none(), None);
|
|
|
|
}
|
2018-04-26 18:29:22 -05:00
|
|
|
|
|
|
|
#[test]
|
2019-04-01 14:08:19 -05:00
|
|
|
fn test_option_as_deref() {
|
2018-04-26 18:29:22 -05:00
|
|
|
// Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
|
|
|
|
let ref_option = &Some(&42);
|
2019-04-01 14:08:19 -05:00
|
|
|
assert_eq!(ref_option.as_deref(), Some(&42));
|
2018-04-26 18:29:22 -05:00
|
|
|
|
2018-04-30 14:51:43 -05:00
|
|
|
let ref_option = &Some(String::from("a result"));
|
2019-04-01 14:08:19 -05:00
|
|
|
assert_eq!(ref_option.as_deref(), Some("a result"));
|
2018-04-30 14:51:43 -05:00
|
|
|
|
|
|
|
let ref_option = &Some(vec![1, 2, 3, 4, 5]);
|
2019-04-01 14:08:19 -05:00
|
|
|
assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
|
2018-04-30 14:51:43 -05:00
|
|
|
|
2018-04-26 18:29:22 -05:00
|
|
|
// None: &Option<T: Deref>>::None -> None
|
2018-04-27 09:07:26 -05:00
|
|
|
let ref_option: &Option<&i32> = &None;
|
2019-04-01 14:08:19 -05:00
|
|
|
assert_eq!(ref_option.as_deref(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_option_as_deref_mut() {
|
|
|
|
// Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
|
|
|
|
let mut val = 42;
|
|
|
|
let ref_option = &mut Some(&mut val);
|
|
|
|
assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
|
|
|
|
|
|
|
|
let ref_option = &mut Some(String::from("a result"));
|
|
|
|
assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
|
|
|
|
|
|
|
|
let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
|
|
|
|
assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
|
|
|
|
|
|
|
|
// None: &mut Option<T: Deref>>::None -> None
|
|
|
|
let ref_option: &mut Option<&mut i32> = &mut None;
|
|
|
|
assert_eq!(ref_option.as_deref_mut(), None);
|
2018-07-29 22:03:15 -05:00
|
|
|
}
|
|
|
|
|
2018-07-09 07:50:54 -05:00
|
|
|
#[test]
|
|
|
|
fn test_replace() {
|
|
|
|
let mut x = Some(2);
|
|
|
|
let old = x.replace(5);
|
|
|
|
|
|
|
|
assert_eq!(x, Some(5));
|
|
|
|
assert_eq!(old, Some(2));
|
2018-04-30 14:51:43 -05:00
|
|
|
|
2018-07-09 07:50:54 -05:00
|
|
|
let mut x = None;
|
|
|
|
let old = x.replace(3);
|
2018-04-30 14:51:43 -05:00
|
|
|
|
2018-07-09 07:50:54 -05:00
|
|
|
assert_eq!(x, Some(3));
|
|
|
|
assert_eq!(old, None);
|
2018-04-26 18:29:22 -05:00
|
|
|
}
|
2020-09-03 17:13:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn option_const() {
|
|
|
|
// test that the methods of `Option` are usable in a const context
|
|
|
|
|
|
|
|
const OPTION: Option<usize> = Some(32);
|
2021-10-18 05:19:28 -05:00
|
|
|
assert_eq!(OPTION, Some(32));
|
|
|
|
|
|
|
|
const OPTION_FROM: Option<usize> = Option::from(32);
|
|
|
|
assert_eq!(OPTION_FROM, Some(32));
|
2020-09-03 17:13:25 -05:00
|
|
|
|
|
|
|
const REF: Option<&usize> = OPTION.as_ref();
|
|
|
|
assert_eq!(REF, Some(&32));
|
|
|
|
|
2021-10-18 05:19:28 -05:00
|
|
|
const REF_FROM: Option<&usize> = Option::from(&OPTION);
|
|
|
|
assert_eq!(REF_FROM, Some(&32));
|
|
|
|
|
2020-09-03 17:13:25 -05:00
|
|
|
const IS_SOME: bool = OPTION.is_some();
|
|
|
|
assert!(IS_SOME);
|
|
|
|
|
|
|
|
const IS_NONE: bool = OPTION.is_none();
|
|
|
|
assert!(!IS_NONE);
|
2021-07-02 16:34:52 -05:00
|
|
|
|
|
|
|
const COPIED: Option<usize> = OPTION.as_ref().copied();
|
|
|
|
assert_eq!(COPIED, OPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
const fn option_const_mut() {
|
|
|
|
// test that the methods of `Option` that take mutable references are usable in a const context
|
|
|
|
|
|
|
|
let mut option: Option<usize> = Some(32);
|
|
|
|
|
|
|
|
let _take = option.take();
|
|
|
|
let _replace = option.replace(42);
|
2021-10-16 10:02:42 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
let as_mut = option.as_mut();
|
|
|
|
match as_mut {
|
|
|
|
Some(v) => *v = 32,
|
|
|
|
None => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 05:19:28 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
let as_mut: Option<&mut usize> = Option::from(&mut option);
|
|
|
|
match as_mut {
|
|
|
|
Some(v) => *v = 42,
|
|
|
|
None => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2020-09-03 17:13:25 -05:00
|
|
|
}
|
2020-09-07 17:10:35 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unwrap_drop() {
|
|
|
|
struct Dtor<'a> {
|
|
|
|
x: &'a Cell<isize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> std::ops::Drop for Dtor<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.x.set(self.x.get() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unwrap<T>(o: Option<T>) -> T {
|
|
|
|
match o {
|
|
|
|
Some(v) => v,
|
|
|
|
None => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = &Cell::new(1);
|
|
|
|
|
|
|
|
{
|
|
|
|
let b = Some(Dtor { x });
|
|
|
|
let _c = unwrap(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(x.get(), 0);
|
|
|
|
}
|
2020-11-22 02:08:04 -06:00
|
|
|
|
|
|
|
#[test]
|
2021-07-30 13:13:59 -05:00
|
|
|
fn option_ext() {
|
2020-11-22 02:08:04 -06:00
|
|
|
let thing = "{{ f }}";
|
|
|
|
let f = thing.find("{{");
|
|
|
|
|
|
|
|
if f.is_none() {
|
|
|
|
println!("None!");
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 13:13:59 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn zip_options() {
|
|
|
|
let x = Some(10);
|
|
|
|
let y = Some("foo");
|
|
|
|
let z: Option<usize> = None;
|
|
|
|
|
|
|
|
assert_eq!(x.zip(y), Some((10, "foo")));
|
|
|
|
assert_eq!(x.zip(z), None);
|
|
|
|
assert_eq!(z.zip(x), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unzip_options() {
|
|
|
|
let x = Some((10, "foo"));
|
|
|
|
let y = None::<(bool, i32)>;
|
|
|
|
|
|
|
|
assert_eq!(x.unzip(), (Some(10), Some("foo")));
|
|
|
|
assert_eq!(y.unzip(), (None, None));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn zip_unzip_roundtrip() {
|
|
|
|
let x = Some(10);
|
|
|
|
let y = Some("foo");
|
|
|
|
|
|
|
|
let z = x.zip(y);
|
|
|
|
assert_eq!(z, Some((10, "foo")));
|
|
|
|
|
|
|
|
let a = z.unzip();
|
|
|
|
assert_eq!(a, (x, y));
|
|
|
|
}
|