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.interfaces;
7 
8 import io.stream.types;
9 import io.stream.traits;
10 
11 /**
12  * A source is a stream that can be read from.
13  */
14 interface Source
15 {
16     /**
17      * Reads data into the specified buffer. The number of bytes read is
18      * returned.
19      */
20     size_t read(ubyte[] buf);
21 }
22 
23 unittest
24 {
25     static assert( isSource!Source);
26     static assert(!isSink!Source);
27     static assert(!isSeekable!Source);
28 }
29 
30 /**
31  * A sink is a stream that can be written to.
32  */
33 interface Sink
34 {
35     /**
36      * Writes data to the stream. The number of bytes successfully written is
37      * returned.
38      */
39     size_t write(in ubyte[] data);
40 
41     /// Ditto
42     alias put = write;
43 }
44 
45 unittest
46 {
47     static assert(!isSource!Sink);
48     static assert( isSink!Sink);
49     static assert(!isSeekable!Sink);
50 }
51 
52 /**
53  * A stream that is both a Source and a Sink.
54  */
55 interface SourceSink : Source, Sink {}
56 
57 /**
58  * A seekable stream can move the read/write starting position in the stream.
59  */
60 interface Seekable(Stream) : Stream
61 {
62     /**
63      * Seeks to the specified offset relative to the given starting location.
64      *
65      * Params:
66      *   offset = The offset relative to $(D from).
67      *   from = The relative position to seek to.
68      */
69     long seekTo(long offset, From from = From.start);
70 }
71 
72 unittest
73 {
74     static assert( isSource!(Seekable!Source));
75     static assert( isSink!(Seekable!Sink));
76     static assert( isSource!(Seekable!SourceSink));
77     static assert( isSink!(Seekable!SourceSink));
78     static assert(!isSource!(Seekable!Sink));
79     static assert(!isSink!(Seekable!Source));
80     static assert( isSeekable!(Seekable!Source));
81     static assert( isSeekable!(Seekable!Sink));
82     static assert( isSeekable!(Seekable!SourceSink));
83 }
84 
85 unittest
86 {
87     static assert(is(Seekable!SourceSink : Source));
88     static assert(is(Seekable!SourceSink : Sink));
89     static assert(is(Seekable!Source : Source));
90     static assert(is(Seekable!Sink : Sink));
91     static assert(!is(Seekable!Source : Sink));
92     static assert(!is(Seekable!Sink : Source));
93 }