-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move request id generation and checking to separate wrapper
- Loading branch information
Showing
3 changed files
with
66 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// ------------------------------------------------------------------------ | ||
// Gufo SNMP: Id Generator | ||
// ------------------------------------------------------------------------ | ||
// Copyright (C) 2023-24, Gufo Labs | ||
// See LICENSE.md for details | ||
// ------------------------------------------------------------------------ | ||
|
||
use rand::Rng; | ||
|
||
#[derive(Default)] | ||
pub struct RequestId(i64); | ||
|
||
impl RequestId { | ||
/// Get next value | ||
pub fn next(&mut self) -> i64 { | ||
let mut rng = rand::thread_rng(); | ||
let x: i64 = rng.gen(); | ||
self.0 = x & 0x7fffffff; | ||
self.0 | ||
} | ||
/// Check values for match | ||
pub fn check(&self, v: i64) -> bool { | ||
self.0 == v | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_default() { | ||
let r = RequestId::default(); | ||
assert!(r.check(0)) | ||
} | ||
|
||
#[test] | ||
fn test_check() { | ||
let mut r = RequestId::default(); | ||
let v1 = r.next(); | ||
assert!(r.check(v1)) | ||
} | ||
|
||
#[test] | ||
fn test_seq() { | ||
let mut r = RequestId::default(); | ||
let v1 = r.next(); | ||
let v2 = r.next(); | ||
assert!(v1 != v2) | ||
} | ||
} |