Skip to content

Commit 4023a9c

Browse files
committed
chore: improve no_std maintainability:
Similar to how `rustls` does it: https://github.com/rustls/rustls/blob/513e374b2e2ce9f1fb57ac78ab3ca053afc8f133/rustls/src/lib.rs#L353-L359 > ... `extern crate` plus the `#![no_std]` attribute changes the default prelude from `std::prelude` to `core::prelude`. That forces one to _explicitly_ import (`use`) everything that is in `std::prelude` but not in `core::prelude`. This helps maintain no-std support as even developers that are not interested in, or aware of, no-std support and / or that never run `cargo build --no-default-features` locally will get errors when they rely on `std::prelude` API.
1 parent 6914543 commit 4023a9c

File tree

16 files changed

+36
-36
lines changed

16 files changed

+36
-36
lines changed

prost-types/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ doctest = false
2020

2121
[features]
2222
default = ["std"]
23-
std = ["prost/std"]
23+
std = ["prost/std", "proptest/std"]
2424

2525
[dependencies]
2626
prost = { version = "0.12.4", path = "../prost", default-features = false, features = ["prost-derive"] }
2727

2828
[dev-dependencies]
29-
proptest = "1"
29+
proptest = { version = "1", default-features = false }

prost-types/src/datetime.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! A date/time type which exists primarily to convert [`Timestamp`]s into an RFC 3339 formatted
22
//! string.
3-
43
use core::fmt;
54

65
use crate::Duration;
@@ -583,9 +582,12 @@ impl From<DateTime> for Timestamp {
583582

584583
#[cfg(test)]
585584
mod tests {
586-
use super::*;
585+
use alloc::{format, string::ToString};
586+
587587
use proptest::prelude::*;
588588

589+
use super::*;
590+
589591
#[test]
590592
fn test_min_max() {
591593
assert_eq!(
@@ -604,8 +606,8 @@ mod tests {
604606
);
605607
}
606608

607-
#[cfg(feature = "std")]
608609
#[test]
610+
#[cfg(feature = "std")]
609611
fn test_datetime_from_timestamp() {
610612
let case = |expected: &str, secs: i64, nanos: i32| {
611613
let timestamp = Timestamp {

prost-types/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![doc(html_root_url = "https://docs.rs/prost-types/0.12.2")]
2-
31
//! Protocol Buffers well-known types.
42
//!
53
//! Note that the documentation for the types defined in this crate are generated from the Protobuf
@@ -12,7 +10,14 @@
1210
//!
1311
//! [1]: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf
1412
15-
#![cfg_attr(not(feature = "std"), no_std)]
13+
#![doc(html_root_url = "https://docs.rs/prost-types/0.12.2")]
14+
#![no_std]
15+
16+
// See: https://github.com/tokio-rs/prost/pull/1047
17+
#[cfg(any(feature = "std", test))]
18+
extern crate std;
19+
20+
extern crate alloc;
1621

1722
#[rustfmt::skip]
1823
pub mod compiler;

prost/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ default = ["derive", "std"]
2626
derive = ["dep:prost-derive"]
2727
prost-derive = ["derive"] # deprecated, please use derive feature instead
2828
no-recursion-limit = []
29-
std = []
29+
std = ["proptest/std"]
3030

3131
[dependencies]
3232
bytes = { version = "1", default-features = false }
@@ -36,7 +36,7 @@ prost-derive = { version = "0.12.4", path = "../prost-derive", optional = true }
3636
criterion = { version = "0.4", default-features = false }
3737
env_logger = { version = "0.10", default-features = false }
3838
log = "0.4"
39-
proptest = "1"
39+
proptest = { version = "1", default-features = false }
4040
rand = "0.8"
4141

4242
[[bench]]

prost/src/encoding.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,6 @@ pub mod btree_map {
14221422

14231423
#[cfg(test)]
14241424
mod test {
1425-
#[cfg(not(feature = "std"))]
14261425
use alloc::string::ToString;
14271426
use core::borrow::Borrow;
14281427
use core::fmt::Debug;

prost/src/error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Protobuf encoding and decoding errors.
22
33
use alloc::borrow::Cow;
4-
#[cfg(not(feature = "std"))]
54
use alloc::boxed::Box;
6-
#[cfg(not(feature = "std"))]
75
use alloc::vec::Vec;
86

97
use core::fmt;

prost/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#![doc(html_root_url = "https://docs.rs/prost/0.12.2")]
2-
#![cfg_attr(not(feature = "std"), no_std)]
32
#![doc = include_str!("../../README.md")]
3+
#![no_std]
4+
5+
// See: https://github.com/tokio-rs/prost/pull/1047
6+
#[cfg(any(feature = "std", test))]
7+
extern crate std;
48

59
// Re-export the alloc crate for use within derived code.
610
#[doc(hidden)]

prost/src/message.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#[cfg(not(feature = "std"))]
21
use alloc::boxed::Box;
3-
#[cfg(not(feature = "std"))]
42
use alloc::vec::Vec;
53

64
use core::fmt::Debug;

prost/src/name.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use crate::Message;
44

5-
#[cfg(not(feature = "std"))]
65
use alloc::{format, string::String};
76

87
/// Associate a type name with a [`Message`] type.

prost/src/types.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
//! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and
66
//! `prost-build`.
77
8-
#[cfg(not(feature = "std"))]
98
use alloc::string::String;
10-
#[cfg(not(feature = "std"))]
119
use alloc::vec::Vec;
1210

1311
use ::bytes::{Buf, BufMut, Bytes};

0 commit comments

Comments
 (0)