Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

devtools helpers extended #1186

Merged
merged 2 commits into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions devtools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ extern crate rand;

pub mod random_path;
pub mod test_socket;
pub mod stop_guard;

pub use random_path::*;
pub use test_socket::*;
pub use stop_guard::*;
12 changes: 11 additions & 1 deletion devtools/src/random_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ pub struct RandomTempPath {
}

pub fn random_filename() -> String {
(0..8).map(|_| ((random::<f32>() * 26.0) as u8 + 97) as char).collect()
random_str(8)
}

pub fn random_str(len: usize) -> String {
(0..len).map(|_| ((random::<f32>() * 26.0) as u8 + 97) as char).collect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random_alpha_lowercase might be a more descriptive name

Copy link
Contributor Author

@NikVolf NikVolf May 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better keep it small until we have more than just one

Copy link
Contributor

@gavofyork gavofyork May 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random::<f32>? really? what about plain old random::<u8>() % 26?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not strictly uniform, not that it matters in this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not used anywhere except tests and it's been there for ages
it is not about this pr

}

impl RandomTempPath {
Expand Down Expand Up @@ -54,6 +58,12 @@ impl RandomTempPath {
pub fn as_str(&self) -> &str {
self.path.to_str().unwrap()
}

pub fn new_in(&self, name: &str) -> String {
let mut path = self.path.clone();
path.push(name);
path.to_str().unwrap().to_owned()
}
}

impl Drop for RandomTempPath {
Expand Down
42 changes: 42 additions & 0 deletions devtools/src/stop_guard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Random path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover doc comment


use std::sync::Arc;
use std::sync::atomic::*;

pub struct StopGuard {
flag: Arc<AtomicBool>,
}

impl StopGuard {
pub fn new() -> StopGuard {
StopGuard {
flag: Arc::new(AtomicBool::new(false))
}
}

pub fn share(&self) -> Arc<AtomicBool> {
self.flag.clone()
}
}

impl Drop for StopGuard {
fn drop(&mut self) {
self.flag.store(true, Ordering::Relaxed)
}
}