@@ -10,7 +10,7 @@ use log::trace;
10
10
use crate :: { hash_type_id, stream:: Stream , ArcMutex , Result , Sendable } ;
11
11
12
12
use super :: { connector:: StreamConnector , input, listener:: SocketListener , StreamCollection } ;
13
-
13
+ /// A client for sending and receiving data.
14
14
pub struct Client {
15
15
socket : ArcMutex < TcpStream > ,
16
16
streams : ArcMutex < StreamCollection > ,
@@ -39,7 +39,7 @@ impl Client {
39
39
self
40
40
}
41
41
42
- pub fn new < T : ToSocketAddrs > ( addr : T ) -> Result < Client > {
42
+ pub fn connect < T : ToSocketAddrs > ( addr : T ) -> Result < Client > {
43
43
let stream = addr. to_socket_addrs ( ) ?;
44
44
for addr in stream {
45
45
match TcpStream :: connect ( addr) {
@@ -70,8 +70,8 @@ impl Client {
70
70
socket. write_all ( & bytes) ?;
71
71
Ok ( ( ) )
72
72
}
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 .
75
75
pub fn recv ( & mut self ) -> Result < ( ) > {
76
76
if self . listener . is_some ( ) {
77
77
return Err ( io:: Error :: new (
@@ -101,7 +101,8 @@ impl Client {
101
101
where
102
102
T : Sendable + ' static ,
103
103
{
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 ( ) } ;
105
106
let info = StreamConnector :: new ( & stream) ;
106
107
self . streams
107
108
. lock ( )
@@ -248,7 +249,7 @@ mod tests {
248
249
249
250
#[ test]
250
251
fn test_stream_data_struct ( ) {
251
- let mut stream: Stream < TestStruct > = Stream :: new ( ) ;
252
+ let mut stream: Stream < TestStruct > = unsafe { Stream :: new ( ) } ;
252
253
let mut data = StreamConnector :: new ( & stream) ;
253
254
unsafe {
254
255
data. push_raw ( TestStruct { a : 30 , b : 40 } . send ( ) . into ( ) )
0 commit comments