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 #736.
  • Loading branch information
vks committed Feb 25, 2019
1 parent b9211b1 commit 790aa8a
Show file tree
Hide file tree
Showing 5 changed files with 23 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)] // Required for compatibility with Rust < 1.24.
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
6 changes: 5 additions & 1 deletion rand_os/src/linux_android.rs
Expand Up @@ -18,7 +18,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)] // Required for compatibility with Rust < 1.24.
use std::sync::atomic::ATOMIC_BOOL_INIT;
use std::sync::{Once, ONCE_INIT};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -53,6 +55,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 @@ -160,6 +163,7 @@ fn getrandom_try_fill(dest: &mut [u8], blocking: bool) -> Result<(), Error> {

fn is_getrandom_available() -> bool {
static CHECKER: Once = ONCE_INIT;
#[allow(deprecated)]
static AVAILABLE: AtomicBool = ATOMIC_BOOL_INIT;

if NR_GETRANDOM == 0 { return false };
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)] // Required for compatibility with Rust < 1.24.
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
5 changes: 4 additions & 1 deletion rand_os/src/solarish.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, AtomicUsize};
use std::sync::atomic::{AtomicBool, Ordering, AtomicUsize};
#[allow(deprecated)] // Required for compatibility with Rust < 1.24.
use std::sync::atomic::ATOMIC_BOOL_INIT;
use std::cmp;
use std::mem;

Expand Down Expand Up @@ -68,6 +70,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
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)] // Required for compatibility with Rust < 1.24.
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 790aa8a

Please sign in to comment.