Skip to content

Commit 15e715c

Browse files
authored
feat: Allow convert i32 to Code in const context (#2195)
1 parent 9e9ad8a commit 15e715c

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

tonic/src/status.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,28 @@ impl Code {
779779
/// Get the `Code` that represents the integer, if known.
780780
///
781781
/// If not known, returns `Code::Unknown` (surprise!).
782-
pub fn from_i32(i: i32) -> Code {
783-
Code::from(i)
782+
pub const fn from_i32(i: i32) -> Code {
783+
match i {
784+
0 => Code::Ok,
785+
1 => Code::Cancelled,
786+
2 => Code::Unknown,
787+
3 => Code::InvalidArgument,
788+
4 => Code::DeadlineExceeded,
789+
5 => Code::NotFound,
790+
6 => Code::AlreadyExists,
791+
7 => Code::PermissionDenied,
792+
8 => Code::ResourceExhausted,
793+
9 => Code::FailedPrecondition,
794+
10 => Code::Aborted,
795+
11 => Code::OutOfRange,
796+
12 => Code::Unimplemented,
797+
13 => Code::Internal,
798+
14 => Code::Unavailable,
799+
15 => Code::DataLoss,
800+
16 => Code::Unauthenticated,
801+
802+
_ => Code::Unknown,
803+
}
784804
}
785805

786806
/// Convert the string representation of a `Code` (as stored, for example, in the `grpc-status`
@@ -845,27 +865,7 @@ impl Code {
845865

846866
impl From<i32> for Code {
847867
fn from(i: i32) -> Self {
848-
match i {
849-
0 => Code::Ok,
850-
1 => Code::Cancelled,
851-
2 => Code::Unknown,
852-
3 => Code::InvalidArgument,
853-
4 => Code::DeadlineExceeded,
854-
5 => Code::NotFound,
855-
6 => Code::AlreadyExists,
856-
7 => Code::PermissionDenied,
857-
8 => Code::ResourceExhausted,
858-
9 => Code::FailedPrecondition,
859-
10 => Code::Aborted,
860-
11 => Code::OutOfRange,
861-
12 => Code::Unimplemented,
862-
13 => Code::Internal,
863-
14 => Code::Unavailable,
864-
15 => Code::DataLoss,
865-
16 => Code::Unauthenticated,
866-
867-
_ => Code::Unknown,
868-
}
868+
Code::from_i32(i)
869869
}
870870
}
871871

0 commit comments

Comments
 (0)