@@ -122,8 +122,12 @@ impl ExpnGlobals {
122
122
}
123
123
124
124
pub trait Message : Serialize + DeserializeOwned {
125
- fn read ( inp : & mut impl BufRead , buf : & mut String ) -> io:: Result < Option < Self > > {
126
- Ok ( match read_json ( inp, buf) ? {
125
+ fn read < R : BufRead > (
126
+ from_proto : ProtocolRead < R > ,
127
+ inp : & mut R ,
128
+ buf : & mut String ,
129
+ ) -> io:: Result < Option < Self > > {
130
+ Ok ( match from_proto ( inp, buf) ? {
127
131
None => None ,
128
132
Some ( text) => {
129
133
let mut deserializer = serde_json:: Deserializer :: from_str ( text) ;
@@ -134,44 +138,20 @@ pub trait Message: Serialize + DeserializeOwned {
134
138
}
135
139
} )
136
140
}
137
- fn write ( self , out : & mut impl Write ) -> io:: Result < ( ) > {
141
+ fn write < W : Write > ( self , to_proto : ProtocolWrite < W > , out : & mut W ) -> io:: Result < ( ) > {
138
142
let text = serde_json:: to_string ( & self ) ?;
139
- write_json ( out, & text)
143
+ to_proto ( out, & text)
140
144
}
141
145
}
142
146
143
147
impl Message for Request { }
144
148
impl Message for Response { }
145
149
146
- fn read_json < ' a > ( inp : & mut impl BufRead , buf : & ' a mut String ) -> io:: Result < Option < & ' a String > > {
147
- loop {
148
- buf. clear ( ) ;
149
-
150
- inp. read_line ( buf) ?;
151
- buf. pop ( ) ; // Remove trailing '\n'
152
-
153
- if buf. is_empty ( ) {
154
- return Ok ( None ) ;
155
- }
156
-
157
- // Some ill behaved macro try to use stdout for debugging
158
- // We ignore it here
159
- if !buf. starts_with ( '{' ) {
160
- tracing:: error!( "proc-macro tried to print : {}" , buf) ;
161
- continue ;
162
- }
163
-
164
- return Ok ( Some ( buf) ) ;
165
- }
166
- }
167
-
168
- fn write_json ( out : & mut impl Write , msg : & str ) -> io:: Result < ( ) > {
169
- tracing:: debug!( "> {}" , msg) ;
170
- out. write_all ( msg. as_bytes ( ) ) ?;
171
- out. write_all ( b"\n " ) ?;
172
- out. flush ( ) ?;
173
- Ok ( ( ) )
174
- }
150
+ #[ allow( type_alias_bounds) ]
151
+ type ProtocolRead < R : BufRead > =
152
+ for <' i , ' buf > fn ( inp : & ' i mut R , buf : & ' buf mut String ) -> io:: Result < Option < & ' buf String > > ;
153
+ #[ allow( type_alias_bounds) ]
154
+ type ProtocolWrite < W : Write > = for <' o , ' msg > fn ( out : & ' o mut W , msg : & ' msg str ) -> io:: Result < ( ) > ;
175
155
176
156
#[ cfg( test) ]
177
157
mod tests {
0 commit comments