From 4023a9c7a501bf7dc4b6bbe58ef248d3fc1fc154 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sun, 28 Apr 2024 11:57:19 +0200 Subject: [PATCH] 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. --- prost-types/Cargo.toml | 4 ++-- prost-types/src/datetime.rs | 8 +++++--- prost-types/src/lib.rs | 11 ++++++++--- prost/Cargo.toml | 4 ++-- prost/src/encoding.rs | 1 - prost/src/error.rs | 2 -- prost/src/lib.rs | 6 +++++- prost/src/message.rs | 2 -- prost/src/name.rs | 1 - prost/src/types.rs | 2 -- tests/src/bootstrap.rs | 1 + tests/src/debug.rs | 7 ++++--- tests/src/deprecated_field.rs | 1 - tests/src/lib.rs | 10 +++++----- tests/src/message_encoding.rs | 5 ++--- tests/src/skip_debug.rs | 7 ++----- 16 files changed, 36 insertions(+), 36 deletions(-) diff --git a/prost-types/Cargo.toml b/prost-types/Cargo.toml index 453536918..fe339c267 100644 --- a/prost-types/Cargo.toml +++ b/prost-types/Cargo.toml @@ -20,10 +20,10 @@ doctest = false [features] default = ["std"] -std = ["prost/std"] +std = ["prost/std", "proptest/std"] [dependencies] prost = { version = "0.12.4", path = "../prost", default-features = false, features = ["prost-derive"] } [dev-dependencies] -proptest = "1" +proptest = { version = "1", default-features = false } diff --git a/prost-types/src/datetime.rs b/prost-types/src/datetime.rs index 2435ffe73..3ede20c6d 100644 --- a/prost-types/src/datetime.rs +++ b/prost-types/src/datetime.rs @@ -1,6 +1,5 @@ //! A date/time type which exists primarily to convert [`Timestamp`]s into an RFC 3339 formatted //! string. - use core::fmt; use crate::Duration; @@ -583,9 +582,12 @@ impl From for Timestamp { #[cfg(test)] mod tests { - use super::*; + use alloc::{format, string::ToString}; + use proptest::prelude::*; + use super::*; + #[test] fn test_min_max() { assert_eq!( @@ -604,8 +606,8 @@ mod tests { ); } - #[cfg(feature = "std")] #[test] + #[cfg(feature = "std")] fn test_datetime_from_timestamp() { let case = |expected: &str, secs: i64, nanos: i32| { let timestamp = Timestamp { diff --git a/prost-types/src/lib.rs b/prost-types/src/lib.rs index 3196f4426..4cc1babe1 100644 --- a/prost-types/src/lib.rs +++ b/prost-types/src/lib.rs @@ -1,5 +1,3 @@ -#![doc(html_root_url = "https://docs.rs/prost-types/0.12.2")] - //! Protocol Buffers well-known types. //! //! Note that the documentation for the types defined in this crate are generated from the Protobuf @@ -12,7 +10,14 @@ //! //! [1]: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf -#![cfg_attr(not(feature = "std"), no_std)] +#![doc(html_root_url = "https://docs.rs/prost-types/0.12.2")] +#![no_std] + +// See: https://github.com/tokio-rs/prost/pull/1047 +#[cfg(any(feature = "std", test))] +extern crate std; + +extern crate alloc; #[rustfmt::skip] pub mod compiler; diff --git a/prost/Cargo.toml b/prost/Cargo.toml index 965505733..a48a031dd 100644 --- a/prost/Cargo.toml +++ b/prost/Cargo.toml @@ -26,7 +26,7 @@ default = ["derive", "std"] derive = ["dep:prost-derive"] prost-derive = ["derive"] # deprecated, please use derive feature instead no-recursion-limit = [] -std = [] +std = ["proptest/std"] [dependencies] bytes = { version = "1", default-features = false } @@ -36,7 +36,7 @@ prost-derive = { version = "0.12.4", path = "../prost-derive", optional = true } criterion = { version = "0.4", default-features = false } env_logger = { version = "0.10", default-features = false } log = "0.4" -proptest = "1" +proptest = { version = "1", default-features = false } rand = "0.8" [[bench]] diff --git a/prost/src/encoding.rs b/prost/src/encoding.rs index 88d4fe891..f535376e9 100644 --- a/prost/src/encoding.rs +++ b/prost/src/encoding.rs @@ -1422,7 +1422,6 @@ pub mod btree_map { #[cfg(test)] mod test { - #[cfg(not(feature = "std"))] use alloc::string::ToString; use core::borrow::Borrow; use core::fmt::Debug; diff --git a/prost/src/error.rs b/prost/src/error.rs index 6572e502d..756ee8172 100644 --- a/prost/src/error.rs +++ b/prost/src/error.rs @@ -1,9 +1,7 @@ //! Protobuf encoding and decoding errors. use alloc::borrow::Cow; -#[cfg(not(feature = "std"))] use alloc::boxed::Box; -#[cfg(not(feature = "std"))] use alloc::vec::Vec; use core::fmt; diff --git a/prost/src/lib.rs b/prost/src/lib.rs index 32b36f5cf..5498818cd 100644 --- a/prost/src/lib.rs +++ b/prost/src/lib.rs @@ -1,6 +1,10 @@ #![doc(html_root_url = "https://docs.rs/prost/0.12.2")] -#![cfg_attr(not(feature = "std"), no_std)] #![doc = include_str!("../../README.md")] +#![no_std] + +// See: https://github.com/tokio-rs/prost/pull/1047 +#[cfg(any(feature = "std", test))] +extern crate std; // Re-export the alloc crate for use within derived code. #[doc(hidden)] diff --git a/prost/src/message.rs b/prost/src/message.rs index 98235ea9f..bc628f001 100644 --- a/prost/src/message.rs +++ b/prost/src/message.rs @@ -1,6 +1,4 @@ -#[cfg(not(feature = "std"))] use alloc::boxed::Box; -#[cfg(not(feature = "std"))] use alloc::vec::Vec; use core::fmt::Debug; diff --git a/prost/src/name.rs b/prost/src/name.rs index 1e4a36e98..4ba54a898 100644 --- a/prost/src/name.rs +++ b/prost/src/name.rs @@ -2,7 +2,6 @@ use crate::Message; -#[cfg(not(feature = "std"))] use alloc::{format, string::String}; /// Associate a type name with a [`Message`] type. diff --git a/prost/src/types.rs b/prost/src/types.rs index 9aa53a9e7..864a2adda 100644 --- a/prost/src/types.rs +++ b/prost/src/types.rs @@ -5,9 +5,7 @@ //! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and //! `prost-build`. -#[cfg(not(feature = "std"))] use alloc::string::String; -#[cfg(not(feature = "std"))] use alloc::vec::Vec; use ::bytes::{Buf, BufMut, Bytes}; diff --git a/tests/src/bootstrap.rs b/tests/src/bootstrap.rs index d329cad58..a94d51d1c 100644 --- a/tests/src/bootstrap.rs +++ b/tests/src/bootstrap.rs @@ -7,6 +7,7 @@ use std::io::Read; use std::io::Write; use std::path::Path; use std::path::PathBuf; +use std::string::String; /// Test which bootstraps protobuf.rs and compiler.rs from the .proto definitions in the Protobuf /// repo. Ensures that the checked-in compiled versions are up-to-date. diff --git a/tests/src/debug.rs b/tests/src/debug.rs index 789c9e461..dccaac2e8 100644 --- a/tests/src/debug.rs +++ b/tests/src/debug.rs @@ -3,9 +3,8 @@ //! The tests check against expected output. This may be a bit fragile, but it is likely OK for //! actual use. -use prost::alloc::format; -#[cfg(not(feature = "std"))] -use prost::alloc::string::String; +use ::alloc::format; +use ::alloc::string::String; // Borrow some types from other places. #[cfg(feature = "std")] @@ -16,6 +15,8 @@ use crate::message_encoding::BasicEnumeration; #[test] #[cfg(feature = "std")] fn basic() { + use std::string::ToString; + let mut basic = Basic::default(); assert_eq!( format!("{:?}", basic), diff --git a/tests/src/deprecated_field.rs b/tests/src/deprecated_field.rs index 296e3f247..e981b4785 100644 --- a/tests/src/deprecated_field.rs +++ b/tests/src/deprecated_field.rs @@ -1,4 +1,3 @@ -#[cfg(not(feature = "std"))] use alloc::string::ToString; mod deprecated_field { diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 00926afe0..cad33fa13 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -3,7 +3,11 @@ clippy::module_inception, clippy::unreadable_literal )] -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] + +// See: https://github.com/tokio-rs/prost/pull/1036 +#[cfg(any(feature = "std", test))] +extern crate std; #[macro_use] extern crate cfg_if; @@ -14,7 +18,6 @@ cfg_if! { if #[cfg(feature = "edition-2015")] { extern crate anyhow; extern crate bytes; - extern crate core; extern crate prost; extern crate prost_types; extern crate protobuf; @@ -140,7 +143,6 @@ pub mod default_string_escape { include!(concat!(env!("OUT_DIR"), "/default_string_escape.rs")); } -#[cfg(not(feature = "std"))] use alloc::vec::Vec; use anyhow::anyhow; @@ -280,10 +282,8 @@ where #[cfg(test)] mod tests { - use alloc::collections::{BTreeMap, BTreeSet}; use alloc::vec; - #[cfg(not(feature = "std"))] use alloc::{borrow::ToOwned, boxed::Box, string::ToString}; use super::*; diff --git a/tests/src/message_encoding.rs b/tests/src/message_encoding.rs index a98052f4c..5083e8f35 100644 --- a/tests/src/message_encoding.rs +++ b/tests/src/message_encoding.rs @@ -1,6 +1,5 @@ -use prost::alloc::vec; -#[cfg(not(feature = "std"))] -use prost::alloc::{borrow::ToOwned, string::String, vec::Vec}; +use alloc::vec; +use alloc::{borrow::ToOwned, string::String, vec::Vec}; use prost::bytes::Bytes; use prost::{Enumeration, Message, Oneof}; diff --git a/tests/src/skip_debug.rs b/tests/src/skip_debug.rs index cfa9c663b..11440d0bd 100644 --- a/tests/src/skip_debug.rs +++ b/tests/src/skip_debug.rs @@ -1,10 +1,7 @@ //! Tests for skipping the default Debug implementation. -use std::fmt; - -use prost::alloc::format; -#[cfg(not(feature = "std"))] -use prost::alloc::string::String; +use alloc::string::{String, ToString}; +use alloc::{fmt, format}; use crate::custom_debug::{msg, AnEnum, Msg}; use crate::message_encoding::BasicEnumeration;