Skip to content

Commit d3b13f2

Browse files
committed
Improve ergonomics of the new trait
1 parent a49cdc6 commit d3b13f2

File tree

5 files changed

+16
-27
lines changed

5 files changed

+16
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ sec1_decode = "^0.1.0"
3333
base64 = "^0.13"
3434
chrono = "^0.4"
3535
log = "^0.4"
36-
async-trait = "0.1.73"
36+
async-trait = "^0.1"
3737

3838
[dev-dependencies]
3939
argparse = "^0.2"

src/clients/hyper_client.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use async_trait::async_trait;
2-
use std::convert::Infallible;
32

43
use http::header::{CONTENT_LENGTH, RETRY_AFTER};
54
use hyper::{body::HttpBody, client::HttpConnector, Body, Client, Request as HttpRequest};
@@ -22,22 +21,21 @@ pub struct HyperWebPushClient {
2221

2322
impl Default for HyperWebPushClient {
2423
fn default() -> Self {
25-
Self::new().unwrap()
24+
Self::new()
2625
}
2726
}
2827

29-
#[async_trait]
30-
impl WebPushClient for HyperWebPushClient {
31-
type CreationError = Infallible;
32-
28+
impl HyperWebPushClient {
3329
/// Creates a new client.
34-
fn new() -> Result<Self, Self::CreationError> {
35-
//This method can never fail, but returns error to match API of the isahc client.
36-
Ok(Self {
30+
pub fn new() -> Self {
31+
Self {
3732
client: Client::builder().build(HttpsConnector::new()),
38-
})
33+
}
3934
}
35+
}
4036

37+
#[async_trait]
38+
impl WebPushClient for HyperWebPushClient {
4139
/// Sends a notification. Never times out.
4240
async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError> {
4341
trace!("Message: {:?}", message);

src/clients/isahc_client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ impl Default for IsahcWebPushClient {
2626
}
2727
}
2828

29-
#[async_trait]
30-
impl WebPushClient for IsahcWebPushClient {
31-
type CreationError = WebPushError;
32-
29+
impl IsahcWebPushClient {
3330
/// Creates a new client. Can fail under resource depletion.
34-
fn new() -> Result<Self, Self::CreationError> {
31+
pub fn new() -> Result<Self, WebPushError> {
3532
Ok(Self {
3633
client: HttpClient::new()?,
3734
})
3835
}
36+
}
3937

38+
#[async_trait]
39+
impl WebPushClient for IsahcWebPushClient {
4040
/// Sends a notification. Never times out.
4141
async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError> {
4242
trace!("Message: {:?}", message);

src/clients/mod.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@ pub mod isahc_client;
1717
/// An async client for sending the notification payload.
1818
/// Other features, such as thread safety, may vary by implementation.
1919
#[async_trait]
20-
pub trait WebPushClient
21-
where
22-
Self: Sized,
23-
{
24-
/// Errors that can occur when creating a client.
25-
type CreationError;
26-
27-
/// Creates a new client.
28-
fn new() -> Result<Self, Self::CreationError>;
29-
20+
pub trait WebPushClient {
3021
/// Sends a notification. Never times out.
3122
async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError>;
3223
}

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl From<hyper::Error> for WebPushError {
7575
}
7676
}
7777

78-
#[cfg(not(feature = "hyper-client"))]
78+
#[cfg(feature = "isahc-client")]
7979
impl From<isahc::Error> for WebPushError {
8080
fn from(_: isahc::Error) -> Self {
8181
Self::Unspecified

0 commit comments

Comments
 (0)