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
//! A generic `SourceStream` implementation for any `Iterator` of `char`s and
//! that provides the position of each `char` relative to the start of the
//! iteration.

use std::{
    iter::{Peekable, Map, Enumerate},
    rc::Rc, sync::Arc,
};

use crate::{
    SourceStream, SourceIterItem, Text, TextBase,
    text::chunk::{CharPos, PosStrish, RefCntStrish},
    parser::{DatumAllocator, AllocError},
};


// These functions offer different trade-offs for converting the `String`s used
// for buffering incoming `char`s to the shared-ownership types needed by
// `PosStrish` to achieve zero-copy operations.

/// Converting a `String` to a boxed `str` slice gives the program a chance to
/// use minimal memory by freeing the `String`'s extra capacity.
///
/// This might cheaply convert in place, because it only shrinks the allocated
/// size if the `String`'s capacity is greater than its length (but this is
/// likely), and the allocator has a chance to not move it if reallocation is
/// done and a chance to free and reuse the extra capacity (depending on the
/// allocator used).  But the allocator might move it, which is a cost, but this
/// might be acceptable for the application because that one-time performance
/// cost could be worth giving it the chance to reclaim unused memory from these
/// now-immutable strings which might live a long time.
///
/// But the additional disadvantage of `Rc<Box<str>>` is that there is an extra
/// level of pointer indirection compared to `Rc<str>`, which could impact cache
/// locality.  However, [converting to `Rc<str>`](fn.to_rc_str.html) always must
/// move the string contents and never has the chance of cheaply converting in
/// place.
#[inline]
pub fn to_rc_box_str(s: String) -> Rc<Box<str>> {
    Rc::new(s.into_boxed_str())
}

/// Simply putting a `String` in an `Rc` has the benefit that no moving of the
/// string contents will ever occur, which is always very fast to construct.
///
/// But the disadvantages are: unused `String` capacity is not freed, which
/// might be a problem if you keep many of these alive; and there is an extra
/// level of pointer indirection [compared to `Rc<str>`](fn.to_rc_str.html),
/// which could impact cache locality.
#[inline]
pub fn to_rc_string(s: String) -> Rc<String> {
    Rc::new(s)
}

/// Converting a `String` to an `Rc<str>` slice (which is also "boxed",
/// i.e. heap-allocated) has the benefits of:
///
/// 1) Always freeing the `String`'s unused capacity, which might be important
/// if you keep many of these alive.
///
/// 2) Minimizing pointer indirection to only the one level that is an `Rc`
/// pointing at a heap block, which might be important for cache locality.
///
/// However, this conversion always has the one-time cost of moving the string
/// contents into the new `Rc` allocation.
#[inline]
pub fn to_rc_str(s: String) -> Rc<str> {
    s.into()
}

/// Use an `Arc`, for multi-threaded sharing, for the conversion with the same
/// trade-offs as [`to_rc_box_str`](fn.to_rc_box_str.html).
#[inline]
pub fn to_arc_box_str(s: String) -> Arc<Box<str>> {
    Arc::new(s.into_boxed_str())
}

/// Use an `Arc`, for multi-threaded sharing, for the conversion with the same
/// trade-offs as [`to_rc_string`](fn.to_rc_string.html).
#[inline]
pub fn to_arc_string(s: String) -> Arc<String> {
    Arc::new(s)
}

/// Use an `Arc`, for multi-threaded sharing, for the conversion with the same
/// trade-offs as [`to_rc_str`](fn.to_rc_str.html).
#[inline]
pub fn to_arc_str(s: String) -> Arc<str> {
    s.into()
}


