1 /**
2  * Copyright: Copyright Jason White, 2014-2016
3  * License:   $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4  * Authors:   Jason White
5  */
6 module io.stream.shim;
7 
8 import io.stream.traits;
9 import io.stream.types;
10 
11 /**
12  * Wraps a stream to provide useful higher-level functions.
13  */
14 struct StreamShim(Stream)
15     if (isStream!Stream)
16 {
17     Stream stream;
18 
19     alias stream this;
20 
21     /**
22      * Copying is disabled. Reference counting should be used instead.
23      */
24     @disable this(this);
25 
26     /**
27      * Forwards arguments to super class.
28      */
29     this(T...)(auto ref T args)
30     {
31         import std.functional : forward;
32         stream = Stream(forward!args);
33     }
34 
35     static if (isSource!Stream)
36     {
37         /**
38          * Fills the given buffer with data from the stream.
39          *
40          * Note: This is not guaranteed to read the entire buffer from the
41          * stream.  If $(D T.sizeof) is larger than 1, it is possible that an
42          * element is partially read. If a guarantee that the entire buffer is
43          * filled, use $(D readExactly) instead.
44          *
45          * Returns: The number of bytes read.
46          */
47         size_t read(T)(T[] buf)
48         {
49             return stream.read(cast(ubyte[])buf);
50         }
51     }
52 
53     static if (isSink!Stream)
54     {
55         /**
56          * Writes an array of type T to the stream.
57          *
58          * Returns: The number of bytes written.
59          *
60          * Note: This is not guaranteed to write the entire buffer to the
61          * stream. If $(D T.sizeof) is larger than 1, it is possible that an
62          * element may not be fully written. If the guarantee that the entire
63          * buffer is written to the stream, use $(D writeExactly) instead.
64          */
65         size_t write(T)(in T[] buf)
66         {
67             return stream.write(cast(const(ubyte)[])buf);
68         }
69 
70         alias put = write;
71     }
72 }