ergot 0.12.0

Eloquence in messaging
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! "Raw Owned" sockets
//!
//! "Owned" sockets require `T: 'static`, and store messages in their deserialized `T` form,
//! rather as serialized bytes.
//!
//! "Raw Owned" sockets are generic over the [`Storage`] trait, which describes a basic
//! ring buffer. The [`owned`](crate::socket::owned) module contains variants of this
//! raw socket that use a specific kind of ring buffer impl, e.g. using std or stackful
//! storage.

use core::{
    any::TypeId,
    cell::UnsafeCell,
    marker::PhantomData,
    pin::Pin,
    ptr::{NonNull, addr_of},
    task::{Context, Poll, Waker},
};

use cordyceps::list::Links;
use serde::de::DeserializeOwned;

use super::{Attributes, HeaderMessage, Response, SocketHeader, SocketSendError, SocketVTable};
use crate::logging::trace;
use crate::{HeaderSeq, Key, ProtocolError, nash::NameHash, net_stack::NetStackHandle};

#[derive(Debug, PartialEq)]
pub struct StorageFull;

pub trait Storage<T: 'static>: 'static {
    fn is_full(&self) -> bool;
    fn is_empty(&self) -> bool;
    fn push(&mut self, t: T) -> Result<(), StorageFull>;
    fn try_pop(&mut self) -> Option<T>;
}

/// SocketPtr represents an equivalent ownership as Pin<&'a mut Socket<S, T, N>>,
/// however we use it to abstract over `Pin<&mut Socket<S, T, N>>` and
/// `Pin<Box<Socket<S, T, N>>>`.
///
/// This is achieved by having an `on_drop` function, which for borrowed items
/// is a no-op, but for owned/boxed items acts as a drop.
struct SocketPtr<'a, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    ptr: NonNull<Socket<S, T, N>>,
    on_drop: unsafe fn(NonNull<Socket<S, T, N>>),
    _pd: PhantomData<Pin<&'a mut Socket<S, T, N>>>,
}

impl<S, T, N> SocketPtr<'_, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    pub(crate) fn as_ptr(&self) -> NonNull<Socket<S, T, N>> {
        self.ptr
    }
}

impl<S, T, N> Drop for SocketPtr<'_, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    fn drop(&mut self) {
        unsafe { (self.on_drop)(self.ptr) }
    }
}

impl<S, T, N> From<Pin<&mut Socket<S, T, N>>> for SocketPtr<'_, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    fn from(value: Pin<&mut Socket<S, T, N>>) -> Self {
        let ptr_self: NonNull<Socket<S, T, N>> =
            NonNull::from(unsafe { value.get_unchecked_mut() });
        SocketPtr {
            ptr: ptr_self,
            on_drop: Socket::<S, T, N>::nop_drop,
            _pd: PhantomData,
        }
    }
}

#[cfg(feature = "std")]
impl<S, T, N> From<Pin<Box<Socket<S, T, N>>>> for SocketPtr<'static, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    fn from(value: Pin<Box<Socket<S, T, N>>>) -> Self {
        let box_self: Box<Socket<S, T, N>> = unsafe { Pin::into_inner_unchecked(value) };
        let ptr_self: NonNull<Socket<S, T, N>> =
            unsafe { NonNull::new_unchecked(Box::into_raw(box_self)) };
        SocketPtr {
            ptr: ptr_self,
            on_drop: Socket::<S, T, N>::pin_box_drop,
            _pd: PhantomData,
        }
    }
}

// Socket
#[repr(C)]
pub struct Socket<S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    // LOAD BEARING: must be first
    hdr: UnsafeCell<SocketHeader>,
    pub(crate) net: N::Target,
    inner: UnsafeCell<StoreBox<S, Response<T>>>,
}

pub struct SocketHdl<'a, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    ptr: SocketPtr<'a, S, T, N>,
    port: u8,
}

pub struct Recv<'a, 'b, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    hdl: &'a mut SocketHdl<'b, S, T, N>,
}

struct StoreBox<S: Storage<T>, T: 'static> {
    wait: Option<Waker>,
    sto: S,
    _pd: PhantomData<fn() -> T>,
}

// ---- impls ----

// impl Socket