/// A generic [`SourceStream`] implementation for any `Iterator` of `char`s and
/// that provides the position of each `char` relative to the start of the
/// iteration.  The various `to_a?rc(_box)?_str(ing)?` converter functions are
/// used with this.
///
/// This is useful for streaming sources that are not entirely in memory, that
/// do not buffer in ways compatible with `StrishIterSourceStream`, and that do
/// not provide their own (or better) character position information.  Most
/// streaming sources will have their own internal buffers and copy `char`s out
/// of that, and those will be buffered again by `CharIterSourceStream`, so this
/// is not zero-copy in this regard.  But once copied in this way and converted
/// to a shared-ownership type by a converter function (which might copy again
/// one last time, depending on which one is used), the `Text` types we produce
/// have zero-copy operations thereafter (achieved via shared ownership).
///
/// If the streaming source's internal buffering can be made compatible with
/// [`StrishIterSourceStream`], then that should be used instead of
/// `CharIterSourceStream`, because that can achieve full zero-copy operation.
///
/// [`SourceStream`]: ../../kul_core/trait.SourceStream.html
/// [`StrishIterSourceStream`]: struct.StrishIterSourceStream.html
#[derive(Debug)]
pub struct CharIterSourceStream<CI, F, R>
    where CI: Iterator<Item = char>,
          F: Fn(String) -> R,
          R: RefCntStrish,
{
    pe_iter: PeekableSourceIterItemIter<CI>,
    accum: Option<(String, CharPos)>,
    // Zero-sized when our above converters (or any "function item type") are
    // used. http://doc.rust-lang.org/reference/types/function-item.html
    to_refcnt_strish: F,
}

type PeekableSourceIterItemIter<CI> = Peekable<Map<Enumerate<CI>, MapFn>>;

type MapFn = fn((usize, char)) -> SourceIterItem<CharPos>;

impl<CI, F, R> CharIterSourceStream<CI, F, R>
    where CI: Iterator<Item = char>,
          F: Fn(String) -> R,
          R: RefCntStrish,
{
    /// Given anything that can convert into an `Iterator` of `char`s, make a
    /// new `SourceStream` from it that yields its `char`s and their positions
    /// relative to the start of the iteration and that can accumulate these
    /// items to make new `Text`s from.
    ///
    /// The `to_refcnt_strish` argument must be a function/closure that converts
    /// the `String`s we will accumulate into the reference-counted string-ish,
    /// `RefCntStrish`-implementing, type we want to use with the `PosStrish`
    /// chunk values we will produce as a `SourceStream`.
    pub fn new<I>(iter: I, to_refcnt_strish: F) -> Self
        where I: IntoIterator<IntoIter = CI, Item = char>,
    {
        let map_fn: MapFn = |(pos, ch)| SourceIterItem{ch, pos: CharPos(pos)};
        Self {
            pe_iter: iter.into_iter()
                         .enumerate()
                         .map(map_fn)
                         .peekable(),
            accum: None,
            to_refcnt_strish,
        }
    }
}


/// Required by `SourceStream`.
impl<CI, F, R> Iterator for CharIterSourceStream<CI, F, R>
    where CI: Iterator<Item = char>,
          F: Fn(String) -> R,
          R: RefCntStrish,
{
    type Item = SourceIterItem<CharPos>;

    /// Note: If `next_accum` was previously called (to do an accumulation) and
    /// returned some item but `accum_done` was not called (to finish an
    /// accumulation), i.e. if we have an unfinished accumulation, this will
    /// abort and drop the unfinished accumulation.
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.accum = None;
        self.pe_iter.next()
    }
}


