77// option. This file may not be copied, modified, or distributed
88// except according to those terms.
99
10+ use crate :: error:: SerializationError ;
1011use crate :: platform:: { self , OsIpcChannel , OsIpcReceiver , OsIpcReceiverSet , OsIpcSender } ;
1112use crate :: platform:: {
1213 OsIpcOneShotServer , OsIpcSelectionResult , OsIpcSharedMemory , OsOpaqueIpcChannel ,
1314} ;
15+ use crate :: { IpcError , TryRecvError } ;
1416
1517use bincode;
1618use serde:: { de:: Error , Deserialize , Deserializer , Serialize , Serializer } ;
1719use std:: cell:: RefCell ;
1820use std:: cmp:: min;
19- use std:: error:: Error as StdError ;
2021use std:: fmt:: { self , Debug , Formatter } ;
2122use std:: io;
2223use std:: marker:: PhantomData ;
@@ -37,57 +38,6 @@ thread_local! {
3738 const { RefCell :: new( Vec :: new( ) ) }
3839}
3940
40- #[ derive( Debug ) ]
41- pub enum IpcError {
42- Bincode ( bincode:: Error ) ,
43- Io ( io:: Error ) ,
44- Disconnected ,
45- }
46-
47- impl fmt:: Display for IpcError {
48- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
49- match * self {
50- IpcError :: Bincode ( ref err) => write ! ( fmt, "bincode error: {err}" ) ,
51- IpcError :: Io ( ref err) => write ! ( fmt, "io error: {err}" ) ,
52- IpcError :: Disconnected => write ! ( fmt, "disconnected" ) ,
53- }
54- }
55- }
56-
57- impl StdError for IpcError {
58- fn source ( & self ) -> Option < & ( dyn StdError + ' static ) > {
59- match * self {
60- IpcError :: Bincode ( ref err) => Some ( err) ,
61- IpcError :: Io ( ref err) => Some ( err) ,
62- IpcError :: Disconnected => None ,
63- }
64- }
65- }
66-
67- #[ derive( Debug ) ]
68- pub enum TryRecvError {
69- IpcError ( IpcError ) ,
70- Empty ,
71- }
72-
73- impl fmt:: Display for TryRecvError {
74- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
75- match * self {
76- TryRecvError :: IpcError ( ref err) => write ! ( fmt, "ipc error: {err}" ) ,
77- TryRecvError :: Empty => write ! ( fmt, "empty" ) ,
78- }
79- }
80- }
81-
82- impl StdError for TryRecvError {
83- fn source ( & self ) -> Option < & ( dyn StdError + ' static ) > {
84- match * self {
85- TryRecvError :: IpcError ( ref err) => Some ( err) ,
86- TryRecvError :: Empty => None ,
87- }
88- }
89- }
90-
9141/// Create a connected [IpcSender] and [IpcReceiver] that
9242/// transfer messages of a given type provided by type `T`
9343/// or inferred by the types of messages sent by the sender.
@@ -248,15 +198,18 @@ where
248198{
249199 /// Blocking receive.
250200 pub fn recv ( & self ) -> Result < T , IpcError > {
251- self . os_receiver . recv ( ) ?. to ( ) . map_err ( IpcError :: Bincode )
201+ self . os_receiver
202+ . recv ( ) ?
203+ . to ( )
204+ . map_err ( IpcError :: SerializationError )
252205 }
253206
254207 /// Non-blocking receive
255208 pub fn try_recv ( & self ) -> Result < T , TryRecvError > {
256209 self . os_receiver
257210 . try_recv ( ) ?
258211 . to ( )
259- . map_err ( IpcError :: Bincode )
212+ . map_err ( IpcError :: SerializationError )
260213 . map_err ( TryRecvError :: IpcError )
261214 }
262215
@@ -270,7 +223,7 @@ where
270223 self . os_receiver
271224 . try_recv_timeout ( duration) ?
272225 . to ( )
273- . map_err ( IpcError :: Bincode )
226+ . map_err ( IpcError :: SerializationError )
274227 . map_err ( TryRecvError :: IpcError )
275228 }
276229
@@ -364,7 +317,7 @@ where
364317 }
365318
366319 /// Send data across the channel to the receiver.
367- pub fn send ( & self , data : T ) -> Result < ( ) , bincode :: Error > {
320+ pub fn send ( & self , data : T ) -> Result < ( ) , IpcError > {
368321 let mut bytes = Vec :: with_capacity ( 4096 ) ;
369322 OS_IPC_CHANNELS_FOR_SERIALIZATION . with ( |os_ipc_channels_for_serialization| {
370323 OS_IPC_SHARED_MEMORY_REGIONS_FOR_SERIALIZATION . with (
@@ -377,7 +330,7 @@ where
377330 let os_ipc_shared_memory_regions;
378331 let os_ipc_channels;
379332 {
380- bincode:: serialize_into ( & mut bytes, & data) ?;
333+ bincode:: serialize_into ( & mut bytes, & data) . map_err ( SerializationError ) ?;
381334 os_ipc_channels = mem:: replace (
382335 & mut * os_ipc_channels_for_serialization. borrow_mut ( ) ,
383336 old_os_ipc_channels,
@@ -726,7 +679,7 @@ impl IpcMessage {
726679 }
727680
728681 /// Deserialize the raw data in the contained message into the inferred type.
729- pub fn to < T > ( mut self ) -> Result < T , bincode :: Error >
682+ pub fn to < T > ( mut self ) -> Result < T , SerializationError >
730683 where
731684 T : for < ' de > Deserialize < ' de > + Serialize ,
732685 {
@@ -744,7 +697,7 @@ impl IpcMessage {
744697 . map ( Some )
745698 . collect ( ) ,
746699 ) ;
747- let result = bincode:: deserialize ( & self . data [ ..] ) ;
700+ let result = bincode:: deserialize ( & self . data [ ..] ) . map_err ( |e| e . into ( ) ) ;
748701 * os_ipc_shared_memory_regions_for_deserialization. borrow_mut ( ) =
749702 old_ipc_shared_memory_regions_for_deserialization;
750703 mem:: swap (
@@ -886,7 +839,7 @@ where
886839 ) )
887840 }
888841
889- pub fn accept ( self ) -> Result < ( IpcReceiver < T > , T ) , bincode :: Error > {
842+ pub fn accept ( self ) -> Result < ( IpcReceiver < T > , T ) , IpcError > {
890843 let ( os_receiver, ipc_message) = self . os_server . accept ( ) ?;
891844 Ok ( (
892845 IpcReceiver {
0 commit comments