Comments (6)
Current working draft depends on an unstable nightly feature (generic associated types). We should try to come up with an alternate solution that doesn't require this.
pub struct Udp;
pub struct Closed;
pub struct Connected;
pub struct Bound;
pub trait Mode {}
impl Mode for Udp {}
pub trait Status {}
impl Status for Closed {}
impl Status for Connected {}
impl Status for Bound {}
pub trait OpenStatus: Status {}
impl OpenStatus for Connected {}
impl OpenStatus for Bound {}
pub trait UdpStack {]
type UdpSocket<M: Mode, S: Status>;]
type Error: core::fmt::Debug;
]
fn socket(&self) -> Result<Self::UdpSocket<Udp, Closed>, Self::Error> where <Self as UdpStack>::UdpSocket<Udp, Closed>: core::marker::Sized;
fn connect(&self, socket: Self::UdpSocket<Udp, Closed>, remote: SocketAddr) -> Result<Self::UdpSocket<Udp, Connected>, Self::Error> where <Self as UdpStack>::UdpSocket<Udp, Connected>: core::marker::Sized;
fn bind(&self, socket: Self::UdpSocket<Udp, Closed>, local_port: u16) -> Result<Self::UdpSocket<Udp, Bound>, Self::Error> where <Self as UdpStack>::UdpSocket<Udp, Bound>: core::marker::Sized;
fn send(&self, socket: &mut Self::UdpSocket<Udp, Connected>, buffer: &[u8]) -> nb::Result<(), Self::Error>;
fn send_to(
&self,
socket: &mut Self::UdpSocket<Udp, impl OpenStatus>,
remote: SocketAddr,
buffer: &[u8],
) -> nb::Result<(), Self::Error>;
fn receive(
&self,
socket: &mut Self::UdpSocket<Udp, impl OpenStatus>,
buffer: &mut [u8],
) -> nb::Result<(usize, SocketAddr), Self::Error>;
fn close<S: Status>(&self, socket: Self::UdpSocket<Udp, S>) -> Result<(), Self::Error>;
}
from embedded-nal.
A solution that would omit the need for GAT would be to publish our own Socket struct with the needed generics. Something like this:
pub struct Socket<Mode, Status, Extra> {
// Pretty sure PhantomData is necessary here, but maybe type parameters could be underscore prefixed instead
mode: std::marker::PhantomData<Mode>;
status: std::marker::PhantomData<Status>;
pub extra: Extra;
}
The user would then provide an interior Extra
type, instead of the Socket
type itself. The extra would likely be necessary for encapsulating the socket's identity, and possibly other arbitrary data needed by the driver. We might also be able to do some trait implementation trickery to make it easier to work with... perhaps Deref it into its Extra type?
from embedded-nal.
Fallibility is often a difficult aspect of type-state interfaces since they consume resources.
For example, with the interface above if I want to bind a socket to one of the free ports within a range I need to open a new socket for each attempt:
let stack = StackImpl{};
let mut port = 8080;
let bound_socket_res = loop {
let socket = stack.socket().unwrap(); // I need to open a new socket each time
match stack.bind(socket, 8081) {
Ok(s) => break Ok(s),
Err(e) => { // socket was consumed
if port > 8085 {
break Err(e);
}
port += 1; // retry with next port
}
}
};
One could say an implementation could return the socket in the error type but then you need recursion to implement something like that:
fn bind<S: UdpStack<Error = UdpSocket>>(
stack: &S,
socket: UdpSocket,
port: u16,
port_max: u16,
) -> Result<UdpSocket, UdpSocket> {
match stack.bind(socket, port) {
Ok(s) => Ok(s),
Err(s) => {
if port > port_max {
Err(s)
} else {
bind(stack, s, port + 1, port_max)
}
}
}
}
let bound_socket_res = bind(&stack, socket, 8080, 8085);
Recursion is ok in this case but might not be as easy to do in other cases.
Have you thought about this?
from embedded-nal.
Maybe a socket trait is enough to get around GAT. I think this would be better (if it works) than forcing a specific socket struct.
from embedded-nal.
@Sympatron I tried to use a trait, but it did not have the same effect. I couldn't get around needing to raise the generics up to the top-level of the Stack trait, which defeated the whole purpose. If you can figure out a way to apply it, I'd love to see a solution. I was unable to.
from embedded-nal.
@eldruin That's an interesting problem. Maybe it's because I haven't done much network programming, but that doesn't seem like a super common use-case. However, I did manage to create a solution that doesn't require recursion, just a loop (which is exactly what I'd expect to accomplish this task).
It's ugly; I stopped working on it as soon as the compiler was happy 😃
fn bind_range<S: UdpStack<Error = UdpSocket>>(
stack: &S,
socket: UdpSocket,
port: u16,
port_max: u16,
) -> Result<UdpSocket, UdpSocket> {
let mut p = port;
let mut loop_socket = socket;
loop {
match stack.bind(loop_socket, p) {
Ok(s) => return Ok(s),
Err(s) => {
loop_socket = s;
if p == port_max {
return Err(loop_socket);
}
}
}
p += 1;
}
}
To solve this in general though, I'd be in favor of changing the signature of socket-consuming fallible functions to return a tuple of (Socket, Self::Error)
.
fn connect(&self, socket: Self::UdpSocket<Udp, Closed>, remote: SocketAddr) -> Result<Self::UdpSocket<Udp, Connected>, (Self::UdpSocket<Udp, Closed>, Self::Error)> where <Self as UdpStack>::UdpSocket<Udp, Connected>: core::marker::Sized;
from embedded-nal.
Related Issues (20)
- Guidance on TcpConnect and self-referential structs
- e-nal-async: Missing lifetimes in `UdpStack` HOT 15
- bind-multiple is hard to imlement on smoltcp HOT 4
- Notes for implementer not visible on docs.rs
- TCP socket state/error handling insufficiently specified HOT 3
- [Proposal] Socket-directed API HOT 17
- TCP: receive_exact? HOT 8
- Dealing with stacks that need pinning HOT 5
- How to implement RAII / Drop for Sockets? HOT 4
- TCP: Typestate client and server sockets HOT 1
- need an object that can impl uWrite HOT 7
- Proposal: embedded-nal-async HOT 2
- Reintroduce UDP traits for async
- Restore feature parity in async TCP HOT 1
- AVR Applications using the embedded-nal crate fail to compile
- #use of connect() for tftp protocol HOT 8
- New release of `embedded-nal` HOT 3
- `Dns` traits force caching in-flight DNS requests HOT 1
- get_host_by_name: Clarify behavior w/rt AI_V4MAPPED and AI_ADDRCONFIG
- SocketAddr associated types HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from embedded-nal.