/// Enables `CharIterSourceStream` to be used as the input source for parsing
/// with compatible `Parser` types.
impl<CI, F, R, TT, DA> SourceStream<DA> for CharIterSourceStream<CI, F, R>
    where CI: Iterator<Item = char>,
          F: Fn(String) -> R,
          R: RefCntStrish,
          TT: Text<Pos = CharPos>,
          TT::Chunk: From<PosStrish<R>>,
          DA: DatumAllocator<TT = TT>,  // Ignored
{
    #[inline]
    fn peek(&mut self) -> Option<&<Self as Iterator>::Item> {
        self.pe_iter.peek()
    }

    fn next_accum(&mut self, _: &mut DA)
                  -> Result<Option<<Self as Iterator>::Item>, AllocError>
    {
        let next = self.pe_iter.next();
        if let Some(SourceIterItem{ch, pos}) = next {
            if let Some((s, _)) = &mut self.accum {
                // Already set, so extend
                s.push(ch);
            } else {
                // Not set yet, so set
                let mut s = String::new();
                s.push(ch);
                self.accum = Some((s, pos));
            }
        }
        Ok(next)
    }

    fn accum_done(&mut self, _: &mut DA) -> Result<TT, AllocError> {
        let ps = if let Some((s, pos)) = self.accum.take() {
            // These calls to `to_refcnt_strish` are direct (do not involve
            // function pointer indirection) when the field is a "function item
            // type".
            PosStrish::new((self.to_refcnt_strish)(s), pos)
        } else {
            PosStrish::empty()
        };
        Ok(TT::from_chunkish(ps))
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn iter() {
        let mut ciss0 = CharIterSourceStream::new("".chars(), to_rc_string);
        assert!(ciss0.next().is_none());

        let ciss1 = CharIterSourceStream::new("a".chars(), to_arc_string);
        assert_eq!(ciss1.collect::<Vec<_>>(),
                   vec![SourceIterItem{ch: 'a', pos: CharPos(0)}]);

        let ciss2 = CharIterSourceStream::new("λb".chars(), to_rc_box_str);
        assert_eq!(ciss2.collect::<Vec<_>>(),
                   vec![SourceIterItem{ch: 'λ', pos: CharPos(0)},
                        SourceIterItem{ch: 'b', pos: CharPos(1)}]);

        let s = String::from("c▷ ▷");
        let ciss3 = CharIterSourceStream::new(s.chars(), to_arc_box_str);
        assert_eq!(ciss3.collect::<Vec<_>>(),
                   vec![SourceIterItem{ch: 'c', pos: CharPos(0)},
                        SourceIterItem{ch: '▷', pos: CharPos(1)},
                        SourceIterItem{ch: ' ', pos: CharPos(2)},
                        SourceIterItem{ch: '▷', pos: CharPos(3)}]);

        let ciss4 = CharIterSourceStream::new(" 1 ".chars().chain("23".chars()),
                                              to_rc_str);
        assert_eq!(ciss4.collect::<Vec<_>>(),
                   vec![SourceIterItem{ch: ' ', pos: CharPos(0)},
                        SourceIterItem{ch: '1', pos: CharPos(1)},
                        SourceIterItem{ch: ' ', pos: CharPos(2)},
                        SourceIterItem{ch: '2', pos: CharPos(3)},
                        SourceIterItem{ch: '3', pos: CharPos(4)}]);

        let ciss5 = CharIterSourceStream::new(
            (0..4321).map(|n| if n % 2 == 0 { 'λ' } else { '-' }),
            to_arc_str);
        assert_eq!(ciss5.collect::<Vec<_>>(),
                   (0..4321)
                   .map(|n| SourceIterItem{ch: if n % 2 == 0 { 'λ' } else { '-' },
                                           pos: CharPos(n)})
                   .collect::<Vec<_>>());
    }

    #[test]
    #[allow(clippy::cyclomatic_complexity)]
    fn source_stream() {
        use std::marker::PhantomData;
        use crate::{text::TextVec, Datum, datum::DatumBox};

        struct DummyDA<R>(PhantomData<R>);
        impl<R> DatumAllocator for DummyDA<R>
            where R: RefCntStrish,
        {
            type TT = TextVec<PosStrish<R>>;
            type ET = ();
            type DR = DatumBox<Self::TT, Self::ET>;
            fn new_datum(&mut self, _from: Datum<Self::TT, Self::ET, Self::DR>)
                         -> Result<Self::DR, AllocError> {
                unreachable!()
            }
        }

        fn txt_to_chunks<R>(t: &TextVec<PosStrish<R>>) -> Vec<(&str, usize)>
            where R: RefCntStrish,
        {
            use crate::Text;
            t.iter_chunks().map(|c| (c.val.as_ref(), c.pos.0)).collect::<Vec<_>>()
        }

        let dda_rc_string: &mut DummyDA<Rc<String>> = &mut DummyDA(PhantomData);
        let dda_arc_box_str: &mut DummyDA<Arc<Box<str>>> = &mut DummyDA(PhantomData);
        let dda_rc_str: &mut DummyDA<Rc<str>> = &mut DummyDA(PhantomData);
        let dda_arc_string: &mut DummyDA<Arc<String>> = &mut DummyDA(PhantomData);

        let mut ciss0 = CharIterSourceStream::new("".chars(), to_rc_string);
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss0), None);
        assert_eq!(ciss0.next_accum(dda_rc_string), Ok(None));
        assert_eq!(ciss0.accum_done(dda_rc_string).map(|t| t.is_empty()), Ok(true));

        let mut ciss1 = CharIterSourceStream::new("Z".chars(), to_arc_box_str);
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss1),
                   Some(&SourceIterItem{ch: 'Z', pos: CharPos(0)}));
        assert_eq!(ciss1.next_accum(dda_arc_box_str),
                   Ok(Some(SourceIterItem{ch: 'Z', pos: CharPos(0)})));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss1), None);
        assert_eq!(ciss1.next_accum(dda_arc_box_str), Ok(None));
        assert_eq!(ciss1.accum_done(dda_arc_box_str).as_ref().map(txt_to_chunks),
                   Ok(vec![("Z", 0)]));

        let mut ciss2 = CharIterSourceStream::new(r"y\\x {\}}".chars(), to_rc_str);
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2),
                   Some(&SourceIterItem{ch: 'y', pos: CharPos(0)}));
        assert_eq!(ciss2.next_accum(dda_rc_str),
                   Ok(Some(SourceIterItem{ch: 'y', pos: CharPos(0)})));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2),
                   Some(&SourceIterItem{ch: '\\', pos: CharPos(1)}));
        assert_eq!(ciss2.accum_done(dda_rc_str).as_ref().map(txt_to_chunks),
                   Ok(vec![("y", 0)]));
        assert_eq!(ciss2.next(), Some(SourceIterItem{ch: '\\', pos: CharPos(1)}));
        assert_eq!(ciss2.next_accum(dda_rc_str),
                   Ok(Some(SourceIterItem{ch: '\\', pos: CharPos(2)})));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2),
                   Some(&SourceIterItem{ch: 'x', pos: CharPos(3)}));
        assert_eq!(ciss2.next_accum(dda_rc_str),
                   Ok(Some(SourceIterItem{ch: 'x', pos: CharPos(3)})));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2),
                   Some(&SourceIterItem{ch: ' ', pos: CharPos(4)}));
        assert_eq!(ciss2.next_accum(dda_rc_str),
                   Ok(Some(SourceIterItem{ch: ' ', pos: CharPos(4)})));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2),
                   Some(&SourceIterItem{ch: '{', pos: CharPos(5)}));
        assert_eq!(ciss2.accum_done(dda_rc_str).as_ref().map(txt_to_chunks),
                   Ok(vec![(r"\x ", 2)]));
        assert_eq!(ciss2.next(), Some(SourceIterItem{ch: '{', pos: CharPos(5)}));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2),
                   Some(&SourceIterItem{ch: '\\', pos: CharPos(6)}));
        assert_eq!(ciss2.accum_done(dda_rc_str).as_ref().map(txt_to_chunks),
                   Ok(vec![("", 0)]));
        assert_eq!(ciss2.next(), Some(SourceIterItem{ch: '\\', pos: CharPos(6)}));
        assert_eq!(ciss2.next_accum(dda_rc_str),
                   Ok(Some(SourceIterItem{ch: '}', pos: CharPos(7)})));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2),
                   Some(&SourceIterItem{ch: '}', pos: CharPos(8)}));
        assert_eq!(ciss2.accum_done(dda_rc_str).as_ref().map(txt_to_chunks),
                   Ok(vec![("}", 7)]));
        assert_eq!(ciss2.next(), Some(SourceIterItem{ch: '}', pos: CharPos(8)}));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2), None);
        assert_eq!(ciss2.accum_done(dda_rc_str).as_ref().map(txt_to_chunks),
                   Ok(vec![("", 0)]));
        assert_eq!(ciss2.next_accum(dda_rc_str), Ok(None));
        assert_eq!(ciss2.accum_done(dda_rc_str).as_ref().map(txt_to_chunks),
                   Ok(vec![("", 0)]));
        assert_eq!(ciss2.next(), None);
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss2), None);

        let mut ciss3 = CharIterSourceStream::new("wVu".chars(), to_arc_string);
        assert_eq!(ciss3.next_accum(dda_arc_string),
                   Ok(Some(SourceIterItem{ch: 'w', pos: CharPos(0)})));
        assert_eq!(ciss3.next_accum(dda_arc_string),
                   Ok(Some(SourceIterItem{ch: 'V', pos: CharPos(1)})));
        // next() after next_accum() loses the accumulation
        assert_eq!(ciss3.next(), Some(SourceIterItem{ch: 'u', pos: CharPos(2)}));
        assert_eq!(ciss3.accum_done(dda_arc_string).as_ref().map(txt_to_chunks),
                   Ok(vec![("", 0)]));
        assert_eq!(SourceStream::<DummyDA<_>>::peek(&mut ciss3), None);
        assert_eq!(ciss3.next_accum(dda_arc_string), Ok(None));
    }
}