Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4b93098

Browse filesBrowse files
committed
make sure that SmallRng uses a unique seed per call
Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>
1 parent c13db04 commit 4b93098
Copy full SHA for 4b93098

1 file changed

+24-4Lines changed: 24 additions & 4 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎plotly/src/plot.rs‎

Copy file name to clipboardExpand all lines: plotly/src/plot.rs
+24-4Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::atomic::{AtomicU64, Ordering};
2+
use std::time::{SystemTime, UNIX_EPOCH};
13
use std::{fs::File, io::Write, path::Path};
24

35
use askama::Template;
@@ -16,6 +18,8 @@ use serde::Serialize;
1618

1719
use crate::{layout::Frame, Configuration, Layout};
1820

21+
static SEED_COUNTER: AtomicU64 = AtomicU64::new(0);
22+
1923
#[derive(Template)]
2024
#[template(path = "plot.html", escape = "none")]
2125
struct PlotTemplate<'a> {
@@ -270,7 +274,8 @@ impl Plot {
270274

271275
// Set up the temp file with a unique filename.
272276
let mut temp = env::temp_dir();
273-
let mut plot_name = Alphanumeric.sample_string(&mut SmallRng::seed_from_u64(42), 22);
277+
let mut plot_name =
278+
Alphanumeric.sample_string(&mut SmallRng::seed_from_u64(Self::generate_seed()), 22);
274279
plot_name.push_str(".html");
275280
plot_name = format!("plotly_{plot_name}");
276281
temp.push(plot_name);
@@ -313,7 +318,8 @@ impl Plot {
313318

314319
// Set up the temp file with a unique filename.
315320
let mut temp = env::temp_dir();
316-
let mut plot_name = Alphanumeric.sample_string(&mut SmallRng::seed_from_u64(42), 22);
321+
let mut plot_name =
322+
Alphanumeric.sample_string(&mut SmallRng::seed_from_u64(Self::generate_seed()), 22);
317323
plot_name.push_str(".html");
318324
plot_name = format!("plotly_{plot_name}");
319325
temp.push(plot_name);
@@ -371,13 +377,16 @@ impl Plot {
371377
pub fn to_inline_html(&self, plot_div_id: Option<&str>) -> String {
372378
let plot_div_id = match plot_div_id {
373379
Some(id) => id.to_string(),
374-
None => Alphanumeric.sample_string(&mut SmallRng::seed_from_u64(42), 20),
380+
None => {
381+
Alphanumeric.sample_string(&mut SmallRng::seed_from_u64(Self::generate_seed()), 20)
382+
}
375383
};
376384
self.render_inline(&plot_div_id)
377385
}
378386

379387
fn to_jupyter_notebook_html(&self) -> String {
380-
let plot_div_id = Alphanumeric.sample_string(&mut SmallRng::seed_from_u64(42), 20);
388+
let plot_div_id =
389+
Alphanumeric.sample_string(&mut SmallRng::seed_from_u64(Self::generate_seed()), 20);
381390

382391
let tmpl = JupyterNotebookPlotTemplate {
383392
plot: self,
@@ -869,6 +878,17 @@ impl Plot {
869878
.spawn()
870879
.expect(DEFAULT_HTML_APP_NOT_FOUND);
871880
}
881+
882+
/// Generate unique seeds for SmallRng such that file names and div names
883+
/// are unique random for each call
884+
pub(crate) fn generate_seed() -> u64 {
885+
let time = SystemTime::now()
886+
.duration_since(UNIX_EPOCH)
887+
.unwrap_or_default()
888+
.as_nanos() as u64;
889+
let counter = SEED_COUNTER.fetch_add(1, Ordering::Relaxed);
890+
time ^ counter
891+
}
872892
}
873893

874894
impl PartialEq for Plot {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.