Skip to content

Commit 19b0a5a

Browse files
authored
bump getrandom to version0.3 (#194)
* bump version * change functions * so it is supported now ;o? * more fills * custom implementation * out fo the module * it should work now * more changes * more changes related to no std * serde alloc * proper names * ahh, it was important * fix error
1 parent 9a9d7f4 commit 19b0a5a

File tree

13 files changed

+91
-33
lines changed

13 files changed

+91
-33
lines changed

.github/workflows/run-validations.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ jobs:
107107
name: Check if `no_std` target compiles as expected
108108
runs-on:
109109
group: organization/Default
110+
110111
steps:
111112
- uses: actions/checkout@v5
112113

@@ -118,12 +119,16 @@ jobs:
118119

119120
- name: Run cargo check tool to check if the `no_std` code are valid
120121
uses: actions-rs/cargo@v1
122+
env:
123+
RUSTFLAGS: '--cfg getrandom_backend="unsupported"'
121124
with:
122125
command: check
123-
args: --lib --no-default-features --features=full_no_std_platform_independent,mock_getrandom
126+
args: --lib --no-default-features --features=full_no_std_platform_independent
124127

125128
- name: Run cargo check tool to check if the additional example code are valid
126129
uses: actions-rs/cargo@v1
130+
env:
131+
RUSTFLAGS: '--cfg getrandom_backend="custom"'
127132
with:
128133
command: check
129134
args: --manifest-path examples/no_std/Cargo.toml --target thumbv7m-none-eabi

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ contract_test = ["parse_token", "publish", "access", "crypto", "std", "subscribe
6767
full_no_std = ["serde", "reqwest", "crypto", "parse_token", "blocking", "publish", "access", "subscribe", "tokio", "presence"]
6868
full_no_std_platform_independent = ["serde", "crypto", "parse_token", "blocking", "publish", "access", "subscribe", "presence"]
6969
pubnub_only = ["crypto", "parse_token", "blocking", "publish", "access", "subscribe", "presence"]
70-
mock_getrandom = ["getrandom/custom"]
7170
# TODO: temporary treated as internal until we officially release it
7271
subscribe = ["dep:futures"]
7372
presence = ["dep:futures"]
@@ -91,7 +90,7 @@ sha2 = { version = "0.10", default-features = false }
9190
time = { version = "0.3", features = ["alloc"], default-features = false }
9291

9392
# serde
94-
serde = { version = "1.0", features = ["derive"], optional = true, default-features = false }
93+
serde = { version = "1.0", features = ["derive", "alloc"], optional = true, default-features = false }
9594
serde_json = { version = "1.0", optional = true, features = ["alloc"], default-features = false }
9695

9796
# reqwest
@@ -101,7 +100,7 @@ bytes = { version = "1.4", default-features = false, optional = true }
101100
# crypto
102101
aes = { version = "0.8.2", optional = true }
103102
cbc = { version = "0.1.2", optional = true }
104-
getrandom = { version = "0.2", optional = true }
103+
getrandom = { version = "0.3", optional = true }
105104

106105
# parse_token
107106
ciborium = { version = "0.2.1", default-features = false, optional = true }
@@ -115,7 +114,7 @@ async-channel = { version = "1.8", optional = true }
115114
portable-atomic = { version = "1.3", optional = true, default-features = false, features = ["require-cas", "critical-section"] }
116115

117116
[target.'cfg(target_arch = "wasm32")'.dependencies]
118-
getrandom = { version = "0.2", features = ["js"] }
117+
getrandom = { version = "0.3", features = ["wasm_js"] }
119118

120119
[dev-dependencies]
121120
async-trait = "0.1"
@@ -126,7 +125,7 @@ cucumber = { version = "0.20.2", features = ["output-junit"] }
126125
reqwest = { version = "0.12", features = ["json"] }
127126
test-case = "3.0"
128127
hashbrown = { version = "0.14.0", features = ["serde"] }
129-
getrandom = { version = "0.2", features = ["custom"] }
128+
getrandom = { version = "0.3" }
130129

131130
[build-dependencies]
132131
built = "0.6"

examples/no_std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
[dependencies]
99
pubnub = { path = "../../", default-features = false, features = ["blocking", "serde", "publish", "subscribe", "presence"] }
1010
serde = { version = "1.0", default-features = false, features = ["derive"] }
11-
getrandom = { version = "0.2", default-features = false, features = ["custom"] }
11+
getrandom = { version = "0.3", default_features = false }
1212

1313
[[bin]]
1414
name = "publish"

examples/no_std/src/here_now.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use pubnub::{
2525

2626
// As getrandom crate has limited support of targets, we need to provide custom
2727
// implementation of `getrandom` function.
28-
getrandom::register_custom_getrandom!(custom_random);
2928
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
3029
// We're using `42` as a random number, because it's the answer
3130
// to the Ultimate Question of Life, the Universe, and Everything.
@@ -37,6 +36,21 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
3736
Ok(())
3837
}
3938

39+
// This function is used to register the custom implementation of `getrandom` function.
40+
#[no_mangle]
41+
unsafe extern "Rust" fn __getrandom_v03_custom(
42+
dest: *mut u8,
43+
len: usize,
44+
) -> Result<(), getrandom::Error> {
45+
let buf = unsafe {
46+
// fill the buffer with zeros
47+
core::ptr::write_bytes(dest, 0, len);
48+
// create mutable byte slice
49+
core::slice::from_raw_parts_mut(dest, len)
50+
};
51+
custom_random(buf)
52+
}
53+
4054
// Many targets have very specific requirements for networking, so it's hard to
4155
// provide a generic implementation.
4256
// Depending on the target, you will probably need to implement `Transport` trait.

examples/no_std/src/presence_state.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ struct State {
3232

3333
// As getrandom crate has limited support of targets, we need to provide custom
3434
// implementation of `getrandom` function.
35-
getrandom::register_custom_getrandom!(custom_random);
3635
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
3736
// We're using `42` as a random number, because it's the answer
3837
// to the Ultimate Question of Life, the Universe, and Everything.
@@ -44,6 +43,21 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
4443
Ok(())
4544
}
4645

46+
// This function is used to register the custom implementation of `getrandom` function.
47+
#[no_mangle]
48+
unsafe extern "Rust" fn __getrandom_v03_custom(
49+
dest: *mut u8,
50+
len: usize,
51+
) -> Result<(), getrandom::Error> {
52+
let buf = unsafe {
53+
// fill the buffer with zeros
54+
core::ptr::write_bytes(dest, 0, len);
55+
// create mutable byte slice
56+
core::slice::from_raw_parts_mut(dest, len)
57+
};
58+
custom_random(buf)
59+
}
60+
4761
// Many targets have very specific requirements for networking, so it's hard to
4862
// provide a generic implementation.
4963
// Depending on the target, you will probably need to implement `Transport` trait.

examples/no_std/src/publish.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ struct Message {
3333

3434
// As getrandom crate has limited support of targets, we need to provide custom
3535
// implementation of `getrandom` function.
36-
getrandom::register_custom_getrandom!(custom_random);
3736
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
3837
// We're using `42` as a random number, because it's the answer
3938
// to the Ultimate Question of Life, the Universe, and Everything.
@@ -45,6 +44,21 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
4544
Ok(())
4645
}
4746

47+
// This function is used to register the custom implementation of `getrandom` function.
48+
#[no_mangle]
49+
unsafe extern "Rust" fn __getrandom_v03_custom(
50+
dest: *mut u8,
51+
len: usize,
52+
) -> Result<(), getrandom::Error> {
53+
let buf = unsafe {
54+
// fill the buffer with zeros
55+
core::ptr::write_bytes(dest, 0, len);
56+
// create mutable byte slice
57+
core::slice::from_raw_parts_mut(dest, len)
58+
};
59+
custom_random(buf)
60+
}
61+
4862
// Many targets have very specific requirements for networking, so it's hard to
4963
// provide a generic implementation.
5064
// Depending on the target, you will probably need to implement `Transport` trait.

examples/no_std/src/subscribe.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,32 @@ struct Message {
3434

3535
// As getrandom crate has limited support of targets, we need to provide custom
3636
// implementation of `getrandom` function.
37-
getrandom::register_custom_getrandom!(custom_random);
3837
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
3938
// We're using `42` as a random number, because it's the answer
4039
// to the Ultimate Question of Life, the Universe, and Everything.
41-
// In your program, you should use proper random number generator that is
42-
// supported by your target.
40+
// In your program, you should use proper random number generator that is supported by your target.
4341
for i in buf.iter_mut() {
4442
*i = 42;
4543
}
4644

4745
Ok(())
4846
}
4947

48+
// This function is used to register the custom implementation of `getrandom` function.
49+
#[no_mangle]
50+
unsafe extern "Rust" fn __getrandom_v03_custom(
51+
dest: *mut u8,
52+
len: usize,
53+
) -> Result<(), getrandom::Error> {
54+
let buf = unsafe {
55+
// fill the buffer with zeros
56+
core::ptr::write_bytes(dest, 0, len);
57+
// create mutable byte slice
58+
core::slice::from_raw_parts_mut(dest, len)
59+
};
60+
custom_random(buf)
61+
}
62+
5063
// Many targets have very specific requirements for networking, so it's hard to
5164
// provide a generic implementation.
5265
// Depending on the target, you will probably need to implement `Transport`

examples/no_std/src/where_now.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use pubnub::{
2525

2626
// As getrandom crate has limited support of targets, we need to provide custom
2727
// implementation of `getrandom` function.
28-
getrandom::register_custom_getrandom!(custom_random);
2928
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
3029
// We're using `42` as a random number, because it's the answer
3130
// to the Ultimate Question of Life, the Universe, and Everything.
@@ -37,6 +36,21 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
3736
Ok(())
3837
}
3938

39+
// This function is used to register the custom implementation of `getrandom` function.
40+
#[no_mangle]
41+
unsafe extern "Rust" fn __getrandom_v03_custom(
42+
dest: *mut u8,
43+
len: usize,
44+
) -> Result<(), getrandom::Error> {
45+
let buf = unsafe {
46+
// fill the buffer with zeros
47+
core::ptr::write_bytes(dest, 0, len);
48+
// create mutable byte slice
49+
core::slice::from_raw_parts_mut(dest, len)
50+
};
51+
custom_random(buf)
52+
}
53+
4054
// Many targets have very specific requirements for networking, so it's hard to
4155
// provide a generic implementation.
4256
// Depending on the target, you will probably need to implement `Transport` trait.

src/core/retry_policy.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
//! [`PubNub API`]: https://www.pubnub.com/docs
99
//! [`pubnub`]: ../index.html
1010
11-
use getrandom::getrandom;
12-
1311
use crate::{core::PubNubError, lib::alloc::vec::Vec};
1412

1513
/// List of known endpoint groups (by context)
@@ -310,7 +308,7 @@ impl RequestRetryConfiguration {
310308
let delay = delay_in_seconds * MICROS_IN_SECOND;
311309
let mut random_bytes = [0u8; 8];
312310

313-
if getrandom(&mut random_bytes).is_err() {
311+
if getrandom::fill(&mut random_bytes).is_err() {
314312
return Some(delay);
315313
}
316314

src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,3 @@ mod lib {
336336
}
337337
}
338338
}
339-
340-
// Mocking random for checking if `no_std` compiles.
341-
// Don't use that feature in production.
342-
#[cfg(feature = "mock_getrandom")]
343-
mod mock_getrandom {
344-
use getrandom::{register_custom_getrandom, Error};
345-
346-
pub fn do_nothing(_buf: &mut [u8]) -> Result<(), Error> {
347-
Ok(())
348-
}
349-
350-
register_custom_getrandom!(do_nothing);
351-
}

0 commit comments

Comments
 (0)