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
use Line;
use Cow;
use crate::;
/// A `SkimItem` defines what's been processed(fetched, matched, previewed and returned) by skim
///
/// # Downcast Example
/// Skim will return the item back, but in `Arc<dyn SkimItem>` form. We might want a reference
/// to the concrete type instead of trait object. Skim provide a somehow "complicated" way to
/// `downcast` it back to the reference of the original concrete type.
///
/// ```rust
/// use skim::prelude::*;
///
/// struct MyItem {}
/// impl SkimItem for MyItem {
/// fn text(&self) -> Cow<str> {
/// unimplemented!()
/// }
/// }
///
/// impl MyItem {
/// pub fn mutable(&mut self) -> i32 {
/// 1
/// }
///
/// pub fn immutable(&self) -> i32 {
/// 0
/// }
/// }
///
/// let mut ret: Arc<dyn SkimItem> = Arc::new(MyItem{});
/// let mutable: &mut MyItem = Arc::get_mut(&mut ret)
/// .expect("item is referenced by others")
/// .as_any_mut() // cast to Any
/// .downcast_mut::<MyItem>() // downcast to (mut) concrete type
/// .expect("something wrong with downcast");
/// assert_eq!(mutable.mutable(), 1);
///
/// let immutable: &MyItem = (*ret).as_any() // cast to Any
/// .downcast_ref::<MyItem>() // downcast to concrete type
/// .expect("something wrong with downcast");
/// assert_eq!(immutable.immutable(), 0)
/// ```
//------------------------------------------------------------------------------
// Implement SkimItem for raw strings