flexible-io 0.6.0

Wraps values such that dyn-safe IO traits need not appear as static bounds
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
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
use crate::stable_with_metadata_of::WithMetadataOf;

use std::{
    any::Any,
    io::{Seek, Write},
};

#[cfg(target_os = "windows")]
use std::os::windows::{
    fs::FileExt,
    io::{AsHandle, AsRawHandle, AsRawSocket, AsSocket},
};

#[cfg(target_family = "unix")]
use std::os::{
    fd::{AsFd, AsRawFd},
    unix::fs::FileExt,
};

/// A writer, which can dynamically provide IO traits.
///
/// The following traits may be optionally dynamically provided:
///
/// * [`Seek`]
///
/// The struct comes with a number of setter methods. The call to these requires proof to the
/// compiler that the bound is met, inserting the vtable from the impl instance. Afterward, the
/// bound is not required by any user. Using the (mutable) getters recombines the vtable with the
/// underlying value.
///
/// ## Usage
///
/// ```
/// # use flexible_io::Writer;
/// use std::io::SeekFrom;
///
/// let mut buffer: Vec<u8> = vec![];
/// let cursor = std::io::Cursor::new(&mut buffer);
/// let mut writer = Writer::new(cursor);
/// assert!(writer.as_seek().is_none());
///
/// writer
///     .as_write_mut()
///     .write_all(b"Hello, brain!")
///     .unwrap();
///
/// // But cursors are seekable, let's tell everyone.
/// writer.set_seek();
/// assert!(writer.as_seek().is_some());
///
/// // Now use the Seek implementation to undo our mistake
/// let seek = writer.as_seek_mut().unwrap();
/// seek.seek(SeekFrom::Start(7));
///
/// writer
///     .as_write_mut()
///     .write_all(b"world!")
///     .unwrap();
///
/// let contents: &Vec<u8> = writer.get_ref().get_ref();
/// assert_eq!(contents, b"Hello, world!");
/// ```
pub struct Writer<W: ?Sized> {
    write: *mut dyn Write,
    vtable: OptTable,
    inner: W,
}

#[derive(Clone, Copy, Default)]
struct OptTable {
    seek: Option<*mut dyn Seek>,
    any: Option<*mut dyn Any>,

    // Unix family traits:
    #[cfg(target_family = "unix")]
    file_ext: Option<*mut dyn FileExt>,
    #[cfg(target_family = "unix")]
    as_fd: Option<*mut dyn AsFd>,
    #[cfg(target_family = "unix")]
    as_raw_fd: Option<*mut dyn AsRawFd>,

    // Windows only traits:
    #[cfg(target_os = "windows")]
    file_ext: Option<*mut dyn FileExt>,
    #[cfg(target_os = "windows")]
    as_handle: Option<*mut dyn AsHandle>,
    #[cfg(target_os = "windows")]
    as_raw_handle: Option<*mut dyn AsRawHandle>,
    #[cfg(target_os = "windows")]
    as_socket: Option<*mut dyn AsSocket>,
    #[cfg(target_os = "windows")]
    as_raw_socket: Option<*mut dyn AsRawSocket>,
}

/// A box around a type-erased [`Writer`].
pub struct WriterBox<'lt> {
    inner: Box<dyn Write + 'lt>,
    vtable: OptTable,
}

impl<W: Write> Writer<W> {
    /// Wrap an underlying writer by-value.
    pub fn new(mut writer: W) -> Self {
        let write = lifetime_erase_trait_vtable!((&mut writer): '_ as Write);

        Writer {
            inner: writer,
            write,
            vtable: OptTable::default(),
        }
    }
}

impl<W: ?Sized> Writer<W> {
    /// Provide access to the underlying writer.
    pub fn get_ref(&self) -> &W {
        &self.inner
    }

    /// Provide mutable access to the underlying writer.
    pub fn get_mut(&mut self) -> &mut W {
        &mut self.inner
    }

    /// Get a view equivalent to very-fat mutable reference.
    ///
    /// This erases the concrete type `W` which allows consumers that intend to avoid polymorphic
    /// code that monomorphizes. The mutable reference has all accessors of a mutable reference
    /// except it doesn't offer access with the underlying writer's type itself.
    pub fn as_mut(&mut self) -> WriterMut<'_> {
        // Copy out all the vtable portions, we need a mutable reference to `self` for the
        // conversion into a dynamically typed `&mut dyn Read`.
        let Writer {
            inner: _,
            write: _,
            vtable,
        } = *self;

        WriterMut {
            inner: self.as_write_mut(),
            vtable,
        }
    }

