wrend/utils/
bridge.rs

1use std::{ops::Deref, rc::Rc};
2
3/// Enables accepting either a single item or many items when taking function arguments
4pub struct Bridge<Item>(Vec<Item>);
5
6impl<Item> Deref for Bridge<Item> {
7    type Target = [Item];
8
9    fn deref(&self) -> &Self::Target {
10        &self.0
11    }
12}
13
14impl<Item> From<Bridge<Item>> for Vec<Item> {
15    fn from(bridge: Bridge<Item>) -> Self {
16        bridge.0
17    }
18}
19
20impl<Item: Clone> From<&Bridge<Item>> for Vec<Item> {
21    fn from(bridge: &Bridge<Item>) -> Self {
22        bridge.0.to_owned()
23    }
24}
25
26impl<Item> From<(Item, Item)> for Bridge<Item> {
27    fn from(items: (Item, Item)) -> Self {
28        Bridge(vec![items.0, items.1])
29    }
30}
31
32impl<Item> From<(Item, Item, Item)> for Bridge<Item> {
33    fn from(items: (Item, Item, Item)) -> Self {
34        Bridge(vec![items.0, items.1, items.2])
35    }
36}
37
38impl<Item> From<(Item, Item, Item, Item)> for Bridge<Item> {
39    fn from(items: (Item, Item, Item, Item)) -> Self {
40        Bridge(vec![items.0, items.1, items.2, items.3])
41    }
42}
43
44impl<Item> From<(Item, Item, Item, Item, Item)> for Bridge<Item> {
45    fn from(items: (Item, Item, Item, Item, Item)) -> Self {
46        Bridge(vec![items.0, items.1, items.2, items.3, items.4])
47    }
48}
49
50impl<Item> From<Item> for Bridge<Item> {
51    fn from(items: Item) -> Self {
52        Bridge(vec![items])
53    }
54}
55
56impl<Item: Clone> From<&Item> for Bridge<Item> {
57    fn from(items: &Item) -> Self {
58        Bridge(vec![items.to_owned()])
59    }
60}
61
62impl<Item> From<Vec<Item>> for Bridge<Item> {
63    fn from(items: Vec<Item>) -> Self {
64        Bridge(items)
65    }
66}
67
68impl<Item: Clone> From<&Vec<Item>> for Bridge<Item> {
69    fn from(items: &Vec<Item>) -> Self {
70        Bridge(items.to_owned())
71    }
72}
73
74impl<Item: Clone> From<&[Item]> for Bridge<Item> {
75    fn from(items: &[Item]) -> Self {
76        Bridge(items.to_vec())
77    }
78}
79
80impl<Item: Clone> From<Rc<[Item]>> for Bridge<Item> {
81    fn from(items: Rc<[Item]>) -> Self {
82        Bridge(items.to_vec())
83    }
84}
85
86impl<Item> From<Box<[Item]>> for Bridge<Item> {
87    fn from(items: Box<[Item]>) -> Self {
88        Bridge(Vec::from(items))
89    }
90}
91
92impl<Item> From<Box<Vec<Item>>> for Bridge<Item> {
93    fn from(items: Box<Vec<Item>>) -> Self {
94        Bridge(*items)
95    }
96}
97
98impl<Item: Clone> From<Rc<Vec<Item>>> for Bridge<Item> {
99    fn from(items: Rc<Vec<Item>>) -> Self {
100        Bridge((*items).to_owned())
101    }
102}
103
104// enables any sized array to be used as a bridge
105impl<const U: usize, Item> From<[Item; U]> for Bridge<Item> {
106    fn from(items: [Item; U]) -> Self {
107        Bridge(Vec::from(items))
108    }
109}