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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use embedded_io_async::{Read, Write};
use futures::{Sink, Stream};
use crate::{
FramedCore, ReadError, WriteError,
decode::Decoder,
encode::Encoder,
state::{ReadState, ReadWriteState, WriteState},
};
/// A framer that reads bytes from a [`Read`] source and decodes them into frames using a [`Decoder`].
/// And a sink that writes encoded frames into an underlying [`Write`] sink using an [`Encoder`].
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Framed<'buf, C, RW> {
/// The core framed implementation.
///
/// This field is made public to be used in the [`functions`](crate::functions) module for library authors.
/// If you are using this crate as a user, you should probably not care about this field.
pub core: FramedCore<'buf, C, RW>,
}
impl<'buf, C, RW> Framed<'buf, C, RW> {
/// Creates a new [`Framed`] with the given `codec` and `reader/writer`.
#[inline]
pub const fn new(
codec: C,
inner: RW,
read_buffer: &'buf mut [u8],
write_buffer: &'buf mut [u8],
) -> Self {
Self {
core: FramedCore::new(
codec,
inner,
ReadWriteState::new(ReadState::new(read_buffer), WriteState::new(write_buffer)),
),
}
}
/// Returns reference to the codec.
#[inline]
pub const fn codec(&self) -> &C {
self.core.codec()
}
/// Returns mutable reference to the codec.
#[inline]
pub const fn codec_mut(&mut self) -> &mut C {
self.core.codec_mut()
}
/// Returns reference to the reader/writer.
#[inline]
pub const fn inner(&self) -> &RW {
self.core.inner()
}
/// Returns mutable reference to the reader/writer.
#[inline]
pub const fn inner_mut(&mut self) -> &mut RW {
self.core.inner_mut()
}
/// Consumes the [`Framed`] and returns the `codec` and `reader/writer` and state.
#[inline]
pub fn into_parts(self) -> (C, RW, ReadWriteState<'buf>) {
self.core.into_parts()
}
#[inline]
/// Creates a new [`Framed`] from its parts.
pub const fn from_parts(codec: C, read_write: RW, state: ReadWriteState<'buf>) -> Self {
Self {
core: FramedCore::from_parts(codec, read_write, state),
}
}
/// Returns the number of bytes that can be framed.
#[inline]
pub const fn framable(&self) -> usize {
self.core.framable()
}
/// Tries to read a frame from the underlying reader.
///
/// # Return value
///
/// - `Some(Ok(None))` if the buffer is not framable. Call `maybe_next` again to read more bytes.
/// - `Some(Ok(Some(frame)))` if a frame was successfully decoded. Call `maybe_next` again to read more bytes.
/// - `Some(Err(error))` if an error occurred. The caller should stop reading.
/// - `None` if eof was reached. The caller should stop reading.
///
/// # Usage
///
/// See [`next!`](crate::next!).
///
/// # Example
///
/// Convert bytes into [`str`] frames
///
/// ```rust
/// use core::{error::Error};
///
/// use framez::{Framed, codec::lines::StrLines, mock::Noop, next};
///
/// async fn read() -> Result<(), Box<dyn Error>> {
/// let r_buf = &mut [0u8; 1024];
/// let w_buf = &mut [0u8; 1024];
///
/// let mut framed = Framed::new(StrLines::new(), Noop, r_buf, w_buf);
///
/// while let Some(item) = next!(framed).transpose()? {
/// println!("Frame: {}", item);
/// }
///
/// Ok(())
/// }
/// ```
pub async fn maybe_next<'this>(
&'this mut self,
) -> Option<Result<Option<C::Item>, ReadError<RW::Error, C::Error>>>
where
C: Decoder<'this>,
RW: Read,
{
self.core.maybe_next().await
}
/// Converts the [`Framed`] into a stream of frames using the given `map` function.
///
/// # Example
///
/// Convert bytes into a stream of Strings
///
/// ```rust
/// use core::{error::Error, pin::pin, str::FromStr};
///
/// use framez::{Framed, codec::lines::StrLines, mock::Noop};
/// use futures::StreamExt;
///
/// async fn read() -> Result<(), Box<dyn Error>> {
/// let r_buf = &mut [0u8; 1024];
/// let w_buf = &mut [0u8; 1024];
///
/// let mut framed = Framed::new(StrLines::new(), Noop, r_buf, w_buf);
///
/// let stream = framed.stream(String::from_str);
/// let mut stream = pin!(stream);
///
/// while let Some(item) = stream.next().await.transpose()?.transpose()? {
/// println!("Frame: {}", item);
/// }
///
/// Ok(())
/// }
/// ```
pub fn stream<U>(
&mut self,
map: fn(<C as Decoder<'_>>::Item) -> U,
) -> impl Stream<Item = Result<U, ReadError<RW::Error, C::Error>>> + '_
where
U: 'static,
C: for<'a> Decoder<'a>,
RW: Read,
{
self.core.stream(map)
}
/// Tries to read a frame from the underlying reader and converts it using the given `map` function.
///
/// # Return value
///
/// - `Some(Ok(U))` if a frame was successfully decoded and mapped. Call `next` again to read more frames.
/// - `Some(Err(error))` if an error occurred. The caller should stop reading.
/// - `None` if eof was reached. The caller should stop reading.
pub async fn next<U>(
&mut self,
map: fn(<C as Decoder<'_>>::Item) -> U,
) -> Option<Result<U, ReadError<RW::Error, C::Error>>>
where
U: 'static,
C: for<'a> Decoder<'a>,
RW: Read,
{
self.core.next(map).await
}
/// Writes a frame to the underlying `writer` and flushes it.
pub async fn send<I>(&mut self, item: I) -> Result<(), WriteError<RW::Error, C::Error>>
where
C: Encoder<I>,
RW: Write,
{
self.core.send(item).await
}
/// Converts the [`Framed`] into a sink.
pub fn sink<'this, I>(
&'this mut self,
) -> impl Sink<I, Error = WriteError<RW::Error, C::Error>> + 'this
where
I: 'this,
C: Encoder<I>,
RW: Write,
{
self.core.sink()
}
}
/// A framer that reads bytes from a [`Read`] source and decodes them into frames using a [`Decoder`].
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct FramedRead<'buf, C, R> {
/// The core framed implementation.
///
/// This field is made public to be used in the [`functions`](crate::functions) module for library authors.
/// If you are using this crate as a user, you should probably not care about this field.
pub core: FramedCore<'buf, C, R>,
}
impl<'buf, C, R> FramedRead<'buf, C, R> {
/// Creates a new [`FramedRead`] with the given `decoder` and `reader`.
#[inline]
pub const fn new(codec: C, reader: R, buffer: &'buf mut [u8]) -> Self {
Self {
core: FramedCore::new(
codec,
reader,
ReadWriteState::new(ReadState::new(buffer), WriteState::empty()),
),
}
}
/// Returns reference to the codec.
#[inline]
pub const fn codec(&self) -> &C {
self.core.codec()
}
/// Returns mutable reference to the codec.
#[inline]
pub const fn codec_mut(&mut self) -> &mut C {
self.core.codec_mut()
}
/// Returns reference to the reader.
#[inline]
pub const fn inner(&self) -> &R {
self.core.inner()
}
/// Returns mutable reference to the reader.
#[inline]
pub const fn inner_mut(&mut self) -> &mut R {
self.core.inner_mut()
}
/// Consumes the [`FramedRead`] and returns the `codec` and `reader` and state.
#[inline]
pub fn into_parts(self) -> (C, R, ReadState<'buf>) {
let (codec, reader, state) = self.core.into_parts();
(codec, reader, state.read)
}
#[inline]
/// Creates a new [`FramedRead`] from its parts.
pub const fn from_parts(codec: C, read: R, state: ReadState<'buf>) -> Self {
Self {
core: FramedCore::from_parts(
codec,
read,
ReadWriteState::new(state, WriteState::empty()),
),
}
}
/// Returns the number of bytes that can be framed.
#[inline]
pub const fn framable(&self) -> usize {
self.core.framable()
}
/// See [`Framed::maybe_next`].
pub async fn maybe_next<'this>(
&'this mut self,
) -> Option<Result<Option<C::Item>, ReadError<R::Error, C::Error>>>
where
C: Decoder<'this>,
R: Read,
{
self.core.maybe_next().await
}
/// See [`Framed::stream`].
pub fn stream<U>(
&mut self,
map: fn(<C as Decoder<'_>>::Item) -> U,
) -> impl Stream<Item = Result<U, ReadError<R::Error, C::Error>>> + '_
where
U: 'static,
C: for<'a> Decoder<'a>,
R: Read,
{
self.core.stream(map)
}
/// See [`Framed::next`].
pub async fn next<U>(
&mut self,
map: fn(<C as Decoder<'_>>::Item) -> U,
) -> Option<Result<U, ReadError<R::Error, C::Error>>>
where
U: 'static,
C: for<'a> Decoder<'a>,
R: Read,
{
self.core.next(map).await
}
}
/// A sink that writes encoded frames into an underlying [`Write`] sink using an [`Encoder`].
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct FramedWrite<'buf, C, W> {
/// The core framed implementation.
///
/// This field is made public to be used in the [`functions`](crate::functions) module for library authors.
/// If you are using this crate as a user, you should probably not care about this field.
pub core: FramedCore<'buf, C, W>,
}
impl<'buf, C, W> FramedWrite<'buf, C, W> {
/// Creates a new [`FramedWrite`] with the given `encoder` and `writer`.
#[inline]
pub const fn new(codec: C, writer: W, buffer: &'buf mut [u8]) -> Self {
Self {
core: FramedCore::new(
codec,
writer,
ReadWriteState::new(ReadState::empty(), WriteState::new(buffer)),
),
}
}
/// Returns reference to the codec.
#[inline]
pub const fn codec(&self) -> &C {
self.core.codec()
}
/// Returns mutable reference to the codec.
#[inline]
pub const fn codec_mut(&mut self) -> &mut C {
self.core.codec_mut()
}
/// Returns reference to the writer.
#[inline]
pub const fn inner(&self) -> &W {
self.core.inner()
}
/// Returns mutable reference to the writer.
#[inline]
pub const fn inner_mut(&mut self) -> &mut W {
self.core.inner_mut()
}
/// Consumes the [`FramedWrite`] and returns the `codec` and `writer` and state.
#[inline]
pub fn into_parts(self) -> (C, W, WriteState<'buf>) {
let (codec, writer, state) = self.core.into_parts();
(codec, writer, state.write)
}
#[inline]
/// Creates a new [`FramedWrite`] from its parts.
pub const fn from_parts(codec: C, write: W, state: WriteState<'buf>) -> Self {
Self {
core: FramedCore::from_parts(
codec,
write,
ReadWriteState::new(ReadState::empty(), state),
),
}
}
/// See [`Framed::send`].
pub async fn send<I>(&mut self, item: I) -> Result<(), WriteError<W::Error, C::Error>>
where
C: Encoder<I>,
W: Write,
{
self.core.send(item).await
}
/// See [`Framed::sink`].
pub fn sink<'this, I>(
&'this mut self,
) -> impl Sink<I, Error = WriteError<W::Error, C::Error>> + 'this
where
I: 'this,
C: Encoder<I>,
W: Write,
{
self.core.sink()
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::redundant_pattern_matching)]
#![allow(clippy::let_underscore_future)]
use core::{pin::pin, str::FromStr};
use std::string::String;
use embedded_io_adapters::tokio_1::FromTokio;
use futures::{SinkExt, StreamExt};
use crate::{Framed, FramedRead, FramedWrite, codec::lines::StrLines, next};
#[tokio::test]
#[ignore = "assert that next! macro works on Framed"]
async fn assert_next() {
let (mut stream, _) = tokio::io::duplex(1024);
let read_buf = &mut [0u8; 1024];
let write_buf = &mut [0u8; 1024];
{
let mut framed = Framed::new(
StrLines::new(),
FromTokio::new(&mut stream),
read_buf,
write_buf,
);
while let Some(_) = next!(framed) {}
_ = framed.send("Line").await;
}
{
let mut framed =
FramedRead::new(StrLines::new(), FromTokio::new(&mut stream), read_buf);
while let Some(_) = next!(framed) {}
}
}
#[tokio::test]
#[ignore = "assert that stream() works on Framed"]
async fn assert_stream() {
let (mut stream, _) = tokio::io::duplex(1024);
let read_buf = &mut [0u8; 1024];
let write_buf = &mut [0u8; 1024];
{
let mut framed = Framed::new(
StrLines::new(),
FromTokio::new(&mut stream),
read_buf,
write_buf,
);
let stream = framed.stream(String::from_str);
let mut stream = pin!(stream);
while let Some(_) = stream.next().await {}
}
{
let mut framed =
FramedRead::new(StrLines::new(), FromTokio::new(&mut stream), read_buf);
let stream = framed.stream(String::from_str);
let mut stream = pin!(stream);
while let Some(_) = stream.next().await {}
}
}
#[tokio::test]
#[ignore = "assert that stream() is movable"]
async fn assert_stream_movable() {
let (mut stream, _) = tokio::io::duplex(1024);
let read_buf = &mut [0u8; 1024];
let write_buf = &mut [0u8; 1024];
{
let mut framed = Framed::new(
StrLines::new(),
FromTokio::new(&mut stream),
read_buf,
write_buf,
);
let _ = async move {
// We should be able to move framed and call stream on it.
let stream = framed.stream(String::from_str);
let mut stream = pin!(stream);
while let Some(_) = stream.next().await {}
};
}
{
let mut framed =
FramedRead::new(StrLines::new(), FromTokio::new(&mut stream), read_buf);
let _ = async move {
// We should be able to move framed and call stream on it.
let stream = framed.stream(String::from_str);
let mut stream = pin!(stream);
while let Some(_) = stream.next().await {}
};
}
}
#[tokio::test]
#[ignore = "assert that sink() works on Framed"]
async fn assert_sink() {
let (mut stream, _) = tokio::io::duplex(1024);
let read_buf = &mut [0u8; 1024];
let write_buf = &mut [0u8; 1024];
{
let mut framed = Framed::new(
StrLines::new(),
FromTokio::new(&mut stream),
read_buf,
write_buf,
);
let sink = framed.sink();
let mut sink = pin!(sink);
_ = sink.send("Line").await;
}
{
let mut framed =
FramedWrite::new(StrLines::new(), FromTokio::new(&mut stream), write_buf);
let sink = framed.sink();
let mut sink = pin!(sink);
_ = sink.send("Line").await;
}
}
#[tokio::test]
#[ignore = "assert that sink() is movable"]
async fn assert_sink_movable() {
let (mut stream, _) = tokio::io::duplex(1024);
let read_buf = &mut [0u8; 1024];
let write_buf = &mut [0u8; 1024];
{
let mut framed = Framed::new(
StrLines::new(),
FromTokio::new(&mut stream),
read_buf,
write_buf,
);
let _ = async move {
// We should be able to move framed and call sink on it.
let sink = framed.sink();
let mut sink = pin!(sink);
_ = sink.send("Line").await;
};
}
{
let mut framed =
FramedWrite::new(StrLines::new(), FromTokio::new(&mut stream), write_buf);
let _ = async move {
// We should be able to move framed and call sink on it.
let sink = framed.sink();
let mut sink = pin!(sink);
_ = sink.send("Line").await;
};
}
}
}