Giter Site home page Giter Site logo

Comments (6)

jonahd-g avatar jonahd-g commented on June 29, 2024

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.

jonahd-g avatar jonahd-g commented on June 29, 2024

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.

eldruin avatar eldruin commented on June 29, 2024

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
        }
    }
};

On the playground

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);

On the playground

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.

Sympatron avatar Sympatron commented on June 29, 2024

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.

jonahd-g avatar jonahd-g commented on June 29, 2024

@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.

jonahd-g avatar jonahd-g commented on June 29, 2024

@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;
    }
}

Playground

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.