Skip to content

Commit

Permalink
Unify std::fmt imports and Display & Debug trait implementations (
Browse files Browse the repository at this point in the history
#1782)

This changes all uses of `Debug` and `Display` to be consistent with imports.
What that means is all of the relevant code will look this: 
```
use std::fmt;

impl fmt::Display for T {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // ...
    }
}

impl fmt::Debug for T {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // ...
    }
}
```
  • Loading branch information
nickelc authored and arqunis committed Apr 18, 2022
1 parent 14fc9c6 commit 7a8f2cf
Show file tree
Hide file tree
Showing 41 changed files with 160 additions and 169 deletions.
20 changes: 7 additions & 13 deletions examples/e17_message_components/src/main.rs
@@ -1,10 +1,4 @@
use std::{
env,
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
str::FromStr,
time::Duration,
};
use std::{env, error::Error as StdError, fmt, str::FromStr, time::Duration};

use dotenv::dotenv;
use serenity::{
Expand Down Expand Up @@ -34,16 +28,16 @@ enum Animal {
#[derive(Debug)]
struct ParseComponentError(String);

impl Display for ParseComponentError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for ParseComponentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Failed to parse {} as component", self.0)
}
}

impl StdError for ParseComponentError {}

impl Display for Animal {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for Animal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cat => write!(f, "Cat"),
Self::Dog => write!(f, "Dog"),
Expand Down Expand Up @@ -115,8 +109,8 @@ enum Sound {
Honk,
}

impl Display for Sound {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for Sound {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Meow => write!(f, "meow"),
Self::Woof => write!(f, "woof"),
Expand Down
9 changes: 3 additions & 6 deletions src/client/bridge/gateway/mod.rs
Expand Up @@ -51,10 +51,7 @@ mod shard_queuer;
mod shard_runner;
mod shard_runner_message;

use std::{
fmt::{Display, Formatter, Result as FmtResult},
time::Duration as StdDuration,
};
use std::{fmt, time::Duration as StdDuration};

pub use self::shard_manager::{ShardManager, ShardManagerOptions};
pub use self::shard_manager_monitor::{ShardManagerError, ShardManagerMonitor};
Expand Down Expand Up @@ -132,8 +129,8 @@ pub enum ShardQueuerMessage {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct ShardId(pub u64);

impl Display for ShardId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for ShardId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/client/error.rs
@@ -1,7 +1,4 @@
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
};
use std::{error::Error as StdError, fmt};

/// An error returned from the [`Client`].
///
Expand All @@ -22,8 +19,8 @@ pub enum Error {
Shutdown,
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::ShardBootFailure => f.write_str("Failed to (re-)boot a shard"),
Error::Shutdown => f.write_str("The clients shards shutdown"),
Expand Down
5 changes: 3 additions & 2 deletions src/collector/component_interaction_collector.rs
@@ -1,5 +1,6 @@
use std::{
boxed::Box,
fmt,
future::Future,
pin::Pin,
sync::Arc,
Expand Down Expand Up @@ -179,8 +180,8 @@ struct FilterOptions {
message_id: Option<u64>,
}

impl std::fmt::Debug for FilterOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Debug for FilterOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ComponentInteractionFilter")
.field("collect_limit", &self.collect_limit)
.field("filter", &"Option<Arc<dyn Fn(&Arc<Reaction>) -> bool + 'static + Send + Sync>>")
Expand Down
9 changes: 3 additions & 6 deletions src/collector/error.rs
@@ -1,7 +1,4 @@
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
};
use std::{error::Error as StdError, fmt};

/// An error that occurred while working with a collector.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -31,8 +28,8 @@ pub enum Error {
InvalidEventIdFilters,
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NoEventTypes => f.write_str("No event types provided"),
Error::InvalidEventIdFilters => {
Expand Down
5 changes: 3 additions & 2 deletions src/collector/event_collector.rs
@@ -1,5 +1,6 @@
use std::{
boxed::Box,
fmt,
future::Future,
pin::Pin,
sync::Arc,
Expand Down Expand Up @@ -135,8 +136,8 @@ impl EventFilter {
#[derive(Clone)]
struct FilterFn(Arc<dyn Fn(&Arc<Event>) -> bool + 'static + Send + Sync>);

impl std::fmt::Debug for FilterFn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Debug for FilterFn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Arc<dyn Fn(&Arc<Event>) -> bool + 'static + Send + Sync>")
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/collector/message_collector.rs
@@ -1,5 +1,6 @@
use std::{
boxed::Box,
fmt,
future::Future,
pin::Pin,
sync::Arc,
Expand Down Expand Up @@ -246,8 +247,8 @@ impl Future for CollectReply {
}
}

impl std::fmt::Debug for FilterOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Debug for FilterOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MessageFilter")
.field("collect_limit", &self.collect_limit)
.field("filter", &"Option<Arc<dyn Fn(&Arc<Message>) -> bool + 'static + Send + Sync>>")
Expand Down
5 changes: 3 additions & 2 deletions src/collector/modal_interaction_collector.rs
@@ -1,5 +1,6 @@
use std::{
boxed::Box,
fmt,
future::Future,
pin::Pin,
sync::Arc,
Expand Down Expand Up @@ -182,8 +183,8 @@ struct FilterOptions {
message_id: Option<u64>,
}

impl std::fmt::Debug for FilterOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Debug for FilterOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ModalInteractionFilter")
.field("collect_limit", &self.collect_limit)
.field("filter", &"Option<Arc<dyn Fn(&Arc<Reaction>) -> bool + 'static + Send + Sync>>")
Expand Down
5 changes: 3 additions & 2 deletions src/collector/reaction_collector.rs
@@ -1,5 +1,6 @@
use std::{
boxed::Box,
fmt,
future::Future,
pin::Pin,
sync::Arc,
Expand Down Expand Up @@ -369,8 +370,8 @@ impl Future for CollectReaction {
}
}

impl std::fmt::Debug for FilterOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Debug for FilterOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReactionFilter")
.field("collect_limit", &self.collect_limit)
.field("filter", &"Option<Arc<dyn Fn(&Arc<Reaction>) -> bool + 'static + Send + Sync>>")
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
@@ -1,6 +1,6 @@
use std::{
error::Error as StdError,
fmt::{self, Display, Error as FormatError},
fmt::{self, Error as FormatError},
io::Error as IoError,
num::ParseIntError,
};
Expand Down Expand Up @@ -203,7 +203,7 @@ impl From<ReqwestError> for Error {
}
}

impl Display for Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Decode(msg, _) | Error::Other(msg) => f.write_str(msg),
Expand Down
5 changes: 3 additions & 2 deletions src/framework/standard/structures/buckets.rs
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fmt;
use std::time::{Duration, Instant};

use futures::future::BoxFuture;
Expand Down Expand Up @@ -322,8 +323,8 @@ impl TicketCounter {
#[derive(Debug)]
pub struct RevertBucket;

impl std::fmt::Display for RevertBucket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for RevertBucket {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("RevertBucket")
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/framework/standard/structures/check.rs
@@ -1,5 +1,5 @@
use std::error::Error;
use std::fmt::{self, Debug, Display};
use std::fmt;

use futures::future::BoxFuture;

Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct Check {
pub display_in_help: bool,
}

impl Debug for Check {
impl fmt::Debug for Check {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Check")
.field("name", &self.name)
Expand All @@ -65,7 +65,7 @@ impl Debug for Check {
}
}

impl Display for Reason {
impl fmt::Display for Reason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unknown => f.write_str("Unknown"),
Expand Down
9 changes: 3 additions & 6 deletions src/gateway/error.rs
@@ -1,7 +1,4 @@
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
};
use std::{error::Error as StdError, fmt};

use async_tungstenite::tungstenite::protocol::CloseFrame;

Expand Down Expand Up @@ -57,8 +54,8 @@ pub enum Error {
DisallowedGatewayIntents,
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::BuildingUrl => f.write_str("Error building url"),
Error::Closed(_) => f.write_str("Connection closed"),
Expand Down
6 changes: 3 additions & 3 deletions src/gateway/mod.rs
Expand Up @@ -50,7 +50,7 @@ mod error;
mod shard;
mod ws_client_ext;

use std::fmt::{Display, Formatter, Result as FmtResult};
use std::fmt;

pub use self::{
error::Error as GatewayError,
Expand Down Expand Up @@ -132,8 +132,8 @@ impl ConnectionStage {
}
}

impl Display for ConnectionStage {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for ConnectionStage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::ConnectionStage::*;

f.write_str(match *self {
Expand Down
9 changes: 3 additions & 6 deletions src/http/error.rs
@@ -1,7 +1,4 @@
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
};
use std::{error::Error as StdError, fmt};

use reqwest::{header::InvalidHeaderValue, Error as ReqwestError, Response, StatusCode, Url};
use url::ParseError as UrlError;
Expand Down Expand Up @@ -136,8 +133,8 @@ impl From<InvalidHeaderValue> for Error {
}
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::UnsuccessfulRequest(e) => {
f.write_str(&e.error.message)?;
Expand Down
12 changes: 4 additions & 8 deletions src/internal/ws_impl.rs
@@ -1,9 +1,5 @@
#[cfg(all(feature = "rustls_backend_marker", not(feature = "native_tls_backend_marker")))]
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
io::Error as IoError,
};
use std::{error::Error as StdError, fmt, io::Error as IoError};

use async_trait::async_trait;
use async_tungstenite::tungstenite::Message;
Expand Down Expand Up @@ -106,14 +102,14 @@ impl From<IoError> for RustlsError {
}

#[cfg(all(feature = "rustls_backend_marker", not(feature = "native_tls_backend_marker")))]
impl Display for RustlsError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
impl fmt::Display for RustlsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RustlsError::WebPKI => f.write_str("Failed to validate X.509 certificate"),
RustlsError::HandshakeError => {
f.write_str("TLS handshake failed when making the websocket connection")
},
RustlsError::Io(inner) => Display::fmt(&inner, f),
RustlsError::Io(inner) => fmt::Display::fmt(&inner, f),
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/model/channel/guild_channel.rs
@@ -1,5 +1,4 @@
#[cfg(feature = "model")]
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::fmt;
#[cfg(feature = "model")]
use std::sync::Arc;

Expand Down Expand Up @@ -1289,10 +1288,10 @@ impl GuildChannel {
}
}

impl Display for GuildChannel {
impl fmt::Display for GuildChannel {
/// Formats the channel, creating a mention of it.
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Display::fmt(&self.id.mention(), f)
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.id.mention(), f)
}
}

Expand Down

0 comments on commit 7a8f2cf

Please sign in to comment.