    /// Get a view equivalent to very-fat mutable reference.
    ///
    /// This erases the concrete type `W` which allows consumers that intend to avoid polymorphic
    /// code that monomorphizes. The mutable reference has all accessors of a mutable reference
    /// except it doesn't offer access with the underlying reader's type itself.
    pub fn into_boxed<'lt>(self) -> WriterBox<'lt>
    where
        W: Sized + 'lt,
    {
        let Writer {
            inner,
            write,
            vtable,
        } = self;

        let ptr = Box::into_raw(Box::new(inner));
        let ptr = WithMetadataOf::with_metadata_of_on_stable(ptr, write);
        let inner = unsafe { Box::from_raw(ptr) };

        WriterBox { inner, vtable }
    }
}

dyn_setter! {
    impl<W> Writer<W> = self as that {
        /// Set the V-Table of [`Seek`].
        ///
        /// After this call, the methods [`Self::as_seek`] and [`Self::as_seek_mut`] will return values.
        fn set_seek -> Seek = that.vtable.seek;

        /// Set the V-Table for [`Any`].
        ///
        /// After this call, the methods [`Self::as_any`] and [`Self::as_any_mut`] will return values.
        fn set_any -> Any = that.vtable.any;
    }
}

#[cfg(target_family = "unix")]
dyn_setter! {
    impl<W> Writer<W> = self as that {
        /// Set the V-Table for [`FileExt`].
        ///
        /// After this call, the methods [`Self::as_file_ext`] will return a value. (This trait only has
        /// methods with a `&self` receiver).
        fn set_file_ext -> FileExt = that.vtable.file_ext;

        /// Set the V-Table for [`AsRawFd`].
        ///
        /// After this call, the methods [`Self::as_fd`] will return a value. (This trait only has
        /// methods with a `&self` receiver).
        fn set_as_fd -> AsFd = that.vtable.as_fd;

        /// Set the V-Table for [`AsRawFd`].
        ///
        /// After this call, the methods [`Self::as_raw_fd`] will return a value. (This trait only has
        /// methods with a `&self` receiver).
        fn set_as_raw_fd -> AsRawFd = that.vtable.as_raw_fd;
    }
}

#[cfg(target_os = "windows")]
dyn_setter! {
    impl<W> Writer<W> = self as that {
        /// Set the V-Table for [`FileExt`].
        ///
        /// After this call, the methods [`Self::as_file_ext`] will return a value. (This trait only has
        /// methods with a `&self` receiver).
        fn set_file_ext -> FileExt = that.vtable.file_ext;

        /// Set the V-Table for [`AsHandle`].
        ///
        /// After this call, the methods [`Self::as_handle`] will return a value. (This trait only has
        /// methods with a `&self` receiver).
        fn set_as_handle -> AsHandle = that.vtable.as_handle;

        /// Set the V-Table for [`AsRawHandle`].
        ///
        /// After this call, the methods [`Self::as_raw_handle`] will return a value. (This trait only has
        /// methods with a `&self` receiver).
        fn set_as_raw_handle -> AsRawHandle = that.vtable.as_raw_handle;

        /// Set the V-Table for [`AsSocket`].
        ///
        /// After this call, the methods [`Self::as_socket`] will return a value. (This trait only has
        /// methods with a `&self` receiver).
        fn set_as_socket -> AsSocket = that.vtable.as_socket;

        /// Set the V-Table for [`AsRawSocket`].
        ///
        /// After this call, the methods [`Self::as_raw_socket`] will return a value. (This trait only has
        /// methods with a `&self` receiver).
        fn set_as_raw_socket -> AsRawSocket = that.vtable.as_raw_socket;
    }
}

impl<W: ?Sized> Writer<W> {
    /// Get the inner value as a dynamic `Write` reference.
    pub fn as_write(&self) -> &(dyn Write + '_) {
        let ptr = &self.inner as *const W;
        let local = WithMetadataOf::with_metadata_of_on_stable(ptr, self.write);
        unsafe { &*local }
    }

    /// Get the inner value as a mutable dynamic `Write` reference.
    pub fn as_write_mut(&mut self) -> &mut (dyn Write + '_) {
        let ptr = &mut self.inner as *mut W;
        let local = WithMetadataOf::with_metadata_of_on_stable(ptr, self.write);
        unsafe { &mut *local }
    }

    /// Unwrap the inner value at its original sized type.
    pub fn into_inner(self) -> W
    where
        W: Sized,
    {
        self.inner
    }
}

dyn_getter! {
    impl<W> Writer<W> = self as that {
        unsafe const ptr: &that.inner as *const W;
        unsafe mut ptr: &mut that.inner as *mut W;
    } {
        /// Get the inner value as a dynamic `Seek` reference.
        ///
        /// This returns `None` unless a previous call to [`Self::set_seek`] was executed, by any other caller.
        /// The value can be moved after such call arbitrarily.
        fn as_seek {
            /// Get the inner value as a mutable dynamic `Seek` reference.
            ///
            /// This returns `None` unless a previous call to [`Self::set_seek`] was executed, by any other caller.
            /// The value can be moved after such call arbitrarily.
            mut: fn as_seek_mut
        } -> Seek = that.vtable.seek;

        /// Get the inner value as a dynamic `Any` reference.
        fn as_any {
            /// Get the inner value as a dynamic `Any` reference.
            mut: fn as_any_mut
        } -> Any = that.vtable.any;
    }
}

