@@ -10,7 +10,7 @@ use log::trace;
1010use crate :: { hash_type_id, stream:: Stream , ArcMutex , Result , Sendable } ;
1111
1212use super :: { connector:: StreamConnector , input, listener:: SocketListener , StreamCollection } ;
13-
13+ /// A client for sending and receiving data.
1414pub struct Client {
1515 socket : ArcMutex < TcpStream > ,
1616 streams : ArcMutex < StreamCollection > ,
@@ -39,7 +39,7 @@ impl Client {
3939 self
4040 }
4141
42- pub fn new < T : ToSocketAddrs > ( addr : T ) -> Result < Client > {
42+ pub fn connect < T : ToSocketAddrs > ( addr : T ) -> Result < Client > {
4343 let stream = addr. to_socket_addrs ( ) ?;
4444 for addr in stream {
4545 match TcpStream :: connect ( addr) {
@@ -70,8 +70,8 @@ impl Client {
7070 socket. write_all ( & bytes) ?;
7171 Ok ( ( ) )
7272 }
73- /// Receives data from the socket.
74- /// This is blocking, and for now, manual .
73+
74+ /// Receives data from the socket. This does not return anything, but instead stores the data in the stream .
7575 pub fn recv ( & mut self ) -> Result < ( ) > {
7676 if self . listener . is_some ( ) {
7777 return Err ( io:: Error :: new (
@@ -101,7 +101,8 @@ impl Client {
101101 where
102102 T : Sendable + ' static ,
103103 {
104- let stream: Stream < T > = Stream :: new ( ) ;
104+ // SAFETY: This is safe because the stream is connected to a StreamConnector, which is guaranteed to be valid.
105+ let stream: Stream < T > = unsafe { Stream :: new ( ) } ;
105106 let info = StreamConnector :: new ( & stream) ;
106107 self . streams
107108 . lock ( )
@@ -248,7 +249,7 @@ mod tests {
248249
249250 #[ test]
250251 fn test_stream_data_struct ( ) {
251- let mut stream: Stream < TestStruct > = Stream :: new ( ) ;
252+ let mut stream: Stream < TestStruct > = unsafe { Stream :: new ( ) } ;
252253 let mut data = StreamConnector :: new ( & stream) ;
253254 unsafe {
254255 data. push_raw ( TestStruct { a : 30 , b : 40 } . send ( ) . into ( ) )
0 commit comments