Skip to content

Commit

Permalink
Unconditionally import from alloc/core
Browse files Browse the repository at this point in the history
Instead of doing this:
```
 #[cfg(not(feature = "std"))]
 use alloc::borrow::Cow;
 #[cfg(feature = "std")]
 use std::borrow::Cow;
```

Just unconditionally import from alloc:
```
use alloc::borrow::Cow;
```

Enable #[warn(clippy::std_instead_of_alloc, clippy::std_instead_of_core)]
to warn about these imports in the future.

Conditionally declare #[no_std] if and only if not(cfg(feature = "std"))
This is cleaner and avoids an `extern crate std` declaration,
but semantically there should be no difference.
  • Loading branch information
Techcable committed Sep 23, 2024
1 parent 2be678c commit 6bd9652
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 49 deletions.
25 changes: 0 additions & 25 deletions src/key/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
#[cfg(not(feature = "std"))]
use alloc::borrow::Cow;
#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};
#[cfg(not(feature = "std"))]
use core::clone::Clone;
#[cfg(not(feature = "std"))]
use core::cmp::PartialEq;
#[cfg(not(feature = "std"))]
use core::convert::{AsRef, From};
#[cfg(not(feature = "std"))]
use core::fmt;
#[cfg(not(feature = "std"))]
use core::hash::{Hash, Hasher};
#[cfg(not(feature = "std"))]
use core::iter::{FromIterator, IntoIterator};

#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(feature = "std")]
use std::cmp::PartialEq;
#[cfg(feature = "std")]
use std::convert::{AsRef, From};
#[cfg(feature = "std")]
use std::fmt;
#[cfg(feature = "std")]
use std::hash::{Hash, Hasher};
#[cfg(feature = "std")]
use std::iter::{FromIterator, IntoIterator};
#[cfg(feature = "std")]
use std::string::String;
#[cfg(feature = "std")]
use std::string::ToString;

/// Opaque Key is a representation of a key.
///
/// It is owned, and largely forms a contract for
Expand Down
34 changes: 10 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,41 +279,27 @@
// {{{ Imports & meta
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![no_std]
#![warn(
// Use `core` and `alloc` instead of `std` wherever possible
clippy::alloc_instead_of_core,
clippy::std_instead_of_core,
clippy::std_instead_of_alloc,
)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;
#[macro_use]
#[cfg(feature = "std")]
extern crate std;

mod key;
pub use self::key::Key;
#[cfg(not(feature = "std"))]

use alloc::borrow::{Cow, ToOwned};
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::rc::Rc;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::{sync::Arc, vec::Vec};

use core::str::FromStr;
use core::{convert, fmt, result};
#[cfg(feature = "std")]
use std::borrow::{Cow, ToOwned};
#[cfg(feature = "std")]
use std::boxed::Box;
#[cfg(feature = "std")]
use std::rc::Rc;
#[cfg(feature = "std")]
use std::string::String;
#[cfg(feature = "std")]
use std::sync::Arc;
#[cfg(feature = "std")]
use std::vec::Vec;
// }}}

// {{{ Macros
Expand Down Expand Up @@ -3926,7 +3912,7 @@ pub enum Error {
/// `io::Error` (not available in ![no_std] mode)
Io(std::io::Error),
/// `fmt::Error`
Fmt(std::fmt::Error),
Fmt(core::fmt::Error),
/// Other error
Other,
}
Expand Down Expand Up @@ -3996,7 +3982,7 @@ impl std::error::Error for Error {

#[cfg(feature = "std")]
impl core::fmt::Display for Error {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
Error::Io(ref e) => e.fmt(fmt),
Error::Fmt(ref e) => e.fmt(fmt),
Expand Down

0 comments on commit 6bd9652

Please sign in to comment.