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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// this module is transparently re-exported by its parent `dataset`

use std::collections::HashSet;
use std::marker::PhantomData;

use resiter::filter::*;
use resiter::map::*;

use crate::dataset::adapter::DatasetGraph;
use crate::error::*;
use crate::quad::stream::*;
use crate::quad::*;
use crate::term::matcher::*;
use crate::term::*;

use super::*;
use crate::graph::insert_if_absent;

/// Type alias for the terms returned by a dataset.
pub type DTerm<'a, D> = Term<<<D as Dataset<'a>>::Quad as Quad<'a>>::TermData>;
/// Type alias for results iterators produced by a dataset.
pub type DResult<'a, D, T> = std::result::Result<T, <D as Dataset<'a>>::Error>;
/// Type alias for fallible quad iterators produced by a dataset.
pub type DQuadSource<'a, D> = Box<Iterator<Item = DResult<'a, D, <D as Dataset<'a>>::Quad>> + 'a>;
/// Type alias for fallible hashets of terms produced by a dataset.
pub type DResultTermSet<'a, D> = DResult<'a, D, HashSet<DTerm<'a, D>>>;

/// Generic trait for RDF datasets.
///
/// For convenience, this trait is implemented
/// by [standard collections of quads](#foreign-impls).
///
/// NB: the semantics of this trait allows a dataset to contain duplicate quads;
/// see also [`SetDataset`](trait.SetDataset.html).
///
/// # How to use `Dataset` in a trait bound?
///
/// The same rules as for [`Graph`](../graph/trait.Graph.html#how-to-use-graph-in-a-trait-bound)
/// apply.
///
pub trait Dataset<'a> {
    /// The type of [`Quad`](../quad/trait.Quad.html)s
    /// that the methods of this dataset will yield.
    type Quad: Quad<'a>;
    /// The error type that this dataset may raise.
    ///
    /// Must be either [`Never`](../error/enum.Never.html) (for infallible datasets)
    /// or [`Error`](../error/struct.Error.html).
    type Error: CoercibleWith<Error> + CoercibleWith<Never>;

    /// An iterator visiting all quads of this dataset in arbitrary order.
    ///
    /// This iterator is fallible:
    /// its items are `Result`s,
    /// an error may occur at any time during the iteration.
    fn quads(&'a self) -> DQuadSource<'a, Self>;

    /// An iterator visiting all quads with the given subject.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_s<T>(&'a self, s: &'a Term<T>) -> DQuadSource<'a, Self>
    where
        T: TermData,
    {
        Box::new(self.quads().filter_ok(move |q| q.s() == s))
    }
    /// An iterator visiting all quads with the given predicate.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_p<T>(&'a self, p: &'a Term<T>) -> DQuadSource<'a, Self>
    where
        T: TermData,
    {
        Box::new(self.quads().filter_ok(move |q| q.p() == p))
    }
    /// An iterator visiting add quads with the given object.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_o<T>(&'a self, o: &'a Term<T>) -> DQuadSource<'a, Self>
    where
        T: TermData,
    {
        Box::new(self.quads().filter_ok(move |q| q.o() == o))
    }
    /// An iterator visiting add quads with the given graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_g<T>(&'a self, g: Option<&'a Term<T>>) -> DQuadSource<'a, Self>
    where
        T: TermData,
    {
        Box::new(self.quads().filter_ok(move |q| same_graph_name(q.g(), g)))
    }
    /// An iterator visiting add quads with the given subject and predicate.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_sp<T, U>(&'a self, s: &'a Term<T>, p: &'a Term<U>) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
    {
        Box::new(self.quads_with_s(s).filter_ok(move |q| q.p() == p))
    }
    /// An iterator visiting add quads with the given subject and object.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_so<T, U>(&'a self, s: &'a Term<T>, o: &'a Term<U>) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
    {
        Box::new(self.quads_with_s(s).filter_ok(move |q| q.o() == o))
    }
    /// An iterator visiting add quads with the given subject and graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_sg<T, U>(
        &'a self,
        s: &'a Term<T>,
        g: Option<&'a Term<U>>,
    ) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
    {
        Box::new(self.quads_with_g(g).filter_ok(move |q| q.s() == s))
    }
    /// An iterator visiting add quads with the given predicate and object.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_po<T, U>(&'a self, p: &'a Term<T>, o: &'a Term<U>) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
    {
        Box::new(self.quads_with_p(p).filter_ok(move |q| q.o() == o))
    }
    /// An iterator visiting add quads with the given predicate and graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_pg<T, U>(
        &'a self,
        p: &'a Term<T>,
        g: Option<&'a Term<U>>,
    ) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
    {
        Box::new(self.quads_with_g(g).filter_ok(move |q| q.p() == p))
    }
    /// An iterator visiting add quads with the given object and graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_og<T, U>(
        &'a self,
        o: &'a Term<T>,
        g: Option<&'a Term<U>>,
    ) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
    {
        Box::new(self.quads_with_g(g).filter_ok(move |q| q.o() == o))
    }
    /// An iterator visiting add quads with the given subject, predicate and object.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_spo<T, U, V>(
        &'a self,
        s: &'a Term<T>,
        p: &'a Term<U>,
        o: &'a Term<V>,
    ) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
        V: TermData,
    {
        Box::new(self.quads_with_sp(s, p).filter_ok(move |q| q.o() == o))
    }
    /// An iterator visiting add quads with the given subject, predicate and graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_spg<T, U, V>(
        &'a self,
        s: &'a Term<T>,
        p: &'a Term<U>,
        g: Option<&'a Term<V>>,
    ) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
        V: TermData,
    {
        Box::new(self.quads_with_sg(s, g).filter_ok(move |q| q.p() == p))
    }
    /// An iterator visiting add quads with the given subject, object and graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_sog<T, U, V>(
        &'a self,
        s: &'a Term<T>,
        o: &'a Term<U>,
        g: Option<&'a Term<V>>,
    ) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
        V: TermData,
    {
        Box::new(self.quads_with_sg(s, g).filter_ok(move |q| q.o() == o))
    }
    /// An iterator visiting add quads with the given predicate, object and graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_pog<T, U, V>(
        &'a self,
        p: &'a Term<T>,
        o: &'a Term<U>,
        g: Option<&'a Term<V>>,
    ) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
        V: TermData,
    {
        Box::new(self.quads_with_pg(p, g).filter_ok(move |q| q.o() == o))
    }
    /// An iterator visiting add quads with the given subject, predicate, object and graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_with_spog<T, U, V, W>(
        &'a self,
        s: &'a Term<T>,
        p: &'a Term<U>,
        o: &'a Term<V>,
        g: Option<&'a Term<W>>,
    ) -> DQuadSource<'a, Self>
    where
        T: TermData,
        U: TermData,
        V: TermData,
        W: TermData,
    {
        Box::new(self.quads_with_spg(s, p, g).filter_ok(move |q| q.o() == o))
    }

    /// Return `true` if this dataset contains the given quad.
    fn contains<T, U, V, W>(
        &'a self,
        s: &'a Term<T>,
        p: &'a Term<U>,
        o: &'a Term<V>,
        g: Option<&'a Term<W>>,
    ) -> DResult<'a, Self, bool>
    where
        T: TermData,
        U: TermData,
        V: TermData,
        W: TermData,
    {
        match self.quads_with_spog(s, p, o, g).next() {
            None => Ok(false),
            Some(Ok(_)) => Ok(true),
            Some(Err(err)) => Err(err),
        }
    }

    /// An iterator visiting add quads matching the given subject, predicate, object and graph name.
    ///
    /// See also [`quads`](#tymethod.quads).
    fn quads_matching<S, P, O, G>(
        &'a self,
        ms: &'a S,
        mp: &'a P,
        mo: &'a O,
        mg: &'a G,
    ) -> DQuadSource<'a, Self>
    where
        S: TermMatcher + ?Sized,
        P: TermMatcher + ?Sized,
        O: TermMatcher + ?Sized,
        G: GraphNameMatcher + ?Sized,
    {
        match (
            &ms.constant(),
            &mp.constant(),
            &mo.constant(),
            &mg.constant(),
        ) {
            (None, None, None, None) => Box::from(self.quads().filter_ok(move |q| {
                ms.matches(q.s()) && mp.matches(q.p()) && mo.matches(q.o()) && mg.matches(q.g())
            })),
            (Some(s), None, None, None) => {
                Box::from(self.quads_with_s(s).filter_ok(move |q| {
                    mp.matches(q.p()) && mo.matches(q.o()) && mg.matches(q.g())
                }))
            }
            (None, Some(p), None, None) => {
                Box::from(self.quads_with_p(p).filter_ok(move |q| {
                    ms.matches(q.s()) && mo.matches(q.o()) && mg.matches(q.g())
                }))
            }
            (None, None, Some(o), None) => {
                Box::from(self.quads_with_o(o).filter_ok(move |q| {
                    ms.matches(q.s()) && mp.matches(q.p()) && mg.matches(q.g())
                }))
            }
            (None, None, None, Some(g)) => {
                Box::from(self.quads_with_g(*g).filter_ok(move |q| {
                    ms.matches(q.s()) && mp.matches(q.p()) && mo.matches(q.o())
                }))
            }
            (Some(s), Some(p), None, None) => Box::from(
                self.quads_with_sp(s, p)
                    .filter_ok(move |q| mo.matches(q.o()) && mg.matches(q.g())),
            ),
            (Some(s), None, Some(o), None) => Box::from(
                self.quads_with_so(s, o)
                    .filter_ok(move |q| mp.matches(q.p()) && mg.matches(q.g())),
            ),
            (Some(s), None, None, Some(g)) => Box::from(
                self.quads_with_sg(s, *g)
                    .filter_ok(move |q| mp.matches(q.p()) && mo.matches(q.o())),
            ),
            (None, Some(p), Some(o), None) => Box::from(
                self.quads_with_po(p, o)
                    .filter_ok(move |q| ms.matches(q.s()) && mg.matches(q.g())),
            ),
            (None, Some(p), None, Some(g)) => Box::from(
                self.quads_with_pg(p, *g)
                    .filter_ok(move |q| ms.matches(q.s()) && mo.matches(q.o())),
            ),
            (None, None, Some(o), Some(g)) => Box::from(
                self.quads_with_og(o, *g)
                    .filter_ok(move |q| ms.matches(q.s()) && mp.matches(q.p())),
            ),
            (Some(s), Some(p), Some(o), None) => Box::from(
                self.quads_with_spo(s, p, o)
                    .filter_ok(move |q| mg.matches(q.g())),
            ),
            (Some(s), Some(p), None, Some(g)) => Box::from(
                self.quads_with_spg(s, p, *g)
                    .filter_ok(move |q| mo.matches(q.o())),
            ),
            (Some(s), None, Some(o), Some(g)) => Box::from(
                self.quads_with_sog(s, o, *g)
                    .filter_ok(move |q| mp.matches(q.p())),
            ),
            (None, Some(p), Some(o), Some(g)) => Box::from(
                self.quads_with_pog(p, o, *g)
                    .filter_ok(move |q| ms.matches(q.s())),
            ),
            (Some(s), Some(p), Some(o), Some(g)) => self.quads_with_spog(s, p, o, *g),
        }
    }

    /// Build a Hashset of all the terms used as subject in this Dataset.
    fn subjects(&'a self) -> DResultTermSet<'a, Self> {
        let mut res = std::collections::HashSet::new();
        for q in self.quads() {
            insert_if_absent(&mut res, q?.s());
        }
        Ok(res)
    }

    /// Build a Hashset of all the terms used as predicate in this Dataset.
    fn predicates(&'a self) -> DResultTermSet<'a, Self> {
        let mut res = std::collections::HashSet::new();
        for q in self.quads() {
            insert_if_absent(&mut res, q?.p());
        }
        Ok(res)
    }

    /// Build a Hashset of all the terms used as object in this Dataset.
    fn objects(&'a self) -> DResultTermSet<'a, Self> {
        let mut res = std::collections::HashSet::new();
        for q in self.quads() {
            insert_if_absent(&mut res, q?.o());
        }
        Ok(res)
    }

    /// Build a Hashset of all the terms used as graph names in this Dataset.
    fn graph_names(&'a self) -> DResultTermSet<'a, Self> {
        let mut res = std::collections::HashSet::new();
        for q in self.quads() {
            let q = q?;
            let name = q.g();
            if let Some(name) = name {
                insert_if_absent(&mut res, name);
            }
        }
        Ok(res)
    }

    /// Build a Hashset of all the IRIs used in this Dataset.
    fn iris(&'a self) -> DResultTermSet<'a, Self> {
        let mut res = std::collections::HashSet::new();
        for q in self.quads() {
            let q = q?;
            let (s, p, o) = (q.s(), q.p(), q.o());
            if let Iri(_) = s {
                insert_if_absent(&mut res, s)
            }
            if let Iri(_) = p {
                insert_if_absent(&mut res, p)
            }
            if let Iri(_) = o {
                insert_if_absent(&mut res, o)
            }
            if let Some(gn) = q.g() {
                if let Iri(_) = gn {
                    insert_if_absent(&mut res, &gn)
                }
            }
        }
        Ok(res)
    }

    /// Build a Hashset of all the BNodes used in this Dataset.
    fn bnodes(&'a self) -> DResultTermSet<'a, Self> {
        let mut res = std::collections::HashSet::new();
        for q in self.quads() {
            let q = q?;
            let (s, p, o) = (q.s(), q.p(), q.o());
            if let BNode(_) = s {
                insert_if_absent(&mut res, s)
            }
            if let BNode(_) = p {
                insert_if_absent(&mut res, p)
            }
            if let BNode(_) = o {
                insert_if_absent(&mut res, o)
            }
            if let Some(gn) = q.g() {
                if let BNode(_) = gn {
                    insert_if_absent(&mut res, &gn)
                }
            }
        }
        Ok(res)
    }

    /// Build a Hashset of all the Literals used in this Dataset.
    fn literals(&'a self) -> DResultTermSet<'a, Self> {
        let mut res = std::collections::HashSet::new();
        for q in self.quads() {
            let q = q?;
            let (s, p, o) = (q.s(), q.p(), q.o());
            if let Literal(_, _) = s {
                insert_if_absent(&mut res, s)
            }
            if let Literal(_, _) = p {
                insert_if_absent(&mut res, p)
            }
            if let Literal(_, _) = o {
                insert_if_absent(&mut res, o)
            }
            if let Some(gn) = q.g() {
                if let Literal(_, _) = gn {
                    insert_if_absent(&mut res, &gn)
                }
            }
        }
        Ok(res)
    }

    /// Build a Hashset of all the variables used in this Dataset.
    fn variables(&'a self) -> DResultTermSet<'a, Self> {
        let mut res = std::collections::HashSet::new();
        for q in self.quads() {
            let q = q?;
            let (s, p, o) = (q.s(), q.p(), q.o());
            if let Variable(_) = s {
                insert_if_absent(&mut res, s)
            }
            if let Variable(_) = p {
                insert_if_absent(&mut res, p)
            }
            if let Variable(_) = o {
                insert_if_absent(&mut res, o)
            }
            if let Some(gn) = q.g() {
                if let Variable(_) = gn {
                    insert_if_absent(&mut res, &gn)
                }
            }
        }
        Ok(res)
    }

    /// Borrows one of the graphs of this dataset
    fn graph<T>(&self, graph_name: Option<&Term<T>>) -> DatasetGraph<Self, &Self, Option<BoxTerm>>
    where
        T: TermData,
    {
        DatasetGraph {
            dataset: self,
            gmatcher: graph_name.map(|n| n.into()),
            _phantom: PhantomData,
        }
    }

    /// Borrows mutably one of the graphs of this dataset
    fn graph_mut<T>(
        &mut self,
        graph_name: Option<&Term<T>>,
    ) -> DatasetGraph<Self, &mut Self, Option<BoxTerm>>
    where
        T: TermData,
    {
        DatasetGraph {
            dataset: self,
            gmatcher: graph_name.map(|n| n.into()),
            _phantom: PhantomData,
        }
    }

    /// Borrows a graph containing the union of all graphs matched by `gmatcher`
    fn union_graph<T>(&'a self, gmatcher: T) -> DatasetGraph<Self, &Self, T>
    where
        T: GraphNameMatcher,
    {
        DatasetGraph {
            dataset: self,
            gmatcher,
            _phantom: PhantomData,
        }
    }
}

/// Type alias for results produced by a mutable dataset.
pub type MDResult<D, T> = std::result::Result<T, <D as MutableDataset>::MutationError>;

/// Generic trait for mutable RDF datasets.
///
/// NB: the semantics of this trait allows a dataset to contain duplicate quads;
/// see also [`SetDataset`](trait.SetDataset.html).
///
pub trait MutableDataset: for<'x> Dataset<'x> {
    /// The error type that this dataset may raise during mutations.
    ///
    /// Must be either [`Never`](../error/enum.Never.html) (for infallible datasets)
    /// or [`Error`](../error/struct.Error.html).
    type MutationError: CoercibleWith<Error> + CoercibleWith<Never>;

    /// Insert the given quad in this dataset.
    ///
    /// Return `true` iff the quad was actually inserted.
    ///
    /// NB: unless this dataset also implements [`SetDataset`](trait.SetDataset.html),
    /// a return value of `true` does *not* mean that the quad was not already in the dataset,
    /// only that the dataset now has one more occurence of it.
    ///
    fn insert<T, U, V, W>(
        &mut self,
        s: &Term<T>,
        p: &Term<U>,
        o: &Term<V>,
        g: Option<&Term<W>>,
    ) -> MDResult<Self, bool>
    where
        T: TermData,
        U: TermData,
        V: TermData,
        W: TermData;

    /// Remove the given quad in this dataset.
    ///
    /// Return `true` iff the quad was actually removed.
    ///
    /// NB: unless this dataset also implements [`SetDataset`](trait.SetDataset.html),
    /// a return value of `true` does *not* mean that the quad is not still contained in the dataset,
    /// only that the dataset now has one less occurence of it.
    ///
    fn remove<T, U, V, W>(
        &mut self,
        s: &Term<T>,
        p: &Term<U>,
        o: &Term<V>,
        g: Option<&Term<W>>,
    ) -> MDResult<Self, bool>
    where
        T: TermData,
        U: TermData,
        V: TermData,
        W: TermData;

    /// Return a [`QuadSink`](../quad/stream/trait.QuadSink.html)
    /// that will insert into this dataset all the quads it receives.
    #[inline]
    fn inserter(&mut self) -> Inserter<Self> {
        Inserter::new(self)
    }

    /// Insert into this dataset all quads from the given source.
    #[inline]
    fn insert_all<'a, TS>(
        &mut self,
        src: &mut TS,
    ) -> CoercedResult<usize, TS::Error, <Self as MutableDataset>::MutationError>
    where
        TS: QuadSource<'a>,
        TS::Error: CoercibleWith<<Self as MutableDataset>::MutationError>,
    {
        src.in_sink(&mut self.inserter())
    }

    /// Return a [`QuadSink`](../quad/stream/trait.QuadSink.html)
    /// that will remove from this dataset all the quads it receives.
    #[inline]
    fn remover(&mut self) -> Remover<Self> {
        Remover::new(self)
    }

    /// Remove from this dataset all quads from the given source.
    #[inline]
    fn remove_all<'a, TS>(
        &mut self,
        src: &mut TS,
    ) -> CoercedResult<usize, TS::Error, <Self as MutableDataset>::MutationError>
    where
        TS: QuadSource<'a>,
        TS::Error: CoercibleWith<<Self as MutableDataset>::MutationError>,
    {
        src.in_sink(&mut self.remover())
    }

    /// Remove all quads matching the given matchers.
    ///
    /// Note that the default implementation is rather naive,
    /// and could be improved in specific implementations of the trait.
    fn remove_matching<S, P, O, G>(
        &mut self,
        ms: &S,
        mp: &P,
        mo: &O,
        mg: &G,
    ) -> MDResult<Self, usize>
    where
        S: TermMatcher + ?Sized,
        P: TermMatcher + ?Sized,
        O: TermMatcher + ?Sized,
        G: GraphNameMatcher + ?Sized,
        // The following trait bound means that Self::Error must convert to Self::MutationError;
        // it is always satisfied when both of them are either Error or Never;
        // it is required to raise an error when building to_remove
        for<'a> <Self as Dataset<'a>>::Error: Into<Self::MutationError>,
        // The following trait bound is required by remove_all,
        // who coerces to_remove::Error (always Never) with self::MutationError.
        Never: CoercibleWith<Self::MutationError>,
        // The following trait is trivially verified in all cases,
        // (acatually T is always EQUAL to CoercedError<Never, T>)
        // but unfortunetaly the compiler can not see that.
        Self::MutationError: From<CoercedError<Never, Self::MutationError>>,
    {
        let to_remove: Vec<_> = self
            .quads_matching(ms, mp, mo, mg)
            .map_ok(|q| {
                (
                    [
                        BoxTerm::from(q.s()),
                        BoxTerm::from(q.p()),
                        BoxTerm::from(q.o()),
                    ],
                    q.g().map(BoxTerm::from),
                )
            })
            .collect::<std::result::Result<_, _>>()
            .map_err(Into::into)?;
        let mut to_remove = to_remove.into_iter().as_quad_source();
        Ok(self.remove_all(&mut to_remove)?)
    }

    /// Keep only the quads matching the given matchers.
    ///
    /// Note that the default implementation is rather naive,
    /// and could be improved in specific implementations of the trait.
    ///
    fn retain<S, P, O, G>(&mut self, ms: &S, mp: &P, mo: &O, mg: &G) -> MDResult<Self, ()>
    where
        S: TermMatcher + ?Sized,
        P: TermMatcher + ?Sized,
        O: TermMatcher + ?Sized,
        G: GraphNameMatcher + ?Sized,
        // The following trait bound means that Self::Error must convert to Self::MutationError;
        // it is always satisfied when both of them are either Error or Never;
        // it is required to raise an error when building to_remove
        for<'a> <Self as Dataset<'a>>::Error: Into<Self::MutationError>,
        // The following trait bound is required by remove_all,
        // who coerces to_remove::Error (always Never) with self::MutationError.
        Never: CoercibleWith<Self::MutationError>,
        // The following trait is trivially verified in all cases,
        // (acatually T is always EQUAL to CoercedError<Never, T>)
        // but unfortunetaly the compiler can not see that.
        Self::MutationError: From<CoercedError<Never, Self::MutationError>>,
    {
        let to_remove: Vec<_> = self
            .quads()
            .filter_ok(|q| {
                !(ms.matches(q.s()) && mp.matches(q.p()) && mo.matches(q.o()) && mg.matches(q.g()))
            })
            .map_ok(|q| {
                (
                    [
                        BoxTerm::from(q.s()),
                        BoxTerm::from(q.p()),
                        BoxTerm::from(q.o()),
                    ],
                    q.g().map(BoxTerm::from),
                )
            })
            .collect::<std::result::Result<_, _>>()
            .map_err(Into::into)?;
        let mut to_remove = to_remove.into_iter().as_quad_source();
        self.remove_all(&mut to_remove)?;
        Ok(())
    }
}

/// Marker trait constraining the semantics of
/// [`Dataset`](trait.Dataset.html) and [`MutableDataset`](trait.MutableDataset.html),
/// by guaranteeing that quads will never be returned / stored multiple times.
pub trait SetDataset {}

#[cfg(test)]
mod test {
    // The code from this module is tested through its use in other modules
    // (especially the macro test_dataset_impl!).
}