limq

Check-in Differences
Login

Check-in Differences

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Difference From limq-0.1.3 To trunk

2024-10-05
17:47
Add success and fail tests for try_return. Leaf check-in: 6f677d36a6 user: jan tags: trunk
15:31
Docs. check-in: 9860ad541a user: jan tags: trunk
01:29
Release maintenance. check-in: c213f001af user: jan tags: limq-0.1.3, trunk
01:26
Add LimQ::try_return(). check-in: 3fec62e1b7 user: jan tags: trunk

Changes to src/lib.rs.

229
230
231
232
233
234
235
236


237
238
239


240
241
242
243
244
245
246
229
230
231
232
233
234
235

236
237
238


239
240
241
242
243
244
245
246
247







-
+
+

-
-
+
+







      while self.q.len() > (max_len - 1) {
        let _ = self.q.pop_front();
      }
    }
    self.q.push_back(n);
  }

  /// Attempt to push node onto queue so it will be the next in line.
  /// Attempt to push node onto queue so it will be the next in line to be
  /// taken off it.
  ///
  /// Meant to be used to return an element to the queue that was just taken
  /// off it, but is currently not available to be processed.
  /// Can be used to return an element to the queue that was just taken off it,
  /// but is currently not available to be processed.
  ///
  /// ```
  /// use limq::LimQ;
  ///
  /// // Construct a queue with a maximum 2 element length limit
  /// let mut q: LimQ<u32> = LimQ::new(Some(2));
  ///
322
323
324
325
326
327
328
329






330

































331
323
324
325
326
327
328
329

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370







-
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

  fn force_on_full() {
    let mut q: LimQ<u32> = LimQ::new(Some(1));
    q.force_push(42);
    q.force_push(11);

    assert_eq!(q.pop(), Some(11));
  }
}

  #[test]
  fn try_return_success() {
    let mut q: LimQ<u32> = LimQ::new(Some(2));
    q.try_push(1).unwrap();
    q.try_push(2).unwrap();

    let n = q.pop().unwrap();
    assert_eq!(n, 1);

    q.try_return(n).unwrap();

    assert_eq!(q.pop(), Some(1));
    assert_eq!(q.pop(), Some(2));
    assert_eq!(q.pop(), None);
  }

  #[test]
  fn try_return_fail() {
    let mut q: LimQ<u32> = LimQ::new(Some(2));
    q.try_push(1).unwrap();
    q.try_push(2).unwrap();

    let n = q.pop().unwrap();
    assert_eq!(n, 1);

    // fill up queue again
    q.try_push(3).unwrap();

    let Err(n) = q.try_return(n) else {
      panic!("Unexpectedly successful");
    };
    assert_eq!(n, 1);

    assert_eq!(q.pop(), Some(2));
    assert_eq!(q.pop(), Some(3));
    assert_eq!(q.pop(), None);
  }
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :