mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-29 22:10:59 +00:00
Implement Clone for ErasedJson (#2142)
This commit is contained in:
parent
2138489ce5
commit
8af38763a5
@ -7,9 +7,11 @@ and this project adheres to [Semantic Versioning].
|
|||||||
|
|
||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
- **added:** Added `TypedHeader` which used to be in `axum` ([#1850])
|
- **added:** `TypedHeader` which used to be in `axum` ([#1850])
|
||||||
|
- **added:** `Clone` implementation for `ErasedJson` ([#2142])
|
||||||
|
|
||||||
[#1850]: https://github.com/tokio-rs/axum/pull/1850
|
[#1850]: https://github.com/tokio-rs/axum/pull/1850
|
||||||
|
[#2142]: https://github.com/tokio-rs/axum/pull/2142
|
||||||
|
|
||||||
# 0.7.4 (18. April, 2023)
|
# 0.7.4 (18. April, 2023)
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
http::{header, HeaderValue, StatusCode},
|
http::{header, HeaderValue, StatusCode},
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
@ -29,21 +31,29 @@ use serde::Serialize;
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "erased-json")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "erased-json")))]
|
||||||
#[derive(Debug)]
|
#[derive(Clone, Debug)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub struct ErasedJson(serde_json::Result<Bytes>);
|
pub struct ErasedJson(Result<Bytes, Arc<serde_json::Error>>);
|
||||||
|
|
||||||
impl ErasedJson {
|
impl ErasedJson {
|
||||||
/// Create an `ErasedJson` by serializing a value with the compact formatter.
|
/// Create an `ErasedJson` by serializing a value with the compact formatter.
|
||||||
pub fn new<T: Serialize>(val: T) -> Self {
|
pub fn new<T: Serialize>(val: T) -> Self {
|
||||||
let mut bytes = BytesMut::with_capacity(128);
|
let mut bytes = BytesMut::with_capacity(128);
|
||||||
Self(serde_json::to_writer((&mut bytes).writer(), &val).map(|_| bytes.freeze()))
|
let result = match serde_json::to_writer((&mut bytes).writer(), &val) {
|
||||||
|
Ok(()) => Ok(bytes.freeze()),
|
||||||
|
Err(e) => Err(Arc::new(e)),
|
||||||
|
};
|
||||||
|
Self(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an `ErasedJson` by serializing a value with the pretty formatter.
|
/// Create an `ErasedJson` by serializing a value with the pretty formatter.
|
||||||
pub fn pretty<T: Serialize>(val: T) -> Self {
|
pub fn pretty<T: Serialize>(val: T) -> Self {
|
||||||
let mut bytes = BytesMut::with_capacity(128);
|
let mut bytes = BytesMut::with_capacity(128);
|
||||||
Self(serde_json::to_writer_pretty((&mut bytes).writer(), &val).map(|_| bytes.freeze()))
|
let result = match serde_json::to_writer_pretty((&mut bytes).writer(), &val) {
|
||||||
|
Ok(()) => Ok(bytes.freeze()),
|
||||||
|
Err(e) => Err(Arc::new(e)),
|
||||||
|
};
|
||||||
|
Self(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user