Skip to content

Commit 6b91c82

Browse files
Factor PanicMessage out of rpc code in proc_macro
1 parent a81b2e9 commit 6b91c82

3 files changed

Lines changed: 74 additions & 70 deletions

File tree

library/proc_macro/src/bridge/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ mod handle;
9999
#[macro_use]
100100
#[forbid(unsafe_code)]
101101
mod rpc;
102+
#[forbid(unsafe_code)]
103+
mod panic_message;
102104
#[allow(unsafe_code)]
103105
mod selfless_reify;
104106
#[forbid(unsafe_code)]
@@ -107,7 +109,7 @@ pub mod server;
107109
mod symbol;
108110

109111
use buffer::Buffer;
110-
pub use rpc::PanicMessage;
112+
pub use panic_message::PanicMessage;
111113
use rpc::{Decode, Encode};
112114

113115
/// Configuration for establishing an active connection between a server and a
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::any::Any;
2+
3+
use crate::bridge::{Buffer, Decode, Encode};
4+
5+
/// Simplified version of panic payloads, ignoring
6+
/// types other than `&'static str` and `String`.
7+
pub enum PanicMessage {
8+
StaticStr(&'static str),
9+
String(String),
10+
Unknown,
11+
}
12+
13+
impl From<Box<dyn Any + Send>> for PanicMessage {
14+
fn from(payload: Box<dyn Any + Send + 'static>) -> Self {
15+
if let Some(s) = payload.downcast_ref::<&'static str>() {
16+
return PanicMessage::StaticStr(s);
17+
}
18+
if let Ok(s) = payload.downcast::<String>() {
19+
return PanicMessage::String(*s);
20+
}
21+
PanicMessage::Unknown
22+
}
23+
}
24+
25+
impl From<PanicMessage> for Box<dyn Any + Send> {
26+
fn from(val: PanicMessage) -> Self {
27+
match val {
28+
PanicMessage::StaticStr(s) => Box::new(s),
29+
PanicMessage::String(s) => Box::new(s),
30+
PanicMessage::Unknown => {
31+
struct UnknownPanicMessage;
32+
Box::new(UnknownPanicMessage)
33+
}
34+
}
35+
}
36+
}
37+
38+
impl PanicMessage {
39+
pub fn as_str(&self) -> Option<&str> {
40+
match self {
41+
PanicMessage::StaticStr(s) => Some(s),
42+
PanicMessage::String(s) => Some(s),
43+
PanicMessage::Unknown => None,
44+
}
45+
}
46+
47+
pub fn into_string(self) -> Option<String> {
48+
match self {
49+
PanicMessage::StaticStr(s) => Some(s.into()),
50+
PanicMessage::String(s) => Some(s),
51+
PanicMessage::Unknown => None,
52+
}
53+
}
54+
}
55+
56+
impl<S> Encode<S> for PanicMessage {
57+
#[inline]
58+
fn encode(self, w: &mut Buffer, s: &mut S) {
59+
self.as_str().encode(w, s);
60+
}
61+
}
62+
63+
impl<S> Decode<'_, '_, S> for PanicMessage {
64+
#[inline]
65+
fn decode(r: &mut &[u8], s: &mut S) -> Self {
66+
match Option::<String>::decode(r, s) {
67+
Some(s) => PanicMessage::String(s),
68+
None => PanicMessage::Unknown,
69+
}
70+
}
71+
}

library/proc_macro/src/bridge/rpc.rs

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Serialization for client-server communication.
22
3-
use std::any::Any;
43
use std::io::Write;
54
use std::num::NonZero;
65

@@ -259,71 +258,3 @@ impl<'a, S, T: for<'s> Decode<'a, 's, S>> Decode<'a, '_, S> for Vec<T> {
259258
vec
260259
}
261260
}
262-
263-
/// Simplified version of panic payloads, ignoring
264-
/// types other than `&'static str` and `String`.
265-
pub enum PanicMessage {
266-
StaticStr(&'static str),
267-
String(String),
268-
Unknown,
269-
}
270-
271-
impl From<Box<dyn Any + Send>> for PanicMessage {
272-
fn from(payload: Box<dyn Any + Send + 'static>) -> Self {
273-
if let Some(s) = payload.downcast_ref::<&'static str>() {
274-
return PanicMessage::StaticStr(s);
275-
}
276-
if let Ok(s) = payload.downcast::<String>() {
277-
return PanicMessage::String(*s);
278-
}
279-
PanicMessage::Unknown
280-
}
281-
}
282-
283-
impl From<PanicMessage> for Box<dyn Any + Send> {
284-
fn from(val: PanicMessage) -> Self {
285-
match val {
286-
PanicMessage::StaticStr(s) => Box::new(s),
287-
PanicMessage::String(s) => Box::new(s),
288-
PanicMessage::Unknown => {
289-
struct UnknownPanicMessage;
290-
Box::new(UnknownPanicMessage)
291-
}
292-
}
293-
}
294-
}
295-
296-
impl PanicMessage {
297-
pub fn as_str(&self) -> Option<&str> {
298-
match self {
299-
PanicMessage::StaticStr(s) => Some(s),
300-
PanicMessage::String(s) => Some(s),
301-
PanicMessage::Unknown => None,
302-
}
303-
}
304-
305-
pub fn into_string(self) -> Option<String> {
306-
match self {
307-
PanicMessage::StaticStr(s) => Some(s.into()),
308-
PanicMessage::String(s) => Some(s),
309-
PanicMessage::Unknown => None,
310-
}
311-
}
312-
}
313-
314-
impl<S> Encode<S> for PanicMessage {
315-
#[inline]
316-
fn encode(self, w: &mut Buffer, s: &mut S) {
317-
self.as_str().encode(w, s);
318-
}
319-
}
320-
321-
impl<S> Decode<'_, '_, S> for PanicMessage {
322-
#[inline]
323-
fn decode(r: &mut &[u8], s: &mut S) -> Self {
324-
match Option::<String>::decode(r, s) {
325-
Some(s) => PanicMessage::String(s),
326-
None => PanicMessage::Unknown,
327-
}
328-
}
329-
}

0 commit comments

Comments
 (0)