impl<S, T, N> Socket<S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    unsafe fn nop_drop(_: NonNull<Self>) {}

    #[cfg(feature = "std")]
    unsafe fn pin_box_drop(this: NonNull<Self>) {
        _ = unsafe { Box::from_raw(this.as_ptr()) };
    }

    pub const fn new(
        net: N::Target,
        key: Key,
        attrs: Attributes,
        sto: S,
        name: Option<&str>,
    ) -> Self {
        Self {
            hdr: UnsafeCell::new(SocketHeader {
                links: Links::new(),
                vtable: const { &Self::vtable() },
                port: 0,
                attrs,
                key,
                nash: if let Some(n) = name {
                    Some(NameHash::new(n))
                } else {
                    None
                },
            }),
            inner: UnsafeCell::new(StoreBox::new(sto)),
            net,
        }
    }

    pub fn attach(self: Pin<&mut Self>) -> SocketHdl<'_, S, T, N> {
        let stack = self.net.clone();
        let sp: SocketPtr<'_, S, T, N> = self.into();
        let ptr_self: NonNull<Self> = sp.as_ptr();
        let ptr_erase: NonNull<SocketHeader> = ptr_self.cast();
        let port = unsafe { stack.attach_socket(ptr_erase) };
        SocketHdl { ptr: sp, port }
    }

    #[cfg(feature = "std")]
    pub fn attach_boxed(self: Pin<Box<Self>>) -> SocketHdl<'static, S, T, N> {
        let stack = self.net.clone();
        let sp: SocketPtr<S, T, N> = self.into();
        let ptr_self: NonNull<Self> = sp.as_ptr();
        let ptr_erase: NonNull<SocketHeader> = ptr_self.cast();
        let port = unsafe { stack.attach_socket(ptr_erase) };
        SocketHdl { ptr: sp, port }
    }

    pub fn attach_broadcast(self: Pin<&mut Self>) -> SocketHdl<'_, S, T, N> {
        let stack = self.net.clone();
        let sp: SocketPtr<'_, S, T, N> = self.into();
        let ptr_self: NonNull<Self> = sp.as_ptr();
        let ptr_erase: NonNull<SocketHeader> = ptr_self.cast();
        unsafe { stack.attach_broadcast_socket(ptr_erase) };
        SocketHdl { ptr: sp, port: 255 }
    }

    #[cfg(feature = "std")]
    pub fn attach_broadcast_boxed(self: Pin<Box<Self>>) -> SocketHdl<'static, S, T, N> {
        let stack = self.net.clone();
        let sp: SocketPtr<S, T, N> = self.into();
        let ptr_self: NonNull<Self> = sp.as_ptr();
        let ptr_erase: NonNull<SocketHeader> = ptr_self.cast();
        unsafe { stack.attach_broadcast_socket(ptr_erase) };
        SocketHdl { ptr: sp, port: 255 }
    }

    const fn vtable() -> SocketVTable {
        SocketVTable {
            recv_owned: Some(Self::recv_owned),
            // TODO: We probably COULD support this, but I'm pretty sure it
            // would require serializing, copying to a buffer, then later
            // deserializing. I really don't know if we WANT this.
            //
            // TODO: EXTRA danger: if the item is borrowed we can't use TypeId,
            // which makes it VERY DIFFICULT to do this soundly: The sender and receiver
            // sockets might not ACTUALLY be the same type, for example if the two
            // types pun to each other, e.g. `&str` and `String`. THERE BE EVEN MORE
            // DRAGONS HERE
            // Update: This is now somewhat better because send_bor passes a serializing fn
            recv_bor: None,
            recv_raw: Self::recv_raw,
            recv_err: Some(Self::recv_err),
        }
    }

    pub fn stack(&self) -> N::Target {
        self.net.clone()
    }

    fn recv_err(this: NonNull<()>, hdr: HeaderSeq, err: ProtocolError) {
        let this: NonNull<Self> = this.cast();
        let this: &Self = unsafe { this.as_ref() };
        let mutitem: &mut StoreBox<S, Response<T>> = unsafe { &mut *this.inner.get() };

        let msg = Err(HeaderMessage { hdr, t: err });
        if mutitem.sto.push(msg).is_ok()
            && let Some(w) = mutitem.wait.take()
        {
            w.wake();
        }
    }

    fn recv_owned(
        this: NonNull<()>,
        that: NonNull<()>,
        hdr: HeaderSeq,
        ty: &TypeId,
    ) -> Result<(), SocketSendError> {
        if &TypeId::of::<T>() != ty {
            debug_assert!(false, "Type Mismatch!");
            return Err(SocketSendError::TypeMismatch);
        }
        let that: NonNull<T> = that.cast();
        let that: &T = unsafe { that.as_ref() };
        let this: NonNull<Self> = this.cast();
        let this: &Self = unsafe { this.as_ref() };
        let mutitem: &mut StoreBox<S, Response<T>> = unsafe { &mut *this.inner.get() };

        let msg = Ok(HeaderMessage {
            hdr,
            t: that.clone(),
        });

        match mutitem.sto.push(msg) {
            Ok(()) => {
                if let Some(w) = mutitem.wait.take() {
                    w.wake();
                }
                Ok(())
            }
            Err(StorageFull) => Err(SocketSendError::NoSpace),
        }
    }

    fn recv_raw(this: NonNull<()>, that: &[u8], hdr: HeaderSeq) -> Result<(), SocketSendError> {
        let this: NonNull<Self> = this.cast();
        let this: &Self = unsafe { this.as_ref() };
        let mutitem: &mut StoreBox<S, Response<T>> = unsafe { &mut *this.inner.get() };

        if mutitem.sto.is_full() {
            trace!("{}: recv_raw: StorageFull", hdr);
            return Err(SocketSendError::NoSpace);
        }

        if let Ok(t) = postcard::from_bytes::<T>(that) {
            let msg = Ok(HeaderMessage { hdr, t });
            let _ = mutitem.sto.push(msg);
            if let Some(w) = mutitem.wait.take() {
                w.wake();
            }
            Ok(())
        } else {
            Err(SocketSendError::DeserFailed)
        }
    }
}

