File: lib.rs

package info (click to toggle)
rust-lifeguard 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136 kB
  • sloc: makefile: 4
file content (197 lines) | stat: -rw-r--r-- 5,260 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
extern crate lifeguard;

#[cfg(test)]
mod tests {
  use lifeguard::*;

  #[test]
  fn test_deref() {
      let str_pool : Pool<String> = Pool::with_size(1);
      let rstring = str_pool.new_from("cat");
      assert_eq!("cat", *rstring);
  }

  #[test]
  fn test_deref_rc() {
      let str_pool : Pool<String> = Pool::with_size(1);
      let rstring = str_pool.new_rc_from("cat");
      assert_eq!("cat", *rstring);
  }

  #[test]
  fn test_deref_mut() {
      let str_pool : Pool<String> = Pool::with_size(1);
      let mut rstring = str_pool.new_from("cat");
      rstring.push_str("s love eating mice");
      assert_eq!("cats love eating mice", *rstring);
  }

  #[test]
  fn test_deref_mut_rc() {
      let str_pool : Pool<String> = Pool::with_size(1);
      let mut rstring = str_pool.new_rc_from("cat");
      rstring.push_str("s love eating mice");
      assert_eq!("cats love eating mice", *rstring);
  }

  #[test]
  fn test_as_mut() {
      let str_pool : Pool<String> = Pool::with_size(1);
      let mut rstring = str_pool.new_from("cat");
      rstring.as_mut().push_str("s love eating mice");
      assert_eq!("cats love eating mice", *rstring);
  }

  #[test]
  fn test_as_mut_rc() {
      let str_pool : Pool<String> = Pool::with_size(1);
      let mut rstring = str_pool.new_rc_from("cat");
      rstring.as_mut().push_str("s love eating mice");
      assert_eq!("cats love eating mice", *rstring);
  }

  #[test]
  fn test_as_ref() {
      let str_pool : Pool<String> = Pool::with_size(1);
      let rstring = str_pool.new_from("cat");
      assert_eq!("cat", rstring.as_ref());
  }

  #[test]
  fn test_as_ref_rc() {
      let str_pool : Pool<String> = Pool::with_size(1);
      let rstring = str_pool.new_rc_from("cat");
      assert_eq!("cat", rstring.as_ref());
  }

  #[test]
  fn test_recycle() {
      let str_pool : Pool<String> = Pool::with_size(1);
      {
        assert_eq!(1, str_pool.size());
        let _rstring = str_pool.new_from("cat");
        assert_eq!(0, str_pool.size());
      }
      assert_eq!(1, str_pool.size());
  }

  #[test]
  fn test_recycle_rc() {
      let str_pool : Pool<String> = Pool::with_size(1);
      {
        assert_eq!(1, str_pool.size());
        let _rstring = str_pool.new_rc_from("cat");
        assert_eq!(0, str_pool.size());
      }
      assert_eq!(1, str_pool.size());
  }

  #[test]
  fn test_clone() {
    let str_pool : Pool<String> = Pool::with_size(2);
    {
      assert_eq!(2, str_pool.size());
      let text = str_pool.new_from("cat");
      let text_clone = text.clone();
      assert_eq!(text, text_clone);
      assert_eq!(0, str_pool.size());
    }
    assert_eq!(2, str_pool.size());
  }

  #[test]
  fn test_rc_clone() {
    let str_pool : Pool<String> = Pool::with_size(2);
    {
      assert_eq!(2, str_pool.size());
      let text = str_pool.new_rc_from("cat");
      let text_clone = text.clone();
      assert_eq!(text, text_clone);
      assert_eq!(0, str_pool.size());
    }
    assert_eq!(2, str_pool.size());
  }

  #[test]
  fn test_size_cap() {
      let str_pool : Pool<String> = Pool::with_size_and_max(1, 1);
      {
        assert_eq!(1, str_pool.size());
        let _rstring = str_pool.new_from("dog");
        let _rstring2 = str_pool.new_from("cat");
        assert_eq!(0, str_pool.size());
      }
      assert_eq!(1, str_pool.size());
  }

  #[test]
  fn test_detach() {
      let str_pool : Pool<String> = Pool::with_size(1);
      {
        assert_eq!(1, str_pool.size());
        let _string : String = str_pool.new().detach();
        assert_eq!(0, str_pool.size());
      }
      assert_eq!(0, str_pool.size());
  }

  #[test]
  fn test_detach_rc() {
      let str_pool : Pool<String> = Pool::with_size(1);
      {
        assert_eq!(1, str_pool.size());
        let _string : String = str_pool.new_rc().detach();
        assert_eq!(0, str_pool.size());
      }
      assert_eq!(0, str_pool.size());
  }

  #[test]
  fn test_attach() {
      let str_pool : Pool<String> = Pool::with_size(1);
      {
        assert_eq!(1, str_pool.size());
        let string: String = str_pool.new().detach();
        assert_eq!(0, str_pool.size());
        let _rstring: Recycled<String> = str_pool.attach(string);
      }
      assert_eq!(1, str_pool.size());
  }

  #[test]
  fn test_attach_rc() {
      let str_pool : Pool<String> = Pool::with_size(1);
      {
        assert_eq!(1, str_pool.size());
        let string: String = str_pool.new_rc().detach();
        assert_eq!(0, str_pool.size());
        let _rstring: RcRecycled<String> = str_pool.attach_rc(string);
      }
      assert_eq!(1, str_pool.size());
  }

  #[test]
  fn test_builder() {
    let pool = pool()
        .with(StartingSize(128))
        .with(MaxSize(1_024))
        .with(Supplier(|| String::with_capacity(16_000)))
        .build();
    assert_eq!(pool.size(), 128);
    assert_eq!(pool.max_size(), 1_024);
    assert_eq!(pool.new().capacity(), 16_000);
  }

  #[test]
    fn test_new_from_iter(){
        let vec_pool : Pool<Vec<i32>> = Pool::with_size(1);
        {
            let vec4 = vec_pool.new_from(0..4);
            assert_eq!(4, vec4.len())
        }
        {
            let vec3 = vec_pool.new_from(0..3);
            assert_eq!(3, vec3.len())
        }
    }
}