Skip to content

Commit 9d4c68b

Browse files
committed
Respond to comments
1 parent d0623be commit 9d4c68b

File tree

6 files changed

+51
-62
lines changed

6 files changed

+51
-62
lines changed

.travis.yml

+11-22
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ matrix:
2121
- rustup target add aarch64-apple-ios
2222
script:
2323
- cargo test
24-
- cargo test --examples
2524
- cargo build --target aarch64-apple-ios
2625

2726
- name: "Linux, beta"
@@ -81,41 +80,32 @@ matrix:
8180
- GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
8281
- CHROMEDRIVER=$HOME/chromedriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
8382

84-
- name: "Linux, nightly, docs"
83+
- &nightly_and_docs
84+
name: "Linux, nightly, docs"
8585
rust: nightly
8686
os: linux
8787
install:
8888
- cargo --list | egrep "^\s*deadlinks$" -q || cargo install cargo-deadlinks
8989
- cargo deadlinks -V
9090
script:
91+
# Check that our tests pass in the default configuration
9192
- cargo test
9293
- cargo test --benches
93-
- cargo test --examples
94+
# Check that setting various features does not break the build
95+
- cargo build --features=std
96+
- cargo build --features=log
97+
- cargo build --features=custom
9498
# remove cached documentation, otherwise files from previous PRs can get included
9599
- rm -rf target/doc
96-
- cargo doc --no-deps --features=std,log
100+
- cargo doc --no-deps --features=std,log,custom
97101
- cargo deadlinks --dir target/doc
98102
# also test minimum dependency versions are usable
99103
- cargo generate-lockfile -Z minimal-versions
100-
- cargo test
104+
- cargo test --features=std,log,custom
101105

102-
- name: "OSX, nightly, docs"
103-
rust: nightly
106+
- <<: *nightly_and_docs
107+
name: "OSX, nightly, docs"
104108
os: osx
105-
install:
106-
- cargo --list | egrep "^\s*deadlinks$" -q || cargo install cargo-deadlinks
107-
- cargo deadlinks -V
108-
script:
109-
- cargo test
110-
- cargo test --benches
111-
- cargo test --examples
112-
# remove cached documentation, otherwise files from previous PRs can get included
113-
- rm -rf target/doc
114-
- cargo doc --no-deps --features=std,log
115-
- cargo deadlinks --dir target/doc
116-
# also test minimum dependency versions are usable
117-
- cargo generate-lockfile -Z minimal-versions
118-
- cargo test
119109

120110
- name: "cross-platform build only"
121111
rust: nightly
@@ -201,7 +191,6 @@ before_script:
201191

202192
script:
203193
- cargo test
204-
- cargo test --examples
205194

206195
after_script: set +e
207196

custom/stdweb/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[package]
2-
name = "getrandom-stdweb"
2+
name = "stdweb-getrandom"
33
version = "0.2.0"
44
edition = "2018"
55
authors = ["The Rand Project Developers"]
66
license = "MIT OR Apache-2.0"
77
description = "Custom shim for using getrandom with stdweb"
8-
documentation = "https://docs.rs/getrandom-stdweb"
8+
documentation = "https://docs.rs/stdweb-getrandom"
99
repository = "https://github.com/rust-random/getrandom"
1010
categories = ["wasm"]
1111

1212
[dependencies]
13-
getrandom = { path = "../..", features = ["custom"] }
13+
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
1414
stdweb = "0.4.18"
1515
log = "0.4"
1616

custom/wasm-bindgen/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[package]
2-
name = "getrandom-wasm-bindgen"
2+
name = "wasm-bindgen-getrandom"
33
version = "0.2.0"
44
edition = "2018"
55
authors = ["The Rand Project Developers"]
66
license = "MIT OR Apache-2.0"
77
description = "Custom shim for using getrandom with wasm-bindgen"
8-
documentation = "https://docs.rs/getrandom-bindgen"
8+
documentation = "https://docs.rs/wasm-bindgen-getrandom"
99
repository = "https://github.com/rust-random/getrandom"
1010
categories = ["wasm"]
1111

1212
[dependencies]
13-
getrandom = { path = "../..", features = ["custom"] }
13+
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
1414
wasm-bindgen = "0.2.29"
1515

1616
[dev-dependencies]

src/custom.rs

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010
use crate::Error;
1111
use core::num::NonZeroU32;
1212