// impl SocketHdl

impl<'a, S, T, N> SocketHdl<'a, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    pub fn port(&self) -> u8 {
        self.port
    }

    pub fn stack(&self) -> N::Target {
        unsafe { (*addr_of!((*self.ptr.as_ptr().as_ptr()).net)).clone() }
    }

    pub fn try_recv(&mut self) -> Option<Response<T>> {
        let net: N::Target = self.stack();
        let f = || {
            let this_ref: &Socket<S, T, N> = unsafe { self.ptr.as_ptr().as_ref() };
            let box_ref: &mut StoreBox<S, Response<T>> = unsafe { &mut *this_ref.inner.get() };

            box_ref.sto.try_pop()
        };
        unsafe { net.with_lock(f) }
    }

    pub fn recv<'b>(&'b mut self) -> Recv<'b, 'a, S, T, N> {
        Recv { hdl: self }
    }
}

impl<S, T, N> Drop for SocketHdl<'_, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    fn drop(&mut self) {
        // SAFETY: the drop of the SocketHdl signifies the end of the pinned relationship
        // of the socket. At the beginning of drop, we are pinned, and then remove ourselves
        // from the socket list.
        //
        // `detach_socket`is performed with the mutex locked.
        //
        // Once we return from this function, the contained `SocketPtr` is dropped, and its
        // `on_drop` function is called. If this handle was created with a `Pin<&mut Socket>`,
        // the socket itself will not yet be dropped. If this SocketHdl was created with a
        // Pin<Box<Socket>>, then that socket will be dropped by the on_drop handler.
        unsafe {
            let net = self.stack();
            let ptr: *mut SocketHeader = self.ptr.as_ptr().as_ref().hdr.get();
            let this: NonNull<SocketHeader> = NonNull::new_unchecked(ptr);
            net.detach_socket(this);
        }
    }
}

unsafe impl<S, T, N> Send for SocketHdl<'_, S, T, N>
where
    S: Storage<Response<T>>,
    T: Send,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
}

unsafe impl<S, T, N> Sync for SocketHdl<'_, S, T, N>
where
    S: Storage<Response<T>>,
    T: Send,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
}

// impl Recv

impl<S, T, N> Future for Recv<'_, '_, S, T, N>
where
    S: Storage<Response<T>>,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
    type Output = Response<T>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let net: N::Target = self.hdl.stack();
        let f = || {
            let this_ref: &Socket<S, T, N> = unsafe { self.hdl.ptr.as_ptr().as_ref() };
            let box_ref: &mut StoreBox<S, Response<T>> = unsafe { &mut *this_ref.inner.get() };

            if let Some(resp) = box_ref.sto.try_pop() {
                return Some(resp);
            }

            let new_wake = cx.waker();
            if let Some(w) = box_ref.wait.take()
                && !w.will_wake(new_wake)
            {
                w.wake();
            }
            // NOTE: Okay to register waker AFTER checking, because we
            // have an exclusive lock
            box_ref.wait = Some(new_wake.clone());
            None
        };
        let res = unsafe { net.with_lock(f) };
        if let Some(t) = res {
            Poll::Ready(t)
        } else {
            Poll::Pending
        }
    }
}

unsafe impl<S, T, N> Sync for Recv<'_, '_, S, T, N>
where
    S: Storage<Response<T>>,
    T: Send,
    T: Clone + DeserializeOwned + 'static,
    N: NetStackHandle,
{
}

// impl StoreBox

impl<S: Storage<T>, T: 'static> StoreBox<S, T> {
    const fn new(sto: S) -> Self {
        Self {
            wait: None,
            sto,
            _pd: PhantomData,
        }
    }
}