#[cfg(target_family = "unix")]
dyn_getter! {
    impl<W> Writer<W> = self as that {
        unsafe const ptr: &that.inner as *const W;
        unsafe mut ptr: &mut that.inner as *mut W;
    } {
        /// Get the inner value as a dynamic [`FileExt`] reference.
        fn as_file_ext -> FileExt = that.vtable.file_ext;

        /// Get the inner value as a dynamic [`AsFd`] reference.
        fn as_fd -> AsFd = that.vtable.as_fd;

        /// Get the inner value as a dynamic [`AsRawFd`] reference.
        fn as_raw_fd -> AsRawFd = that.vtable.as_raw_fd;
    }
}

#[cfg(target_os = "windows")]
dyn_getter! {
    impl<W> Writer<W> = self as that {
        unsafe const ptr: &that.inner as *const W;
        unsafe mut ptr: &mut that.inner as *mut W;
    } {
        /// Get the inner value as a dynamic [`FileExt`] reference.
        fn as_file_ext -> FileExt = that.vtable.file_ext;

        /// Get the inner value as a dynamic [`AsHandle`] reference.
        fn as_handle -> AsHandle = that.vtable.as_handle;

        /// Get the inner value as a dynamic [`AsRawHandle`] reference.
        fn as_raw_handle -> AsRawHandle = that.vtable.as_raw_handle;

        /// Get the inner value as a dynamic [`AsSocket`] reference.
        fn as_socket -> AsSocket = that.vtable.as_socket;

        /// Get the inner value as a dynamic [`AsRawSocket`] reference.
        fn as_raw_socket -> AsRawSocket = that.vtable.as_raw_socket;
    }
}

/// A mutable reference to a [`Writer`].
///
/// This type acts similar to a *very* fat mutable reference. It can be obtained by constructing a
/// concrete reader type and calling [`Writer::as_mut`].
///
/// Note: Any mutable reference to a `Writer` implements `Into<WriterMut>` for its lifetime. Use this
/// instead of coercion which would be available if this was a builtin kind of reference.
///
/// Note: Any `Writer` implements `Into<WriterBox>`, which can again be converted to `WriterMut`.
/// Use it for owning a writer without its specific type similar to `Box<dyn Read>`.
pub struct WriterMut<'lt> {
    inner: &'lt mut dyn Write,
    vtable: OptTable,
}

impl WriterMut<'_> {
    /// Get the inner value as a mutable dynamic `Write` reference.
    pub fn as_write_mut(&mut self) -> &mut (dyn Write + '_) {
        &mut *self.inner
    }
}

dyn_getter! {
    impl WriterMut<'_> = self as that {
        unsafe const ptr: that.inner as *const dyn Write;
        unsafe mut ptr: that.inner as *mut dyn Write;
    } {
        /// Get the inner value as a dynamic `Seek` reference.
        ///
        /// This returns `None` unless a previous call to [`Writer::set_seek`] was executed, by any other caller.
        /// The value can be moved after such call arbitrarily.
        fn as_seek {
            /// Get the inner value as a mutable dynamic `Seek` reference.
            ///
            /// This returns `None` unless a previous call to [`Writer::set_seek`] was executed, by any other caller.
            /// The value can be moved after such call arbitrarily.
            mut: fn as_seek_mut
        } -> Seek = that.vtable.seek;

        /// Get the inner value as a dynamic `Any` reference.
        fn as_any {
            /// Get the inner value as a dynamic `Any` reference.
            mut: fn as_any_mut
        } -> Any = that.vtable.any;
    }
}

#[cfg(target_family = "unix")]
dyn_getter! {
    impl WriterMut<'_> = self as that {
        unsafe const ptr: that.inner as *const dyn Write;
        unsafe mut ptr: that.inner as *mut dyn Write;
    } {
        /// Get the inner value as a dynamic [`FileExt`] reference.
        fn as_file_ext -> FileExt = that.vtable.file_ext;

        /// Get the inner value as a dynamic [`AsFd`] reference.
        fn as_fd -> AsFd = that.vtable.as_fd;

        /// Get the inner value as a dynamic [`AsRawFd`] reference.
        fn as_raw_fd -> AsRawFd = that.vtable.as_raw_fd;
    }
}

