Skip to content

Commit

Permalink
Fix deprecation warnings for atomics initialization
Browse files Browse the repository at this point in the history
Since Rust 1.34, `ATOMIC_*_INIT` is deprecated in favor of
`Atomic*::new`. However, this requires the latter to be `const`, which
is not the case for older Rust versions.

Alternatively, we could detect the Rust version by introducing build
scripts to the crates lacking them. However, this increases build time
for a very minor benefit, so the deprecation warnings are ignored
instead.

Fixes rust-random#736.
  • Loading branch information
vks committed Feb 21, 2019
1 parent 731f3d7 commit 240b0e8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
6 changes: 5 additions & 1 deletion rand_jitter/src/lib.rs
Expand Up @@ -75,7 +75,10 @@ pub use error::TimerError;

use core::{fmt, mem, ptr};
#[cfg(feature = "std")]
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
#[cfg(feature = "std")]
#[allow(deprecated)] // Since Rust 1.34, `Atomic*::new` is preferred.
use std::sync::atomic::ATOMIC_USIZE_INIT;

const MEMORY_BLOCKS: usize = 64;
const MEMORY_BLOCKSIZE: usize = 32;
Expand Down Expand Up @@ -167,6 +170,7 @@ impl Clone for JitterRng {

// Initialise to zero; must be positive
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
#[allow(deprecated)]
static JITTER_ROUNDS: AtomicUsize = ATOMIC_USIZE_INIT;

impl JitterRng {
Expand Down
5 changes: 4 additions & 1 deletion rand_os/src/netbsd.rs
Expand Up @@ -14,7 +14,9 @@ use super::OsRngImpl;

use std::fs::File;
use std::io::Read;
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use std::sync::atomic::{AtomicBool, Ordering};
#[allow(deprecated)] // Since Rust 1.34, `Atomic*::new` is preferred.
use std::sync::atomic::ATOMIC_BOOL_INIT;

#[derive(Clone, Debug)]
pub struct OsRng { initialized: bool }
Expand All @@ -34,6 +36,7 @@ impl OsRngImpl for OsRng {
fn test_initialized(&mut self, dest: &mut [u8], _blocking: bool)
-> Result<usize, Error>
{
#[allow(deprecated)]
static OS_RNG_INITIALIZED: AtomicBool = ATOMIC_BOOL_INIT;
if !self.initialized {
self.initialized = OS_RNG_INITIALIZED.load(Ordering::Relaxed);
Expand Down
10 changes: 8 additions & 2 deletions rand_os/src/solaris.rs
Expand Up @@ -28,7 +28,9 @@ use std::io;
use std::io::Read;
use std::fs::{File, OpenOptions};
use std::os::unix::fs::OpenOptionsExt;
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use std::sync::atomic::{AtomicBool, Ordering};
#[allow(deprecated)] // Since Rust 1.34, `Atomic*::new` is preferred.
use std::sync::atomic::ATOMIC_BOOL_INIT;
use std::cmp;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -67,6 +69,7 @@ impl OsRngImpl for OsRng {
fn test_initialized(&mut self, dest: &mut [u8], blocking: bool)
-> Result<usize, Error>
{
#[allow(deprecated)]
static OS_RNG_INITIALIZED: AtomicBool = ATOMIC_BOOL_INIT;
if !self.initialized {
self.initialized = OS_RNG_INITIALIZED.load(Ordering::Relaxed);
Expand Down Expand Up @@ -151,10 +154,13 @@ fn getrandom_try_fill(dest: &mut [u8], blocking: bool) -> Result<(), Error> {
}

fn is_getrandom_available() -> bool {
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use std::sync::atomic::{AtomicBool, Ordering};
#[allow(deprecated)] // Since Rust 1.34, `Atomic*::new` is preferred.
use std::sync::atomic::ATOMIC_BOOL_INIT;
use std::sync::{Once, ONCE_INIT};

static CHECKER: Once = ONCE_INIT;
#[allow(deprecated)]
static AVAILABLE: AtomicBool = ATOMIC_BOOL_INIT;

CHECKER.call_once(|| {
Expand Down
7 changes: 5 additions & 2 deletions src/rngs/adapter/reseeding.rs
Expand Up @@ -286,8 +286,9 @@ where R: BlockRngCore + SeedableRng + CryptoRng,
mod fork {
extern crate libc;

use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT};
use core::sync::atomic::{AtomicUsize, AtomicBool, Ordering};
#[allow(deprecated)] // Since Rust 1.34, `Atomic*::new` is preferred.
use core::sync::atomic::{ATOMIC_USIZE_INIT, ATOMIC_BOOL_INIT};

// Fork protection
//
Expand All @@ -301,12 +302,14 @@ mod fork {
// don't update `fork_counter`, so a reseed is attempted as soon as
// possible.

#[allow(deprecated)]
static RESEEDING_RNG_FORK_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;

pub fn get_fork_counter() -> usize {
RESEEDING_RNG_FORK_COUNTER.load(Ordering::Relaxed)
}

#[allow(deprecated)]
static FORK_HANDLER_REGISTERED: AtomicBool = ATOMIC_BOOL_INIT;

extern fn fork_handler() {
Expand Down

0 comments on commit 240b0e8

Please sign in to comment.