1+ //! Interface for an encrypted channel
12use std:: {
23 collections:: VecDeque ,
34 fmt:: Debug ,
@@ -198,15 +199,18 @@ impl SansIoMachine {
198199 Ok ( made_progress. then_some ( ( ) ) )
199200 }
200201
202+ #[ allow( unused, reason = "vectorized version of 'get_next_sendable_message'" ) ]
201203 fn get_sendable_messages ( & mut self ) -> Result < Vec < Vec < u8 > > , IoError > {
202204 self . poll_all_enc_dec ( ) ?;
203205 Ok ( self . encrypted_tx . drain ( ..) . collect ( ) )
204206 }
207+
205208 fn get_next_sendable_message ( & mut self ) -> Result < Option < Vec < u8 > > , IoError > {
206209 self . poll_all_enc_dec ( ) ?;
207210 Ok ( self . encrypted_tx . pop_front ( ) )
208211 }
209212
213+ #[ allow( unused, reason = "vectorized version of 'receive_next'" ) ]
210214 fn receive_next_messages ( & mut self , encrypted_messages : Vec < Vec < u8 > > ) {
211215 self . encrypted_rx
212216 . extend ( encrypted_messages. into_iter ( ) . map ( Ok ) ) ;
@@ -216,6 +220,7 @@ impl SansIoMachine {
216220 self . encrypted_rx . push_back ( Ok ( encrypted_msg) ) ;
217221 }
218222
223+ #[ allow( unused, reason = "add a new plaintext message to send" ) ]
219224 fn queue_msg ( & mut self , msg : Vec < u8 > ) {
220225 self . plain_tx . push_back ( msg) ;
221226 }
@@ -243,12 +248,17 @@ impl Debug for SansIoMachine {
243248}
244249
245250#[ derive( Debug ) ]
251+ /// Encryption event
246252pub enum Event {
253+ /// Data passed through the handshake payload
247254 HandshakePayload ( Vec < u8 > ) ,
255+ /// Decrypted message
248256 Message ( Vec < u8 > ) ,
257+ /// Error occured in encryption
249258 ErrStuff ( IoError ) ,
250259}
251260
261+ /// Supertrait for duplex channel required by [`Machine`]
252262pub trait MachineIo :
253263 Stream < Item = Result < Vec < u8 > , IoError > > + Sink < Vec < u8 > > + Send + Unpin + ' static
254264{
@@ -277,6 +287,12 @@ impl Debug for Machine {
277287}
278288
279289impl Machine {
290+ /// Create a new [`Machine`]
291+ fn new ( io : Option < Box < dyn MachineIo < Error = std:: io:: Error > > > , inner : SansIoMachine ) -> Self {
292+ Self { io, inner }
293+ }
294+
295+ /// Create a new initiator
280296 pub fn new_dht_init (
281297 io : Option < Box < dyn MachineIo < Error = std:: io:: Error > > > ,
282298 remote_pub_key : & [ u8 ; PUBLIC_KEYLEN ] ,
@@ -285,44 +301,38 @@ impl Machine {
285301 let ss = SecStream :: new_initiator ( remote_pub_key, prologue) ?;
286302 let state = State :: InitiatorStart ( ss) ;
287303 let inner = SansIoMachine :: new ( state) ;
288- Ok ( Self { io, inner } )
304+ Ok ( Self :: new ( io, inner) )
305+ }
306+
307+ /// Create a new initiator
308+ pub fn new_init (
309+ io : Box < dyn MachineIo < Error = std:: io:: Error > > ,
310+ state : SecStream < Initiator < Start > > ,
311+ ) -> Self {
312+ Self :: new ( Some ( io) , SansIoMachine :: new_init ( state) )
289313 }
290314
291- fn new_dht_resp (
315+ /// Create a new responder from a private key
316+ pub fn resp_from_private (
292317 io : Option < Box < dyn MachineIo < Error = std:: io:: Error > > > ,
293318 private : & [ u8 ] ,
294319 ) -> Result < Self , Error > {
295320 let ss = SecStream :: new_responder ( private) ?;
296321 let state = State :: RespStart ( ss) ;
297322 let inner = SansIoMachine :: new ( state) ;
298- Ok ( Self { io, inner } )
323+ Ok ( Self :: new ( io, inner) )
299324 }
300325
301- fn new ( io : Option < Box < dyn MachineIo < Error = std:: io:: Error > > > , inner : SansIoMachine ) -> Self {
302- Self { io, inner }
303- }
304-
305- fn new_init (
306- io : Box < dyn MachineIo < Error = std:: io:: Error > > ,
307- state : SecStream < Initiator < Start > > ,
308- ) -> Self {
309- Self {
310- io : Some ( io) ,
311- inner : SansIoMachine :: new_init ( state) ,
312- }
313- }
314-
315- fn new_resp (
326+ /// Create a new responder
327+ pub fn new_resp (
316328 io : Box < dyn MachineIo < Error = std:: io:: Error > > ,
317329 state : SecStream < Responder < Start > > ,
318330 ) -> Self {
319- Self {
320- io : Some ( io) ,
321- inner : SansIoMachine :: new_resp ( state) ,
322- }
331+ Self :: new ( Some ( io) , SansIoMachine :: new_resp ( state) )
323332 }
324333
325- async fn complete_handshake ( & mut self ) -> Result < ( ) , IoError > {
334+ /// Wait for handshake to complete
335+ pub async fn complete_handshake ( & mut self ) -> Result < ( ) , IoError > {
326336 use futures:: SinkExt ;
327337 use futures:: StreamExt ;
328338
0 commit comments