Skip to content

Commit a460c7a

Browse files
authored
Merge pull request #358 from red15/fastrand_replacement
using fastrand instead of rand crate
2 parents 8540941 + 6bcd2a4 commit a460c7a

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fs = ["async-std"]
2727
serde = ["serde_qs", "serde_crate", "serde_json", "serde_urlencoded", "url/serde"]
2828

2929
[dependencies]
30-
rand = "0.7.3"
30+
fastrand = "1.4.0"
3131
base64 = "0.13.0"
3232
futures-lite = "1.11.1"
3333
async-channel = "1.5.1"

src/trace/trace_context.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rand::Rng;
21
use std::fmt;
32

43
use crate::headers::{Header, HeaderName, HeaderValue, Headers, TRACEPARENT};
@@ -60,12 +59,10 @@ impl TraceContext {
6059
/// assert_eq!(context.sampled(), true);
6160
/// ```
6261
pub fn new() -> Self {
63-
let mut rng = rand::thread_rng();
64-
6562
Self {
66-
id: rng.gen(),
63+
id: fastrand::u64(..),
6764
version: 0,
68-
trace_id: rng.gen(),
65+
trace_id: fastrand::u128(..),
6966
parent_id: None,
7067
flags: 1,
7168
}
@@ -104,7 +101,6 @@ impl TraceContext {
104101
/// ```
105102
pub fn from_headers(headers: impl AsRef<Headers>) -> crate::Result<Option<Self>> {
106103
let headers = headers.as_ref();
107-
let mut rng = rand::thread_rng();
108104

109105
let traceparent = match headers.get(TRACEPARENT) {
110106
Some(header) => header,
@@ -113,7 +109,7 @@ impl TraceContext {
113109
let parts: Vec<&str> = traceparent.as_str().split('-').collect();
114110

115111
Ok(Some(Self {
116-
id: rng.gen(),
112+
id: fastrand::u64(..),
117113
version: u8::from_str_radix(parts[0], 16)?,
118114
trace_id: u128::from_str_radix(parts[1], 16).status(400)?,
119115
parent_id: Some(u64::from_str_radix(parts[2], 16).status(400)?),
@@ -126,10 +122,8 @@ impl TraceContext {
126122
/// The child will have a new randomly genrated `id` and its `parent_id` will be set to the
127123
/// `id` of this TraceContext.
128124
pub fn child(&self) -> Self {
129-
let mut rng = rand::thread_rng();
130-
131125
Self {
132-
id: rng.gen(),
126+
id: fastrand::u64(..),
133127
version: self.version,
134128
trace_id: self.trace_id,
135129
parent_id: Some(self.id),

0 commit comments

Comments
 (0)