Skip to content

Commit ee3d0df

Browse files
authoredSep 12, 2022
feat(tonic): impl Clone for Status using Arc (#1076)
1 parent 5723681 commit ee3d0df

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed
 

‎tonic/src/status.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::metadata::MetadataMap;
33
use bytes::Bytes;
44
use http::header::{HeaderMap, HeaderValue};
55
use percent_encoding::{percent_decode, percent_encode, AsciiSet, CONTROLS};
6-
use std::{borrow::Cow, error::Error, fmt};
6+
use std::{borrow::Cow, error::Error, fmt, sync::Arc};
77
use tracing::{debug, trace, warn};
88

99
const ENCODING_SET: &AsciiSet = &CONTROLS
@@ -33,6 +33,7 @@ const GRPC_STATUS_DETAILS_HEADER: &str = "grpc-status-details-bin";
3333
/// assert_eq!(status1.code(), Code::InvalidArgument);
3434
/// assert_eq!(status1.code(), status2.code());
3535
/// ```
36+
#[derive(Clone)]
3637
pub struct Status {
3738
/// The gRPC status code, found in the `grpc-status` header.
3839
code: Code,
@@ -45,7 +46,7 @@ pub struct Status {
4546
/// or by `Status` fields above, they will be ignored.
4647
metadata: MetadataMap,
4748
/// Optional underlying error.
48-
source: Option<Box<dyn Error + Send + Sync + 'static>>,
49+
source: Option<Arc<dyn Error + Send + Sync + 'static>>,
4950
}
5051

5152
/// gRPC status codes used by [`Status`].
@@ -318,7 +319,7 @@ impl Status {
318319
pub fn from_error(err: Box<dyn Error + Send + Sync + 'static>) -> Status {
319320
Status::try_from_error(err).unwrap_or_else(|err| {
320321
let mut status = Status::new(Code::Unknown, err.to_string());
321-
status.source = Some(err);
322+
status.source = Some(err.into());
322323
status
323324
})
324325
}
@@ -342,7 +343,7 @@ impl Status {
342343
};
343344

344345
if let Some(mut status) = find_status_in_source_chain(&*err) {
345-
status.source = Some(err);
346+
status.source = Some(err.into());
346347
return Ok(status);
347348
}
348349

@@ -370,7 +371,7 @@ impl Status {
370371
};
371372

372373
let mut status = Self::new(code, format!("h2 protocol error: {}", err));
373-
status.source = Some(err);
374+
status.source = Some(Arc::new(*err));
374375
status
375376
}
376377

0 commit comments

Comments
 (0)
Please sign in to comment.