13+
/// Register a function to be invoked by `getrandom` on custom targets. This
14+
/// function will only be invoked on targets not supported by `getrandom`. This
15+
/// prevents crate dependencies from either inadvertently or maliciously
16+
/// overriding the secure RNG implementations in `getrandom`.
17+
///
18+
/// *This API requires the following crate features to be activated: `custom`*
19+
#[macro_export]
20+
macro_rules! register_custom_getrandom {
21+
($path:path) => {
22+
// We use an extern "C" function to get the guarantees of a stable ABI.
23+
#[no_mangle]
24+
extern "C" fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 {
25+
let slice = unsafe { ::std::slice::from_raw_parts_mut(dest, len) };
26+
match $path(slice) {
27+
Ok(()) => 0,
28+
Err(e) => e.code().get(),
29+
}
30+
}
31+
};
32+
}
33+
34+
#[allow(dead_code)]
1335
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
1436
extern "C" {
1537
fn __getrandom_custom(dest: *mut u8, len: usize) -> u32;

src/error.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Error {
3131
pub const UNSUPPORTED: Error = internal_error!(0);
3232
/// The platform-specific `errno` returned a non-positive value.
3333
pub const ERRNO_NOT_POSITIVE: Error = internal_error!(1);
34-
/// Invalid conversion from a non-standard [`std::io::Error`]
34+
/// Invalid conversion from a non-standard [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html)
3535
pub const UNKNOWN_IO_ERROR: Error = internal_error!(2);
3636
/// Call to [`SecRandomCopyBytes`](https://developer.apple.com/documentation/security/1399291-secrandomcopybytes) failed.
3737
pub const SEC_RANDOM_FAILED: Error = internal_error!(3);
@@ -52,13 +52,6 @@ impl Error {
5252
/// On VxWorks, random number generator is not yet initialized.
5353
pub const RAND_SECURE_FATAL: Error = internal_error!(11);
5454

55-
#[deprecated(since = "0.1.7")]
56-
/// Unknown error.
57-
pub const UNKNOWN: Error = Error::UNSUPPORTED;
58-
#[deprecated(since = "0.1.7")]
59-
/// System entropy source is unavailable.
60-
pub const UNAVAILABLE: Error = Error::UNSUPPORTED;
61-
6255
/// Codes below this point represent OS Errors (i.e. positive i32 values).
6356
/// Codes at or above this point, but below [`Error::CUSTOM_START`] are
6457
/// reserved for use by the `rand` and `getrandom` crates.
@@ -70,9 +63,11 @@ impl Error {
7063

7164
/// Extract the raw OS error code (if this error came from the OS)
7265
///
73-
/// This method is identical to `std::io::Error::raw_os_error()`, except
66+
/// This method is identical to [`std::io::Error::raw_os_error()`][1], except
7467
/// that it works in `no_std` contexts. If this method returns `None`, the
7568
/// error value can still be formatted via the `Display` implementation.
69+
///
70+
/// [1]: https://doc.rust-lang.org/std/io/struct.Error.html#method.raw_os_error
7671
#[inline]
7772
pub fn raw_os_error(self) -> Option<i32> {
7873
if self.0.get() < Self::INTERNAL_START {
@@ -171,7 +166,9 @@ fn internal_desc(error: Error) -> Option<&'static str> {
171166
Error::BINDGEN_GRV_UNDEF => Some("wasm-bindgen: crypto.getRandomValues is undefined"),
172167
Error::STDWEB_NO_RNG => Some("stdweb: no randomness source available"),
173168
Error::STDWEB_RNG_FAILED => Some("stdweb: failed to get randomness"),
174-
Error::RAND_SECURE_FATAL => Some("randSecure: random number generator module is not initialized"),
169+
Error::RAND_SECURE_FATAL => {
170+
Some("randSecure: random number generator module is not initialized")
171+
}
175172
_ => None,
176173
}
177174
}

src/lib.rs

+5-24
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ cfg_if! {
155155

156156
mod error;
157157
mod util;
158-
158+
// To prevent a breaking change when targets are added, we always export the
159+
// register_custom_getrandom macro, so old Custom RNG crates continue to build.
160+
#[cfg(feature = "custom")]
161+
mod custom;
159162
#[cfg(feature = "std")]
160163
mod error_impls;
161164

@@ -210,7 +213,7 @@ cfg_if! {
210213
)))] {
211214
#[path = "rdrand.rs"] mod imp;
212215
} else if #[cfg(feature = "custom")] {
213-
#[path = "custom.rs"] mod imp;
216+
use custom as imp;
214217
} else {
215218
compile_error!("\
216219
target is not supported, for more information see: \
@@ -219,28 +222,6 @@ cfg_if! {
219222
}
220223
}
221224

222-
/// Register a function to be invoked by `getrandom` on custom targets. This
223-
/// function will only be invoked on targets not supported by `getrandom`. This
224-
/// prevents crate dependencies from either inadvertently or maliciously
225-
/// overriding the secure RNG implementations in `getrandom`.
226-
///
227-
/// *This API requires the following crate features to be activated: `custom`*
228-
#[macro_export]
229-
#[cfg(feature = "custom")]
230-
macro_rules! register_custom_getrandom {
231-
($path:path) => {
232-
// We use an extern "C" function to get the guarantees of a stable ABI.
233-
#[no_mangle]
234-
extern "C" fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 {
235-
let slice = unsafe { ::std::slice::from_raw_parts_mut(dest, len) };
236-
match $path(slice) {
237-
Ok(()) => 0,
238-
Err(e) => e.code().get(),
239-
}
240-
}
241-
};
242-
}
243-
244225
/// Fill `dest` with random bytes from the system's preferred random number
245226
/// source.
246227
///

0 commit comments

Comments
 (0)