#[cfg(target_os = "windows")]
dyn_getter! {
    impl WriterMut<'_> = self as that {
        unsafe const ptr: that.inner as *const dyn Write;
        unsafe mut ptr: that.inner as *mut dyn Write;
    } {
        /// Get the inner value as a dynamic [`FileExt`] reference.
        fn as_file_ext -> FileExt = that.vtable.file_ext;

        /// Get the inner value as a dynamic [`AsHandle`] reference.
        fn as_handle -> AsHandle = that.vtable.as_handle;

        /// Get the inner value as a dynamic [`AsRawHandle`] reference.
        fn as_raw_handle -> AsRawHandle = that.vtable.as_raw_handle;

        /// Get the inner value as a dynamic [`AsSocket`] reference.
        fn as_socket -> AsSocket = that.vtable.as_socket;

        /// Get the inner value as a dynamic [`AsRawSocket`] reference.
        fn as_raw_socket -> AsRawSocket = that.vtable.as_raw_socket;
    }
}

impl WriterBox<'_> {
    /// Get a view equivalent to very-fat mutable reference.
    ///
    /// This erases the concrete type `W` which allows consumers that intend to avoid polymorphic
    /// code that monomorphizes. The mutable reference has all accessors of a mutable reference
    /// except it doesn't offer access with the underlying writer's type itself.
    pub fn as_mut(&mut self) -> WriterMut<'_> {
        WriterMut {
            vtable: self.vtable,
            inner: self.as_read_mut(),
        }
    }

    /// Provide mutable access to the underlying writer.
    pub fn as_read_mut(&mut self) -> &mut (dyn Write + '_) {
        &mut *self.inner
    }
}

dyn_getter! {
    impl WriterBox<'_> = self as that {
        unsafe const ptr: that.inner.as_ref() as *const dyn Write;
        unsafe mut ptr: that.inner.as_mut() as *mut dyn Write;
    } {
        /// Get the inner value as a dynamic `Seek` reference.
        ///
        /// This returns `None` unless a previous call to [`Writer::set_seek`] was executed, by any other caller.
        /// The value can be moved after such call arbitrarily.
        fn as_seek {
            /// Get the inner value as a mutable dynamic `Seek` reference.
            ///
            /// This returns `None` unless a previous call to [`Writer::set_seek`] was executed, by any other caller.
            /// The value can be moved after such call arbitrarily.
            mut: fn as_seek_mut
        } -> Seek = that.vtable.seek;

        /// Get the inner value as a dynamic `Any` reference.
        fn as_any {
            /// Get the inner value as a dynamic `Any` reference.
            mut: fn as_any_mut
        } -> Any = that.vtable.any;
    }
}

#[cfg(target_family = "unix")]
dyn_getter! {
    impl WriterBox<'_> = self as that {
        unsafe const ptr: that.inner.as_ref() as *const dyn Write;
        unsafe mut ptr: that.inner.as_mut() as *mut dyn Write;
    } {
        /// Get the inner value as a dynamic [`FileExt`] reference.
        fn as_file_ext -> FileExt = that.vtable.file_ext;

        /// Get the inner value as a dynamic [`AsFd`] reference.
        fn as_fd -> AsFd = that.vtable.as_fd;

        /// Get the inner value as a dynamic [`AsRawFd`] reference.
        fn as_raw_fd -> AsRawFd = that.vtable.as_raw_fd;
    }
}

#[cfg(target_os = "windows")]
dyn_getter! {
    impl WriterBox<'_> = self as that {
        unsafe const ptr: that.inner.as_ref() as *const dyn Write;
        unsafe mut ptr: that.inner.as_mut() as *mut dyn Write;
    } {
        /// Get the inner value as a dynamic [`FileExt`] reference.
        fn as_file_ext -> FileExt = that.vtable.file_ext;

        /// Get the inner value as a dynamic [`AsHandle`] reference.
        fn as_handle -> AsHandle = that.vtable.as_handle;

        /// Get the inner value as a dynamic [`AsRawHandle`] reference.
        fn as_raw_handle -> AsRawHandle = that.vtable.as_raw_handle;

        /// Get the inner value as a dynamic [`AsSocket`] reference.
        fn as_socket -> AsSocket = that.vtable.as_socket;

        /// Get the inner value as a dynamic [`AsRawSocket`] reference.
        fn as_raw_socket -> AsRawSocket = that.vtable.as_raw_socket;
    }
}

impl<'lt, R> From<&'lt mut Writer<R>> for WriterMut<'lt> {
    fn from(value: &'lt mut Writer<R>) -> Self {
        value.as_mut()
    }
}

impl<'lt, R: 'lt> From<Writer<R>> for WriterBox<'lt> {
    fn from(value: Writer<R>) -> Self {
        value.into_boxed()
    }
}

impl<'lt> From<&'lt mut WriterBox<'_>> for WriterMut<'lt> {
    fn from(value: &'lt mut WriterBox) -> Self {
        value.as_mut()
    }
}