bulks/into_bulk.rs
1use core::ptr::Thin;
2
3use crate::Bulk;
4
5pub const trait AsBulk
6{
7 /// Creates a bulk from a reference.
8 ///
9 /// See the [crate documentation](crate) for more.
10 ///
11 /// # Examples
12 ///
13 /// ```
14 /// use bulks::*;
15 ///
16 /// let v = [1, 2, 3];
17 /// let bulk = v.bulk();
18 /// let u: [_; _] = bulk.collect();
19 ///
20 /// assert_eq!(u, [&1, &2, &3]);
21 /// ```
22 fn bulk<'a>(&'a self) -> <&'a Self as IntoBulk>::IntoBulk
23 where
24 &'a Self: ~const IntoBulk
25 {
26 self.into_bulk()
27 }
28
29 /// Creates a bulk from a mutable reference.
30 ///
31 /// See the [crate documentation](crate) for more.
32 ///
33 /// # Examples
34 ///
35 /// ```
36 /// use bulks::*;
37 ///
38 /// let mut v = [1, 2, 3];
39 /// let bulk = v.bulk_mut();
40 /// let u: [_; _] = bulk.map(|v| core::mem::replace(v, *v + 1))
41 /// .collect();
42 ///
43 /// assert_eq!(v, [2, 3, 4]);
44 /// assert_eq!(u, [1, 2, 3]);
45 /// ```
46 fn bulk_mut<'a>(&'a mut self) -> <&'a mut Self as IntoBulk>::IntoBulk
47 where
48 &'a mut Self: ~const IntoBulk
49 {
50 self.into_bulk()
51 }
52}
53
54const impl<T> AsBulk for T
55where
56 T: ?Sized
57{
58
59}
60
61pub const trait IntoBulk: IntoIterator<Item: Thin, IntoIter: ExactSizeIterator>
62{
63 /// Which kind of bulk are we turning this into?
64 type IntoBulk: ~const Bulk<Item = Self::Item, IntoIter = Self::IntoIter>;
65
66 /// Creates a bulk from a value.
67 ///
68 /// See the [crate documentation](crate) for more.
69 ///
70 /// # Examples
71 ///
72 /// ```
73 /// use bulks::*;
74 ///
75 /// let v = [1, 2, 3];
76 /// let mut bulk = v.into_bulk();
77 /// let u: [_; _] = bulk.collect();
78 ///
79 /// assert_eq!(u, [1, 2, 3]);
80 /// ```
81 fn into_bulk(self) -> Self::IntoBulk;
82}
83
84const impl<T> IntoBulk for T
85where
86 Self: ~const Bulk
87{
88 type IntoBulk = Self;
89
90 fn into_bulk(self) -> Self::IntoBulk
91 {
92 self
93 }
94}