optee_utee/net/
optee_no_std.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17use super::{Setup, Socket, SocketAdapter, SocketError};
18
19/// A trait used for convenience; import it so that the code remains consistent
20/// with the std version (with the only difference being the return error type).
21///
22/// Take TcpStream as example:
23/// ``` rust,no_run
24/// use optee_utee::net::{Setup, TcpStream, SocketError};
25///
26/// fn connect_without_compact_trait(host: &str, port: u16) -> Result<TcpStream, SocketError> {
27///     let setup = Setup::new_v4(host, port)?;
28///     TcpStream::open(setup)
29/// }
30///
31/// fn connect_with_compact_trait(host: &str, port: u16) -> Result<TcpStream, SocketError> {
32///     use optee_utee::net::StdCompatConnect;
33///
34///     TcpStream::connect_v4(host, port)
35/// }
36/// ```
37pub trait StdCompatConnect: Sized {
38    fn connect_v4(address: &str, port: u16) -> Result<Self, SocketError>;
39    fn connect_v6(address: &str, port: u16) -> Result<Self, SocketError>;
40    fn connect(address: &str, port: u16) -> Result<Self, SocketError> {
41        Self::connect_v4(address, port)
42    }
43}
44
45/// A trait used for convenience; import it so that the code remains consistent
46/// with the std version (with the only difference being the return error type).
47///
48/// Take TcpStream as example:
49/// ``` rust,no_run
50/// use optee_utee::net::{TcpStream, SocketError};
51///
52/// fn write_without_compact_trait(stream: &mut TcpStream, mut buf: &[u8]) -> Result<(), SocketError> {
53///     use optee_utee::ErrorKind;
54///     
55///     while !buf.is_empty() {
56///         match stream.send(buf) {
57///             Ok(0) => return Err(SocketError::Tee(ErrorKind::Generic)),
58///             Ok(n) => buf = &buf[n..],
59///             Err(e) => return Err(e),
60///         }
61///     }
62///     Ok(())
63/// }
64///
65/// fn write_with_compact_trait(stream: &mut TcpStream, buf: &[u8]) -> Result<(), SocketError> {
66///     use optee_utee::net::StdCompatWrite;
67///
68///     stream.write_all(buf)
69/// }
70/// ```
71pub trait StdCompatWrite {
72    fn write(&mut self, buf: &[u8]) -> Result<usize, SocketError>;
73    fn write_all(&mut self, mut buf: &[u8]) -> Result<(), SocketError> {
74        while !buf.is_empty() {
75            match self.write(buf)? {
76                0 => return Err(SocketError::Tee(crate::ErrorKind::Generic)),
77                n => buf = &buf[n..],
78            }
79        }
80        Ok(())
81    }
82}
83
84/// A trait used for convenience; import it so that the code remains consistent
85/// with the std version (with the only difference being the return error type).
86///
87/// Take TcpStream as example:
88/// ``` rust,no_run
89/// use optee_utee::net::{TcpStream, SocketError};
90///
91/// fn read_without_compact_trait(stream: &mut TcpStream, mut buf: &mut [u8]) -> Result<(), SocketError> {
92///     use optee_utee::ErrorKind;
93///
94///     while !buf.is_empty() {
95///         match stream.recv(buf) {
96///             Ok(0) => break,
97///             Ok(n) => buf = &mut buf[n..],
98///             Err(e) => return Err(e),
99///         }
100///     }
101///     if !buf.is_empty() {
102///         return Err(SocketError::Tee(ErrorKind::Generic));
103///     }
104///     Ok(())
105/// }
106///
107/// fn read_with_compact_trait(stream: &mut TcpStream, buf: &mut [u8]) -> Result<(), SocketError> {
108///     use optee_utee::net::StdCompatRead;
109///
110///     stream.read_exact(buf)
111/// }
112/// ```
113pub trait StdCompatRead {
114    fn read(&mut self, buf: &mut [u8]) -> Result<usize, SocketError>;
115    fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), SocketError> {
116        while !buf.is_empty() {
117            match self.read(buf)? {
118                0 => break,
119                n => buf = &mut buf[n..],
120            }
121        }
122        if !buf.is_empty() {
123            return Err(SocketError::Tee(crate::ErrorKind::Generic));
124        }
125        Ok(())
126    }
127}
128
129impl<T: SocketAdapter<Setup = Setup>> StdCompatConnect for Socket<T> {
130    fn connect_v4(address: &str, port: u16) -> Result<Self, SocketError> {
131        let setup = Setup::new_v4(address, port)?;
132        Self::open(setup)
133    }
134    fn connect_v6(address: &str, port: u16) -> Result<Self, SocketError> {
135        let setup = Setup::new_v6(address, port)?;
136        Self::open(setup)
137    }
138}
139
140impl<T: SocketAdapter<Setup = Setup>> StdCompatWrite for Socket<T> {
141    fn write(&mut self, buf: &[u8]) -> Result<usize, SocketError> {
142        self.send(buf)
143    }
144}
145
146impl<T: SocketAdapter<Setup = Setup>> StdCompatRead for Socket<T> {
147    fn read(&mut self, buf: &mut [u8]) -> Result<usize, SocketError> {
148        self.recv(buf)
149    }
150}