From 6777dc1e5cd5eb2d69639f6d79942faaba3a282e Mon Sep 17 00:00:00 2001
From: David Pedersen <david.pdrsn@gmail.com>
Date: Mon, 23 Aug 2021 17:51:30 +0200
Subject: [PATCH] Add constructor to `Sse` (#244)

I think this is more consistent with the rest of the API
---
 examples/sse/src/main.rs |  4 ++--
 src/response/mod.rs      |  6 +-----
 src/response/sse.rs      | 38 +++++++++++++++++++-------------------
 3 files changed, 22 insertions(+), 26 deletions(-)

diff --git a/examples/sse/src/main.rs b/examples/sse/src/main.rs
index aa2fc7c4..f9045c8d 100644
--- a/examples/sse/src/main.rs
+++ b/examples/sse/src/main.rs
@@ -8,7 +8,7 @@ use axum::{
     extract::TypedHeader,
     handler::get,
     http::StatusCode,
-    response::sse::{sse, Event, Sse},
+    response::sse::{Event, Sse},
     Router,
 };
 use futures::stream::{self, Stream};
@@ -59,5 +59,5 @@ async fn sse_handler(
         .map(Ok)
         .throttle(Duration::from_secs(1));
 
-    sse(stream)
+    Sse::new(stream)
 }
diff --git a/src/response/mod.rs b/src/response/mod.rs
index fbed2fb8..90991ca6 100644
--- a/src/response/mod.rs
+++ b/src/response/mod.rs
@@ -21,11 +21,7 @@ pub mod sse;
 pub use crate::Json;
 
 #[doc(inline)]
-pub use self::{
-    headers::Headers,
-    redirect::Redirect,
-    sse::{sse, Sse},
-};
+pub use self::{headers::Headers, redirect::Redirect, sse::Sse};
 
 /// Trait for generating responses.
 ///
diff --git a/src/response/sse.rs b/src/response/sse.rs
index ab9436d8..c21496ee 100644
--- a/src/response/sse.rs
+++ b/src/response/sse.rs
@@ -4,10 +4,10 @@
 //!
 //! ```
 //! use axum::{
-//!     handler::get,
 //!     Router,
+//!     handler::get,
+//!     response::sse::{Event, KeepAlive, Sse},
 //! };
-//! use axum::response::sse::{sse, Event, KeepAlive, Sse};
 //! use std::{time::Duration, convert::Infallible};
 //! use tokio_stream::StreamExt as _ ;
 //! use futures::stream::{self, Stream};
@@ -20,7 +20,7 @@
 //!         .map(Ok)
 //!         .throttle(Duration::from_secs(1));
 //!
-//!     sse(stream).keep_alive(KeepAlive::default())
+//!     Sse::new(stream).keep_alive(KeepAlive::default())
 //! }
 //! # async {
 //! # hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
@@ -50,22 +50,7 @@ use std::{
 use sync_wrapper::SyncWrapper;
 use tokio::time::Sleep;
 
-/// Create a new [`Sse`] response that will respond with the given stream of
-/// [`Event`]s.
-///
-/// See the [module docs](self) for more details.
-pub fn sse<S>(stream: S) -> Sse<S>
-where
-    S: TryStream<Ok = Event> + Send + 'static,
-    S::Error: Into<BoxError>,
-{
-    Sse {
-        stream,
-        keep_alive: None,
-    }
-}
-
-/// An SSE response, created by [`sse`].
+/// An SSE response
 #[derive(Clone)]
 pub struct Sse<S> {
     stream: S,
@@ -73,6 +58,21 @@ pub struct Sse<S> {
 }
 
 impl<S> Sse<S> {
+    /// Create a new [`Sse`] response that will respond with the given stream of
+    /// [`Event`]s.
+    ///
+    /// See the [module docs](self) for more details.
+    pub fn new(stream: S) -> Self
+    where
+        S: TryStream<Ok = Event> + Send + 'static,
+        S::Error: Into<BoxError>,
+    {
+        Sse {
+            stream,
+            keep_alive: None,
+        }
+    }
+
     /// Configure the interval between keep-alive messages.
     ///
     /// Defaults to no keep-alive messages.