Skip to content

Commit cc9cffd

Browse files
committed
Update graceful shutdown example on type as well.
1 parent 5703338 commit cc9cffd

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

axum/src/json.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ where
208208
}
209209
}
210210

211+
impl<T> From<Json<T>> for Bytes
212+
where
213+
T: Serialize,
214+
{
215+
fn from(v: Json<T>) -> Bytes {
216+
let mut buf = BytesMut::with_capacity(128).writer();
217+
match serde_json::to_writer(&mut buf, &v.0) {
218+
Ok(()) => buf.into_inner().freeze(),
219+
Err(err) => todo!(""),
220+
}
221+
}
222+
}
223+
211224
#[cfg(test)]
212225
mod tests {
213226
use super::*;

axum/src/serve.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,22 @@ impl<M, S, F> WithGracefulShutdown<M, S, F> {
287287
/// # Example
288288
/// ```
289289
/// use axum::{Router, routing::get};
290+
/// use std::{future::IntoFuture, sync::Arc};
291+
/// use tokio::sync::oneshot;
290292
///
291293
/// # async {
292294
/// let router = Router::new().route("/", get(|| async { "Hello, World!" }));
293295
///
294296
/// let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
295-
/// axum::serve(listener, router)
296-
/// .with_graceful_shutdown(shutdown_signal())
297-
/// .tcp_nodelay(true)
298-
/// .await
299-
/// .unwrap();
297+
/// let (tx, rx) = oneshot::channel();
298+
/// tokio::spawn(
299+
/// axum::serve(listener, router)
300+
/// .with_graceful_shutdown(async move { rx.await.unwrap_or(()) })
301+
/// .into_future(),
302+
/// );
303+
/// // ...
304+
/// tx.send(()).unwrap();
300305
/// # };
301-
///
302-
/// async fn shutdown_signal() {
303-
/// // ...
304-
/// }
305306
/// ```
306307
pub fn tcp_nodelay(self, nodelay: bool) -> Self {
307308
Self {

0 commit comments

Comments
 (0)