Skip to content

Commit

Permalink
Lower Rustc version requirement to 1.9 for rand_core
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Apr 21, 2018
1 parent bd34459 commit c070b43
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ matrix:
- cargo test --tests --no-default-features
- cargo test --package rand_core --no-default-features
- cargo test --features serde1,log
- rust: 1.9.0
install:
script:
# Ensure minimum rustc version for rand_core.
- cargo test --package rand_core --lib
- rust: stable
os: osx
install:
Expand Down
12 changes: 11 additions & 1 deletion rand_core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-lang-nursery/rand?svg=true)](https://ci.appveyor.com/project/alexcrichton/rand)
[![Latest version](https://img.shields.io/crates/v/rand_core.svg)](https://crates.io/crates/rand_core)
[![Documentation](https://docs.rs/rand_core/badge.svg)](https://docs.rs/rand_core)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-yellow.svg)](https://github.com/rust-lang-nursery/rand#rust-version-requirements)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.9+-yellow.svg)](https://github.com/rust-lang-nursery/rand#rust-version-requirements)

Core traits and error types of the [rand] library, plus tools for implementing
RNGs.
Expand Down Expand Up @@ -53,6 +53,16 @@ default features will also enable `std` support in `rand_core`.
The `serde1` feature can be used to derive `Serialize` and `Deserialize` for RNG
implementations that use the `BlockRng` or `BlockRng64` wrappers.

### Rust version requirements

`rand_core` works with a wide range of Rustc versions, with the oldest supported
version being **Rustc 1.9.0**. The optional `serde1` feature requires a higher
Rustc version, as it depends on the minimum version supported by Serde.

Travis CI always has a build with a pinned version of Rustc matching the oldest
supported Rust release. The current policy is that this can be updated in any
`rand_core` release if required, but the change must be noted in the changelog.


# License

Expand Down
33 changes: 20 additions & 13 deletions rand_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ pub struct Error {

impl Error {
/// Create a new instance, with specified kind and a message.
#[cfg(feature="std")]
pub fn new(kind: ErrorKind, msg: &'static str) -> Self {
#[cfg(feature="std")] {
Error { kind, msg, cause: None }
}
#[cfg(not(feature="std"))] {
Error { kind, msg }
}
Error { kind: kind, msg: msg, cause: None }
}
/// Create a new instance, with specified kind and a message.
#[cfg(not(feature="std"))]
pub fn new(kind: ErrorKind, msg: &'static str) -> Self {
Error { kind: kind, msg: msg, cause: None }
}

/// Create a new instance, with specified kind, message, and a
Expand All @@ -119,7 +120,7 @@ impl Error {
pub fn with_cause<E>(kind: ErrorKind, msg: &'static str, cause: E) -> Self
where E: Into<Box<stdError + Send + Sync>>
{
Error { kind, msg, cause: Some(cause.into()) }
Error { kind: kind, msg: msg, cause: Some(cause.into()) }
}

/// Create a new instance, with specified kind, message, and a
Expand All @@ -128,7 +129,7 @@ impl Error {
/// In `no_std` mode the *cause* is ignored.
#[cfg(not(feature="std"))]
pub fn with_cause<E>(kind: ErrorKind, msg: &'static str, _cause: E) -> Self {
Error { kind, msg }
Error { kind: kind, msg: msg }
}

/// Take the cause, if any. This allows the embedded cause to be extracted.
Expand All @@ -139,18 +140,24 @@ impl Error {
}
}

#[cfg(feature="std")]
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[cfg(feature="std")] {
if let Some(ref cause) = self.cause {
return write!(f, "{} ({}); cause: {}",
self.msg, self.kind.description(), cause);
}
if let Some(ref cause) = self.cause {
return write!(f, "{} ({}); cause: {}",
self.msg, self.kind.description(), cause);
}
write!(f, "{} ({})", self.msg, self.kind.description())
}
}

#[cfg(not(feature="std"))]
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ({})", self.msg, self.kind.description())
}
}

#[cfg(feature="std")]
impl stdError for Error {
fn description(&self) -> &str {
Expand Down
8 changes: 4 additions & 4 deletions rand_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<R: BlockRngCore> BlockRng<R> {
pub fn new(core: R) -> BlockRng<R>{
let results_empty = R::Results::default();
BlockRng {
core,
core: core,
index: results_empty.as_ref().len(),
results: results_empty,
}
Expand Down Expand Up @@ -355,7 +355,7 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
}

fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
Ok(Self::new(R::from_rng(rng)?))
Ok(Self::new(try!(R::from_rng(rng))))
}
}

Expand Down Expand Up @@ -400,7 +400,7 @@ impl<R: BlockRngCore> BlockRng64<R> {
pub fn new(core: R) -> BlockRng64<R>{
let results_empty = R::Results::default();
BlockRng64 {
core,
core: core,
index: results_empty.as_ref().len(),
half_used: false,
results: results_empty,
Expand Down Expand Up @@ -534,7 +534,7 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
}

fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
Ok(Self::new(R::from_rng(rng)?))
Ok(Self::new(try!(R::from_rng(rng))))
}
}

Expand Down
2 changes: 1 addition & 1 deletion rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ pub trait SeedableRng: Sized {
/// [`OsRng`]: ../rand/os/struct.OsRng.html
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
let mut seed = Self::Seed::default();
rng.try_fill_bytes(seed.as_mut())?;
try!(rng.try_fill_bytes(seed.as_mut()));
Ok(Self::from_seed(seed))
}
}
Expand Down

0 comments on commit c070b43

Please sign in to comment.