Skip to content

Commit

Permalink
chore: improve no_std maintainability:
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gibbz00 committed Apr 28, 2024
1 parent 6914543 commit 4023a9c
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions prost-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
8 changes: 5 additions & 3 deletions prost-types/src/datetime.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -583,9 +582,12 @@ impl From<DateTime> 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!(
Expand All @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions prost-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions prost/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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]]
Expand Down
1 change: 0 additions & 1 deletion prost/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions prost/src/error.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 5 additions & 1 deletion prost/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 0 additions & 2 deletions prost/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use core::fmt::Debug;
Expand Down
1 change: 0 additions & 1 deletion prost/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::Message;

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

/// Associate a type name with a [`Message`] type.
Expand Down
2 changes: 0 additions & 2 deletions prost/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
1 change: 1 addition & 0 deletions tests/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions tests/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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),
Expand Down
1 change: 0 additions & 1 deletion tests/src/deprecated_field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(not(feature = "std"))]
use alloc::string::ToString;

mod deprecated_field {
Expand Down
10 changes: 5 additions & 5 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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::*;
Expand Down
5 changes: 2 additions & 3 deletions tests/src/message_encoding.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
7 changes: 2 additions & 5 deletions tests/src/skip_debug.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 4023a9c

Please sign in to comment.