Skip to main content

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) {
32///     // `as _` imports the trait for method resolution only: under `std`
33///     // the inherent `connect_v4` below wins, under no-std the trait
34///     // method does. Either way the call shape is one line; only the
35///     // error type differs, so the result is discarded here instead of
36///     // being annotated.
37///     use optee_utee::net::StdCompatConnect as _;
38///
39///     let _ = TcpStream::connect_v4(host, port);
40/// }
41/// ```
42pub trait StdCompatConnect: Sized {
43    fn connect_v4(address: &str, port: u16) -> Result<Self, SocketError>;
44    fn connect_v6(address: &str, port: u16) -> Result<Self, SocketError>;
45    fn connect(address: &str, port: u16) -> Result<Self, SocketError> {
46        Self::connect_v4(address, port)
47    }
48}
49
50/// A trait used for convenience; import it so that the code remains consistent
51/// with the std version (with the only difference being the return error type).
52///
53/// Take TcpStream as example:
54/// ``` rust,no_run
55/// use optee_utee::net::{TcpStream, SocketError};
56///
57/// fn write_without_compact_trait(stream: &mut TcpStream, mut buf: &[u8]) -> Result<(), SocketError> {
58///     use optee_utee::ErrorKind;
59///     
60///     while !buf.is_empty() {
61///         match stream.send(buf) {
62///             Ok(0) => return Err(SocketError::Tee(ErrorKind::Generic)),
63///             Ok(n) => buf = &buf[n..],
64///             Err(e) => return Err(e),
65///         }
66///     }
67///     Ok(())
68/// }
69///
70/// fn write_with_compact_trait(stream: &mut TcpStream, buf: &[u8]) -> Result<(), SocketError> {
71///     use optee_utee::net::StdCompatWrite;
72///
73///     stream.write_all(buf)
74/// }
75/// ```
76pub trait StdCompatWrite {
77    fn write(&mut self, buf: &[u8]) -> Result<usize, SocketError>;
78    fn write_all(&mut self, mut buf: &[u8]) -> Result<(), SocketError> {
79        while !buf.is_empty() {
80            match self.write(buf)? {
81                0 => return Err(SocketError::Tee(crate::ErrorKind::Generic)),
82                n => buf = &buf[n..],
83            }
84        }
85        Ok(())
86    }
87}
88
89/// A trait used for convenience; import it so that the code remains consistent
90/// with the std version (with the only difference being the return error type).
91///
92/// Take TcpStream as example:
93/// ``` rust,no_run
94/// use optee_utee::net::{TcpStream, SocketError};
95///
96/// fn read_without_compact_trait(stream: &mut TcpStream, mut buf: &mut [u8]) -> Result<(), SocketError> {
97///     use optee_utee::ErrorKind;
98///
99///     while !buf.is_empty() {
100///         match stream.recv(buf) {
101///             Ok(0) => break,
102///             Ok(n) => buf = &mut buf[n..],
103///             Err(e) => return Err(e),
104///         }
105///     }
106///     if !buf.is_empty() {
107///         return Err(SocketError::Tee(ErrorKind::Generic));
108///     }
109///     Ok(())
110/// }
111///
112/// fn read_with_compact_trait(stream: &mut TcpStream, buf: &mut [u8]) -> Result<(), SocketError> {
113///     use optee_utee::net::StdCompatRead;
114///
115///     stream.read_exact(buf)
116/// }
117/// ```
118pub trait StdCompatRead {
119    fn read(&mut self, buf: &mut [u8]) -> Result<usize, SocketError>;
120    fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), SocketError> {
121        while !buf.is_empty() {
122            match self.read(buf)? {
123                0 => break,
124                n => buf = &mut buf[n..],
125            }
126        }
127        if !buf.is_empty() {
128            return Err(SocketError::Tee(crate::ErrorKind::Generic));
129        }
130        Ok(())
131    }
132}
133
134impl<T: SocketAdapter<Setup = Setup>> StdCompatConnect for Socket<T> {
135    fn connect_v4(address: &str, port: u16) -> Result<Self, SocketError> {
136        let setup = Setup::new_v4(address, port)?;
137        Self::open(setup)
138    }
139    fn connect_v6(address: &str, port: u16) -> Result<Self, SocketError> {
140        let setup = Setup::new_v6(address, port)?;
141        Self::open(setup)
142    }
143}
144
145impl<T: SocketAdapter<Setup = Setup>> StdCompatWrite for Socket<T> {
146    fn write(&mut self, buf: &[u8]) -> Result<usize, SocketError> {
147        self.send(buf)
148    }
149}
150
151impl<T: SocketAdapter<Setup = Setup>> StdCompatRead for Socket<T> {
152    fn read(&mut self, buf: &mut [u8]) -> Result<usize, SocketError> {
153        self.recv(buf)
154    }
155}