vectorscan-async 0.0.4

Wrapper for the vectorscan C++ regex library.
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
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
/* Copyright 2022-2024 Danny McClanahan */
/* SPDX-License-Identifier: BSD-3-Clause */

//! Higher-level wrappers to manage state needed for stream parsing.
//!
//! # Stream Parsing
//! "Stream parsing", which in this case refers simply to "matching a pattern
//! against a non-contiguous data stream" (like a pipe), is surprisingly poorly
//! supported by most regex engines as well as higher-level parsers. Most of the
//! time, this is the correct tradeoff to make, as many more optimizations are
//! available for contiguous string search, and in practice many
//! non-contiguous streams can be "tokenized" beforehand e.g. by splitting them
//! into lines, or some other sentinel character known not to appear in any of
//! the input patterns such as a null byte.
//!
//! ## Stream Parsing for Correctness
//! However, it should also be noted that these heuristic methods for
//! tokenization may suddenly break, especially if the application is receiving
//! uncontrolled user input (and therefore can't rely on identifying sentinel
//! characters). Additionally, as discussed in [Advantages], many other regex
//! engines simply find it much more difficult to support interruptible use
//! cases like stream parsing, because they return a single synchronous result
//! instead of using vectorscan's coroutine-like match callback API. This is
//! also why vectorscan is more easily able to report *overlapping matches*,
//! which also improves correctness in that longest-match semantics don't have
//! to be explicitly opted out of.
//!
//! [Advantages]: crate::matchers#advantages
//!
//! ## Stream Parsing for Performance
//! *TODO: citation for this entire paragraph!*
//! Intel primarily designed the hyperscan library's stream parsing
//! functionality for extremely low-latency network traffic analysis. This may
//! involve:
//! - quite large amounts of data, such that tokenizing the data into lines or
//!   generally copying/moving it at all introduces an unacceptable level of
//!   latency (this is why even the smallest `SOM_LEFTMOST` horizon size
//!   [`Mode::SOM_HORIZON_SMALL`] still stores start-of-match offsets within the
//!   last 2^16 bytes!),
//! - received over the network in unpredictable bursts,
//! - in which the data stream cannot be reordered or split up,
//! - with patterns that are often expected to span multiple separate
//!   non-contiguous chunks of data in order to match,
//! - where it's often more important to be able to know *what pattern* was
//!   matched (e.g. to classify input packets) than to know the *precise input
//!   string* that matched it.
//!
//! [`Mode::SOM_HORIZON_SMALL`]: crate::flags::Mode::SOM_HORIZON_SMALL
//!
//! These unusual constraints led Intel to invest in stream parsing, but this
//! author would love to see it supported by more regex engines and parsing
//! frameworks in the future, because **correctly matching patterns that span
//! non-contiguous input is really difficult to solve outside of the regex
//! engine**!
//!
//! ## Workarounds to Reference Streamed Data
//! Because streaming matches may span any number of individual contiguous
//! inputs, neither the vectorscan library nor this crate make any
//! attempt to preserve any reference to the original string data, as part
//! of a "zero-copy" interface. However, this means that the
//! application performing stream parsing can tee the stream input
//! elsewhere while feeding it to vectorscan (perhaps in a compacted form),
//! and only needs to pull out that lookup mechanism if needed to perform
//! further processing on a match that depends on the match's string
//! contents. When performing this strategy, enabling
//! [`Flags::SOM_LEFTMOST`] (as it is by default) is recommended to reduce the
//! cost of reconstructing the match string, at the cost of
//! slightly lower scan performance.
//!
//! [`Flags::SOM_LEFTMOST`]: crate::flags::Flags::SOM_LEFTMOST
//!
//! ### Doing Work at Compile Time to Minimize Need for Stream Data
//! However, before looking into complex mechanisms to keep data around so
//! it can be queried later, keep in mind that the vectorscan library
//! makes a great deal of effort to support complex search queries at
//! "compile time" using features like [`ExpressionSet::with_ids()`] to
//! identify matched sub-patterns, or [`Flags::COMBINATION`] to generate
//! complex acceptance criteria for matching a pattern. If possible, a scan
//! should be performed so that only tracking matches for
//! [`StreamMatch::id`] is necessary, with
//! [`StreamMatch::range`] being used at most to *order* the matches instead of
//! to look up their data.
//!
//! [`ExpressionSet::with_ids()`]: crate::expression::ExpressionSet::with_ids
//! [`Flags::COMBINATION`]: crate::flags::Flags::COMBINATION
//! [`StreamMatch::id`]: crate::matchers::StreamMatch::id
//! [`StreamMatch::range`]: crate::matchers::StreamMatch::range
//!
//! ## Performance Considerations
//! Stream parsing [disables several search optimizations], so even the
//! Intel documentation recommends using [`Mode::BLOCK`] (which returns
//! [`Match`] in this crate) where possible for best performance.
//!
//! [Advantages]: crate::matchers#advantages
//! [disables several search optimizations]: https://intel.github.io/hyperscan/dev-reference/performance.html#block-based-matching
//! [`Mode::BLOCK`]: crate::flags::Mode::BLOCK
//! [`Match`]: crate::matchers::Match
//!
//! # This Module
//! While creating a [`LiveStream`] is really the only part truly required to
//! perform stream searching with [`Scratch::scan_sync_stream()`], most of this
//! module is instead taken up by wrapper structs which combine the
//! [`LiveStream`] object itself with [`StreamMatcher`] and [`Scratch`]
//! instances, making use of [`handles`] to select different lazy vs eager
//! cloning techniques. This decoupling allows [`ScratchStreamSink::scan()`] to
//! provide the nice and easy API we're used to from other regex engines:
//!
//!```
//! #[cfg(feature = "compiler")]
//! fn main() -> Result<(), vectorscan::error::VectorscanError> {
//!   use vectorscan::{expression::*, flags::*, stream::*, state::*, matchers::*};
//!   use std::ops::Range;
//!
//!   let expr: Expression = "a+".parse()?;
//!   let block_db = expr.compile(Flags::SOM_LEFTMOST, Mode::BLOCK)?;
//!   let stream_db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
//!   let mut scratch = Scratch::blank();
//!   scratch.setup_for_db(&block_db)?;
//!   scratch.setup_for_db(&stream_db)?;
//!   let live = stream_db.allocate_stream()?;
//!
//!   // With the non-streaming API, we only need to provide the scratch space and db:
//!   let mut matches: Vec<&str> = Vec::new();
//!   scratch
//!     .scan_sync(&block_db, "aardvarka".into(), |m| {
//!       matches.push(unsafe { m.source.as_str() });
//!       MatchResult::Continue
//!     })?;
//!   assert_eq!(&matches, &["a", "aa", "a", "a"]);
//!
//!   // With the streaming API, we need a little more setup:
//!   // Create the `matches` vector which is mutably captured in the dyn closure.
//!   let mut matches: Vec<StreamMatch> = Vec::new();
//!   // Capture `matches` into `match_fn`;
//!   // in this case, `match_fn` is an unboxed stack-allocated closure.
//!   let mut match_fn = |m| {
//!     matches.push(m);
//!     MatchResult::Continue
//!   };
//!   // Create a scope to mutably borrow `match_fn` in:
//!   {
//!     // `matcher` now keeps the reference to `matches` alive
//!     // in rustc's local lifetime tracking via `match_fn`.
//!     let matcher = StreamMatcher::new(&mut match_fn);
//!     let mut sink = ScratchStreamSink::new(live, matcher, scratch);
//!
//!     // After all this setup, now we have our nice fluent API!
//!     sink.scan("aardvarka".into())?;
//!     sink.scan("a".into())?;
//!     // Streams must explicitly mark the end of data.
//!     sink.flush_eod()?;
//!   }
//!   // This will also drop `matcher`, which means `match_fn`
//!   // holds the only reference to `matches`.
//!   // We could also have used `mem::drop()` explicitly.
//!
//!   // Since `match_fn` is otherwise unused outside of `matcher`,
//!   // rustc can statically determine that no other mutable reference
//!   // to `matches` exists, so it "unlocks" the value
//!   // and lets us consume it with `.into_iter()`.
//!   let matches: Vec<Range<usize>> = matches
//!     .into_iter()
//!     .map(|m| m.range.into())
//!     .collect();
//!   // The `8..10` match crossed our two non-contiguous inputs!
//!   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 8..10]);
//!   Ok(())
//! }
//! # #[cfg(not(feature = "compiler"))]
//! # fn main() {}
//! ```

#[cfg(feature = "vectored")]
use crate::sources::VectoredByteSlices;
use crate::{
  database::Database,
  error::{CompressionError, VectorscanRuntimeError},
  hs,
  matchers::stream::StreamMatcher,
  sources::ByteSlice,
  state::Scratch,
};

use handles::{Handle, Resource};

use std::{ops, ptr};

/// Pointer type for stream allocations used in [`LiveStream#Managing
/// Allocations`](LiveStream#managing-allocations).
pub type NativeStream = hs::hs_stream;

/// Stateful stream object initialized against some [`Database`].
///
/// While this type can be provided directly to methods such as
/// [`Scratch::scan_sync_stream()`], the other structs in this module wrap it in
/// a type-erased [`Handle`] as a way to swap out whether lazy or eager cloning
/// strategies are used.
#[derive(Debug)]
#[repr(transparent)]
pub struct LiveStream(*mut NativeStream);

unsafe impl Send for LiveStream {}

/// # Stream Operations
/// After creation, the stream can be written to, but doing so
/// requires providing an additional match callback, which is the job of structs
/// like [`ScratchStreamSink`]. Instead, this struct provides [`Self::reset()`]
/// and [`Self::compress()`] to modify or serialize the instantaneous stream
/// state, neither of which require invoking a match callback.
impl LiveStream {
  /// Initialize a new live stream object into a newly allocated memory region.
  ///
  /// The stream will be set to the initial automaton state, with match offsets
  /// starting at 0.
  pub fn open(db: &Database) -> Result<Self, VectorscanRuntimeError> {
    let mut ret = ptr::null_mut();
    VectorscanRuntimeError::from_native(unsafe {
      hs::hs_open_stream(
        db.as_ref_native(),
        /* NB: ignoring flags for now! */
        0,
        &mut ret,
      )
    })?;
    Ok(unsafe { Self::from_native(ret) })
  }

  /// Reset this stream's automaton state to the initial state, and restart
  /// match offsets at 0.
  pub fn reset(&mut self) -> Result<(), VectorscanRuntimeError> {
    VectorscanRuntimeError::from_native(unsafe { hs::hs_direct_reset_stream(self.as_mut_native()) })
  }

  /// Write the stream's current state into a buffer according to the `into`
  /// strategy using [`CompressedStream::compress()`].
  ///
  /// The stream can later be deserialized from this state into a working
  /// in-memory stream again with methods such as
  /// [`CompressedStream::expand()`].
  pub fn compress(
    &self,
    into: CompressReserveBehavior,
  ) -> Result<CompressedStream, CompressionError> {
    CompressedStream::compress(into, self)
  }
}

/// # Managing Allocations
/// These methods provide access to the underlying memory allocation containing
/// the data for the in-memory stream. They can be used along with
/// [`CompressedStream::expand_into_at()`] to control the memory
/// location used for the stream, or to preserve stream allocations across
/// weird lifetime constraints.
///
/// Note that [`Database::stream_size()`] can be used to determine the size of
/// the memory allocation pointed to by [`Self::as_ref_native()`] and
/// [`Self::as_mut_native()`], but (**FIXME?**) there is currently no method
/// provided by the vectorscan library to get the stream's allocation size from
/// the stream object itself.
impl LiveStream {
  /// Wrap the provided allocation `p`.
  ///
  /// # Safety
  /// The pointer `p` must point to an initialized db allocation prepared by
  /// [`Self::open()`] or [`CompressedStream::expand_into_at()`]!
  ///
  /// This method also makes it especially easy to create multiple references to
  /// the same allocation, which will then cause a double free when
  /// [`Self::try_drop()`] is called more than once for the same db allocation.
  /// To avoid that issue, you can wrap the result in a
  /// [`ManuallyDrop`](std::mem::ManuallyDrop); but
  /// unlike [`Database::from_native()`], a stream is a mutable object, so
  /// multiple copies of it will break Rust's ownership rules:
  ///
  ///```
  /// #[cfg(feature = "compiler")]
  /// fn main() -> Result<(), vectorscan::error::VectorscanError> {
  ///   use vectorscan::{expression::*, flags::*, matchers::*, stream::*};
  ///   use std::{mem::ManuallyDrop, ops::Range};
  ///
  ///   // Compile a legitimate stream:
  ///   let expr: Expression = "a+".parse()?;
  ///   let db = expr.compile(
  ///     Flags::SOM_LEFTMOST,
  ///     Mode::STREAM | Mode::SOM_HORIZON_LARGE
  ///   )?;
  ///   let mut scratch = db.allocate_scratch()?;
  ///   let mut stream = db.allocate_stream()?;
  ///
  ///   // Create two new references to that allocation,
  ///   // wrapped to avoid calling the drop code:
  ///   let stream_ptr: *mut NativeStream = stream.as_mut_native();
  ///   let mut stream_ref_1 = ManuallyDrop::new(unsafe { LiveStream::from_native(stream_ptr) });
  ///   let mut stream_ref_2 = ManuallyDrop::new(unsafe { LiveStream::from_native(stream_ptr) });
  ///
  ///   // Both stream references are valid and can be used for matching.
  ///
  ///   let mut matches: Vec<Range<usize>> = Vec::new();
  ///   let mut match_fn = |StreamMatch { range, .. }| {
  ///     matches.push(range.into());
  ///     MatchResult::Continue
  ///   };
  ///   let mut matcher = StreamMatcher::new(&mut match_fn);
  ///   scratch
  ///     .scan_sync_stream(&mut stream_ref_1, &mut matcher, "aardvarka".into())?;
  ///   scratch
  ///     .scan_sync_stream(&mut stream_ref_2, &mut matcher, "aardvarka".into())?;
  ///   // The 8..11 demonstrates that this was actually the same mutable stream!
  ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 8..10, 8..11, 14..15, 17..18]);
  ///   Ok(())
  /// }
  /// # #[cfg(not(feature = "compiler"))]
  /// # fn main() {}
  /// ```
  pub const unsafe fn from_native(p: *mut NativeStream) -> Self { Self(p) }

  /// Get a read-only reference to the stream allocation.
  ///
  /// This method is mostly used internally and cast to a pointer to provide to
  /// the vectorscan native library methods.
  pub fn as_ref_native(&self) -> &NativeStream { unsafe { &*self.0 } }

  /// Get a mutable reference to the stream allocation.
  ///
  /// The result of this method can be cast to a pointer and provided to
  /// [`Self::from_native()`].
  pub fn as_mut_native(&mut self) -> &mut NativeStream { unsafe { &mut *self.0 } }

  /// Generate a new stream in a newly allocated memory region which matches the
  /// same db.
  ///
  /// The stream will be set to the initial automaton state.
  pub fn try_clone(&self) -> Result<Self, VectorscanRuntimeError> {
    let mut ret = ptr::null_mut();
    VectorscanRuntimeError::from_native(unsafe {
      hs::hs_copy_stream(&mut ret, self.as_ref_native())
    })?;
    Ok(unsafe { Self::from_native(ret) })
  }

  /// Reset the stream state to the same as that of `source`.
  ///
  /// # Safety
  /// `self` and `source` must have been originally opened against the same db
  /// (meaning the same compiled database, not necessarily the same db
  /// allocation)!
  pub unsafe fn try_clone_from(&mut self, source: &Self) -> Result<(), VectorscanRuntimeError> {
    VectorscanRuntimeError::from_native(unsafe {
      hs::hs_direct_reset_and_copy_stream(self.as_mut_native(), source.as_ref_native())
    })
  }

  /// Free the underlying stream allocation.
  ///
  /// # Safety
  /// This method must be called at most once over the lifetime of each stream
  /// allocation. It is called by default on drop, so
  /// [`ManuallyDrop`](std::mem::ManuallyDrop) is recommended to wrap instances
  /// that reference external data in order to avoid attempting to free the
  /// referenced data.
  ///
  /// ## Only Frees Memory
  /// This method performs no processing other than freeing the allocated
  /// memory, so it can be skipped without leaking resources if the
  /// underlying [`NativeStream`] allocation is freed by some other means.
  pub unsafe fn try_drop(&mut self) -> Result<(), VectorscanRuntimeError> {
    VectorscanRuntimeError::from_native(unsafe { hs::hs_direct_free_stream(self.as_mut_native()) })
  }
}

/// NB: [`Clone::clone_from()`] is not implemented because
/// [`Self::try_clone_from()`] is unsafe!
impl Clone for LiveStream {
  fn clone(&self) -> Self { self.try_clone().unwrap() }
}

impl Resource for LiveStream {
  type Error = VectorscanRuntimeError;

  fn deep_clone(&self) -> Result<Self, Self::Error> { self.try_clone() }

  fn deep_boxed_clone(&self) -> Result<Box<dyn Resource<Error=Self::Error>>, Self::Error> {
    Ok(Box::new(self.try_clone()?))
  }

  unsafe fn sync_drop(&mut self) -> Result<(), Self::Error> { self.try_drop() }
}

impl ops::Drop for LiveStream {
  fn drop(&mut self) {
    unsafe {
      self.try_drop().unwrap();
    }
  }
}

/// A wrapper around all the state needed to execute a stream search.
///
/// By holding handles to [`Self::live`] and [`Self::scratch`], the stream
/// scanning API can be made quite fluent, without as many parameters per call:
///
///```
/// #[cfg(feature = "compiler")]
/// fn main() -> Result<(), vectorscan::error::VectorscanError> {
///   use vectorscan::{expression::*, flags::*, stream::*, matchers::*};
///   use std::{ops::Range, mem};
///
///   let expr: Expression = "a+".parse()?;
///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
///   let scratch = db.allocate_scratch()?;
///   let live = db.allocate_stream()?;
///
///   // Create the `matches` vector which is mutably captured in the dyn closure.
///   let mut matches: Vec<StreamMatch> = Vec::new();
///   // Capture `matches` into `match_fn`;
///   // in this case, `match_fn` is an unboxed stack-allocated closure.
///   let mut match_fn = |m| {
///     matches.push(m);
///     MatchResult::Continue
///   };
///   // `matcher` now keeps the reference to `matches` alive
///   // in rustc's local lifetime tracking.
///   let matcher = StreamMatcher::new(&mut match_fn);
///   let mut sink = ScratchStreamSink::new(live, matcher, scratch);
///
///   sink.scan("aardvark".into())?;
///   sink.flush_eod()?;
///
///   // This will also drop `matcher`, which means `match_fn`
///   // holds the only reference to `matches`.
///   mem::drop(sink);
///   // This could also be performed by explicitly
///   // introducing a scope with `{}`.
///
///   // Since `match_fn` is otherwise unused outside of `matcher`,
///   // rustc can statically determine that no other mutable reference
///   // to `matches` exists, so it "unlocks" the value
///   // and lets us consume it with `.into_iter()`.
///   let matches: Vec<Range<usize>> = matches
///     .into_iter()
///     .map(|m| m.range.into())
///     .collect();
///   assert_eq!(&matches, &[0..1, 0..2, 5..6]);
///   Ok(())
/// }
/// # #[cfg(not(feature = "compiler"))]
/// # fn main() {}
/// ```
pub struct ScratchStreamSink<'code> {
  /// Cloneable handle to a stateful stream.
  pub live: Box<dyn Handle<R=LiveStream>>,
  /// Type-erased wrapper over the user-provided match callback.
  pub matcher: StreamMatcher<'code>,
  /// Cloneable handle to a scratch space initialized for the same db as
  /// [`Self::live`].
  pub scratch: Box<dyn Handle<R=Scratch>>,
}

impl<'code> ScratchStreamSink<'code> {
  /// Collate all the state necessary to match against a stream.
  pub fn new(
    live: impl Handle<R=LiveStream>,
    matcher: StreamMatcher<'code>,
    scratch: impl Handle<R=Scratch>,
  ) -> Self {
    Self {
      live: Box::new(live),
      matcher,
      scratch: Box::new(scratch),
    }
  }

  /// Write a single contiguous string into the automaton.
  ///
  ///```
  /// #[cfg(feature = "compiler")]
  /// fn main() -> Result<(), vectorscan::error::VectorscanError> {
  ///   use vectorscan::{expression::*, flags::*, stream::*, matchers::*};
  ///   use std::ops::Range;
  ///
  ///   let expr: Expression = "a+".parse()?;
  ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
  ///   let scratch = db.allocate_scratch()?;
  ///   let live = db.allocate_stream()?;
  ///
  ///   // Create the `matches` vector which is mutably captured in the dyn closure.
  ///   let mut matches: Vec<StreamMatch> = Vec::new();
  ///   // Capture `matches` into `match_fn`;
  ///   // in this case, `match_fn` is an unboxed stack-allocated closure.
  ///   let mut match_fn = |m| {
  ///     matches.push(m);
  ///     MatchResult::Continue
  ///   };
  ///
  ///   {
  ///     // `matcher` now keeps the reference to `matches` alive
  ///     // in rustc's local lifetime tracking.
  ///     let matcher = StreamMatcher::new(&mut match_fn);
  ///     let mut sink = ScratchStreamSink::new(live, matcher, scratch);
  ///
  ///     sink.scan("aardvarka".into())?;
  ///     sink.scan("a".into())?;
  ///     sink.flush_eod()?;
  ///   }
  ///   // `matches` is now "unlocked" by rustc after `matcher` was dropped!
  ///   let matches: Vec<Range<usize>> = matches
  ///     .into_iter()
  ///     .map(|m| m.range.into())
  ///     .collect();
  ///   // 8..10 is across a non-contiguous input boundary!
  ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 8..10]);
  ///   Ok(())
  /// }
  /// # #[cfg(not(feature = "compiler"))]
  /// # fn main() {}
  /// ```
  #[allow(clippy::needless_lifetimes)]
  pub fn scan<'data>(&mut self, data: ByteSlice<'data>) -> Result<(), VectorscanRuntimeError> {
    let Self {
      live,
      matcher,
      scratch,
    } = self;
    scratch
      .make_mut()?
      .scan_sync_stream(live.make_mut()?, matcher, data)
  }

  /// Write vectored string data into the automaton.
  ///
  ///```
  /// #[cfg(feature = "compiler")]
  /// fn main() -> Result<(), vectorscan::error::VectorscanError> {
  ///   use vectorscan::{expression::*, flags::*, stream::*, matchers::*, sources::*};
  ///   use std::ops::Range;
  ///
  ///   let expr: Expression = "a+".parse()?;
  ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
  ///   let scratch = db.allocate_scratch()?;
  ///   let live = db.allocate_stream()?;
  ///
  ///   let input: [ByteSlice; 2] = [
  ///     "aardvarka".into(),
  ///     "asdf".into(),
  ///   ];
  ///
  ///   // Create the `matches` vector which is mutably captured in the dyn closure.
  ///   let mut matches: Vec<StreamMatch> = Vec::new();
  ///   // Capture `matches` into `match_fn`;
  ///   // in this case, `match_fn` is an unboxed stack-allocated closure.
  ///   let mut match_fn = |m| {
  ///     matches.push(m);
  ///     MatchResult::Continue
  ///   };
  ///
  ///   {
  ///     // `matcher` now keeps the reference to `matches` alive
  ///     // in rustc's local lifetime tracking.
  ///     let matcher = StreamMatcher::new(&mut match_fn);
  ///     let mut sink = ScratchStreamSink::new(live, matcher, scratch);
  ///
  ///     sink.scan_vectored(input.as_ref().into())?;
  ///     sink.flush_eod()?;
  ///   }
  ///   // `matches` is now "unlocked" by rustc after `matcher` was dropped!
  ///   let matches: Vec<Range<usize>> = matches
  ///     .into_iter()
  ///     .map(|m| m.range.into())
  ///     .collect();
  ///   // 8..10 is across a non-contiguous input boundary!
  ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 8..10]);
  ///   Ok(())
  /// }
  /// # #[cfg(not(feature = "compiler"))]
  /// # fn main() {}
  /// ```
  #[cfg(feature = "vectored")]
  #[cfg_attr(docsrs, doc(cfg(feature = "vectored")))]
  pub fn scan_vectored<'data>(
    &mut self,
    data: VectoredByteSlices<'data, 'data>,
  ) -> Result<(), VectorscanRuntimeError> {
    let Self {
      live,
      matcher,
      scratch,
    } = self;
    scratch
      .make_mut()?
      .scan_sync_vectored_stream(live.make_mut()?, matcher, data)
  }

  /// Trigger any match callbacks that require matching against the end of data
  /// (EOD).
  ///
  /// [`Expression::info()`] returns a [`MatchAtEndBehavior`] can be used to
  /// determine whether this check is necessary. But it typically makes sense
  /// to execute it exactly once at the end of every stream instead of trying
  /// to optimize this away.
  ///
  /// [`Expression::info()`]: crate::expression::Expression::info
  /// [`MatchAtEndBehavior`]: crate::expression::info::MatchAtEndBehavior
  pub fn flush_eod(&mut self) -> Result<(), VectorscanRuntimeError> {
    let Self {
      live,
      matcher,
      scratch,
    } = self;
    scratch
      .make_mut()?
      .flush_eod_sync(live.make_mut()?, matcher)
  }

  /// Reach into [`Self::live`] and call [`LiveStream::reset()`].
  pub fn reset(&mut self) -> Result<(), VectorscanRuntimeError> { self.live.make_mut()?.reset() }
}

pub(crate) mod std_impls {
  use super::ScratchStreamSink;
  use crate::sources::ByteSlice;
  #[cfg(feature = "vectored")]
  use crate::sources::VectoredByteSlices;

  use std::io;
  #[cfg(feature = "vectored")]
  use std::mem;

  /// A wrapper over [`ScratchStreamSink`] which implements [`io::Write`].
  ///
  /// The reason this is separate from [`ScratchStreamSink`] is that in the case
  /// of vectored writes, [`io::IoSlice`] must be converted into
  /// [`VectoredByteSlices`]. This would typically require a dynamic memory
  /// allocation, but this struct maintains an internal buffer of strings
  /// which is re-used for subsequent vectored writes to avoid repeated dynamic
  /// memory allocation. This buffer isn't needed except for compatibility with
  /// the [`io::IoSlice`] API.
  ///
  ///```
  /// #[cfg(feature = "compiler")]
  /// fn main() -> Result<(), vectorscan::error::VectorscanError> {
  ///   use vectorscan::{expression::*, flags::*, stream::*, matchers::*};
  ///   use std::{ops::Range, io::Write};
  ///
  ///   let expr: Expression = "a+".parse()?;
  ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
  ///   let scratch = db.allocate_scratch()?;
  ///   let live = db.allocate_stream()?;
  ///
  ///   let mut matches: Vec<StreamMatch> = Vec::new();
  ///   let mut match_fn = |m| {
  ///     matches.push(m);
  ///     MatchResult::Continue
  ///   };
  ///   // Create a scope to mutably borrow `matches` in via `match_fn`:
  ///   {
  ///     let matcher = StreamMatcher::new(&mut match_fn);
  ///     let sink = ScratchStreamSink::new(live, matcher, scratch);
  ///     let mut sink = StreamWriter::new(sink);
  ///
  ///     sink.write_all(b"aardvark").unwrap();
  ///     // No analogy for tokio's .shutdown(), but we still
  ///     // need to explicitly mark end-of-data:
  ///     sink.inner.flush_eod()?;
  ///   }
  ///   // After `sink` (and therefore `matcher`) was dropped,
  ///   // we can access the closed-over data again!
  ///   let matches: Vec<Range<usize>> = matches
  ///     .into_iter()
  ///     .map(|m| m.range.into())
  ///     .collect();
  ///   assert_eq!(&matches, &[0..1, 0..2, 5..6]);
  ///   Ok(())
  /// }
  /// # #[cfg(not(feature = "compiler"))]
  /// # fn main() {}
  /// ```
  pub struct StreamWriter<'code> {
    #[allow(missing_docs)]
    pub inner: ScratchStreamSink<'code>,
    #[cfg(feature = "vectored")]
    vectored_buf_cache: Vec<mem::MaybeUninit<ByteSlice<'static>>>,
  }

  impl<'code> StreamWriter<'code> {
    /// Construct a wrapper over `inner`.
    pub fn new(inner: ScratchStreamSink<'code>) -> Self {
      Self {
        inner,
        #[cfg(feature = "vectored")]
        vectored_buf_cache: Vec::new(),
      }
    }
  }

  impl<'code> io::Write for StreamWriter<'code> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
      self
        .inner
        .scan(ByteSlice::from_slice(buf))
        .map(|()| buf.len())
        .map_err(io::Error::other)
    }

    fn flush(&mut self) -> io::Result<()> { Ok(()) }

    /* TODO: impl `is_write_vectored()` when stabilized! */
    #[cfg(feature = "vectored")]
    #[cfg_attr(docsrs, doc(cfg(feature = "vectored")))]
    fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
      let bufs = VectoredByteSlices::from_io_slices(&mut self.vectored_buf_cache, bufs);
      let len = bufs.length_sum();
      self
        .inner
        .scan_vectored(bufs)
        .map(|()| len)
        .map_err(io::Error::other)
    }
  }
}
pub use std_impls::StreamWriter;

/// Higher-level wrappers for `async` stream parsing.
///
/// # `async` and Stream Parsing
/// As discussed in [Asynchronous String Scanning], `async` code can
/// occasionally offer additional opportunities for parallelism by sending
/// vectorscan matches through an async channel. However, `async` can be
/// particularly useful for stream parsing applications for a different reason:
/// because it uses up much less resources waiting on things like bursty inputs.
/// For example, if you have a pattern with an extremely high match rate, it
/// might be beneficial to buffer its output somewhat instead of performing
/// logic directly in the match callback.
///
/// These structs implement idiomatic `async` interfaces that allow vectorscan
/// to be plugged into a variety of `async` codebases.
///
/// [Asynchronous String Scanning]: crate::state::Scratch#asynchronous-string-scanning
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub mod channel {
  use super::LiveStream;
  #[cfg(feature = "vectored")]
  use crate::sources::VectoredByteSlices;
  use crate::{
    error::{ScanError, VectorscanRuntimeError},
    matchers::{
      stream::{SendStreamMatcher, StreamMatch},
      MatchResult,
    },
    sources::ByteSlice,
    state::Scratch,
  };

  use futures_core::stream::Stream;
  use handles::Handle;
  use tokio::{sync::mpsc, task};

  use std::mem;

  /// An `async` version of [`super::ScratchStreamSink`].
  ///
  /// By holding handles to [`Self::live`] and [`Self::scratch`], the stream
  /// scanning API can be made quite fluent, without as many parameters per
  /// call:
  ///
  ///```
  /// #[cfg(feature = "compiler")]
  /// fn main() -> Result<(), vectorscan::error::VectorscanError> { tokio_test::block_on(async {
  ///   use vectorscan::{expression::*, flags::*, stream::channel::*, matchers::*};
  ///   use futures_util::StreamExt;
  ///   use std::ops::Range;
  ///
  ///   let expr: Expression = "a+".parse()?;
  ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
  ///   let scratch = db.allocate_scratch()?;
  ///   let live = db.allocate_stream()?;
  ///
  ///   let mut match_fn = |_: &_| MatchResult::Continue;
  ///   let mut sink = ScratchStreamSinkChannel::new(live, &mut match_fn, scratch);
  ///
  ///   sink.scan("aardvark".into()).await?;
  ///   sink.flush_eod().await?;
  ///
  ///   let matches: Vec<Range<usize>> = sink.collect_matches()
  ///     .map(|m| m.range.into())
  ///     .collect()
  ///     .await;
  ///   assert_eq!(&matches, &[0..1, 0..2, 5..6]);
  ///   Ok(())
  /// })}
  /// # #[cfg(not(feature = "compiler"))]
  /// # fn main() {}
  /// ```
  pub struct ScratchStreamSinkChannel<'code> {
    /// Cloneable handle to a stateful stream.
    pub live: Box<dyn Handle<R=LiveStream>+Send>,
    /// A "handler function" synthesized by [`Self::new()`] which closes over an
    /// [`mpsc::UnboundedSender`].
    pub hf: Box<dyn FnMut(StreamMatch) -> MatchResult+Send+'code>,
    /// Cloneable handle to a scratch space initialized for the same db as
    /// [`Self::live`].
    pub scratch: Box<dyn Handle<R=Scratch>+Send>,
    /// The other half of the sender/receiver pair created by [`Self::new()`].
    pub rx: mpsc::UnboundedReceiver<StreamMatch>,
  }

  impl<'code> ScratchStreamSinkChannel<'code> {
    fn translate_async_sender(
      hf: &'code mut (dyn FnMut(&StreamMatch) -> MatchResult+Send+'code),
      tx: mpsc::UnboundedSender<StreamMatch>,
    ) -> Box<dyn FnMut(StreamMatch) -> MatchResult+Send+'code> {
      Box::new(move |m| {
        let result = hf(&m);
        tx.send(m).unwrap();
        result
      })
    }

    /// Generate an [`mpsc::unbounded_channel()`] and a wrapper over the
    /// provided handler function `hf` that sends match objects into the
    /// channel as messages.
    pub fn new(
      live: impl Handle<R=LiveStream>+Send,
      hf: &'code mut (dyn FnMut(&StreamMatch) -> MatchResult+Send+'code),
      scratch: impl Handle<R=Scratch>+Send,
    ) -> Self {
      let (tx, rx) = mpsc::unbounded_channel();
      let hf = Self::translate_async_sender(hf, tx);
      Self {
        live: Box::new(live),
        hf,
        scratch: Box::new(scratch),
        rx,
      }
    }

    /// Write a single contiguous string into the automaton.
    ///
    ///```
    /// #[cfg(feature = "compiler")]
    /// fn main() -> Result<(), vectorscan::error::VectorscanError> { tokio_test::block_on(async {
    ///   use vectorscan::{expression::*, flags::*, stream::channel::*, matchers::*};
    ///   use futures_util::StreamExt;
    ///   use std::ops::Range;
    ///
    ///   let expr: Expression = "a+".parse()?;
    ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
    ///   let scratch = db.allocate_scratch()?;
    ///   let live = db.allocate_stream()?;
    ///
    ///   let mut match_fn = |_: &_| MatchResult::Continue;
    ///   let mut sink = ScratchStreamSinkChannel::new(live, &mut match_fn, scratch);
    ///
    ///   sink.scan("aardvarka".into()).await?;
    ///   sink.scan("a".into()).await?;
    ///   sink.flush_eod().await?;
    ///
    ///   let matches: Vec<Range<usize>> = sink.collect_matches()
    ///     .map(|m| m.range.into())
    ///     .collect()
    ///     .await;
    ///   // 8..10 crossed our non-contiguous inputs!
    ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 8..10]);
    ///   Ok(())
    /// })}
    /// # #[cfg(not(feature = "compiler"))]
    /// # fn main() {}
    /// ```
    pub async fn scan<'data>(&mut self, data: ByteSlice<'data>) -> Result<(), ScanError> {
      /* Make the mutable resources static. */
      let Self {
        live, hf, scratch, ..
      } = self;
      let live: &'static mut LiveStream = unsafe { mem::transmute(live.make_mut()?) };
      let scratch: &'static mut Scratch = unsafe { mem::transmute(scratch.make_mut()?) };
      let data: ByteSlice<'static> = unsafe { mem::transmute(data) };

      /* Generate a vtable pointing to the heap-allocated handler function hf. */
      let hf: &mut (dyn FnMut(StreamMatch) -> MatchResult+Send+'code) = hf;
      let matcher = SendStreamMatcher::new(hf);
      let mut matcher: SendStreamMatcher<'static> = unsafe { mem::transmute(matcher) };

      task::spawn_blocking(move || scratch.scan_sync_stream(live, matcher.as_mut_basic(), data))
        .await??;
      Ok(())
    }

    /// Write vectored string data into the automaton.
    ///
    ///```
    /// #[cfg(feature = "compiler")]
    /// fn main() -> Result<(), vectorscan::error::VectorscanError> { tokio_test::block_on(async {
    ///   use vectorscan::{expression::*, flags::*, stream::channel::*, matchers::*, sources::*};
    ///   use futures_util::StreamExt;
    ///   use std::ops::Range;
    ///
    ///   let expr: Expression = "a+".parse()?;
    ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
    ///   let scratch = db.allocate_scratch()?;
    ///   let live = db.allocate_stream()?;
    ///
    ///   let mut match_fn = |_: &_| MatchResult::Continue;
    ///   let mut sink = ScratchStreamSinkChannel::new(live, &mut match_fn, scratch);
    ///
    ///   let input: [ByteSlice; 2] = [
    ///     "aardvarka".into(),
    ///     "a".into(),
    ///   ];
    ///
    ///   sink.scan_vectored(input.as_ref().into()).await?;
    ///   sink.flush_eod().await?;
    ///
    ///   let matches: Vec<Range<usize>> = sink.collect_matches()
    ///     .map(|m| m.range.into())
    ///     .collect()
    ///     .await;
    ///   // 8..10 crossed our non-contiguous inputs!
    ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 8..10]);
    ///   Ok(())
    /// })}
    /// # #[cfg(not(feature = "compiler"))]
    /// # fn main() {}
    /// ```
    #[cfg(feature = "vectored")]
    #[cfg_attr(docsrs, doc(cfg(feature = "vectored")))]
    pub async fn scan_vectored<'data>(
      &mut self,
      data: VectoredByteSlices<'data, 'data>,
    ) -> Result<(), ScanError> {
      /* Make the mutable resources static. */
      let Self {
        live, hf, scratch, ..
      } = self;
      let live: &'static mut LiveStream = unsafe { mem::transmute(live.make_mut()?) };
      let scratch: &'static mut Scratch = unsafe { mem::transmute(scratch.make_mut()?) };
      let data: VectoredByteSlices<'static, 'static> = unsafe { mem::transmute(data) };

      /* Generate a vtable pointing to the heap-allocated handler function hf. */
      let hf: &mut (dyn FnMut(StreamMatch) -> MatchResult+Send+'code) = hf;
      let matcher = SendStreamMatcher::new(hf);
      let mut matcher: SendStreamMatcher<'static> = unsafe { mem::transmute(matcher) };

      task::spawn_blocking(move || {
        scratch.scan_sync_vectored_stream(live, matcher.as_mut_basic(), data)
      })
      .await??;
      Ok(())
    }

    /// Trigger any match callbacks that require matching against the end of
    /// data (EOD).
    ///
    /// [`Expression::info()`] returns a [`MatchAtEndBehavior`] can be used to
    /// determine whether this check is necessary. But it typically makes sense
    /// to execute it exactly once at the end of every stream instead of trying
    /// to optimize this away.
    ///
    /// [`Expression::info()`]: crate::expression::Expression::info
    /// [`MatchAtEndBehavior`]: crate::expression::info::MatchAtEndBehavior
    pub async fn flush_eod(&mut self) -> Result<(), ScanError> {
      /* Make the mutable resources static. */
      let Self {
        live, hf, scratch, ..
      } = self;
      let live: &'static mut LiveStream = unsafe { mem::transmute(live.make_mut()?) };
      let scratch: &'static mut Scratch = unsafe { mem::transmute(scratch.make_mut()?) };

      /* Generate a vtable pointing to the heap-allocated handler function hf. */
      let hf: &mut (dyn FnMut(StreamMatch) -> MatchResult+Send+'code) = hf;
      let matcher = SendStreamMatcher::new(hf);
      let mut matcher: SendStreamMatcher<'static> = unsafe { mem::transmute(matcher) };

      task::spawn_blocking(move || scratch.flush_eod_sync(live, matcher.as_mut_basic())).await??;
      Ok(())
    }

    /// Call [`mpsc::UnboundedReceiver::close()`] on [`Self::rx`] then convert
    /// it into an async iterable stream.
    pub fn collect_matches(mut self) -> impl Stream<Item=StreamMatch> {
      self.rx.close();
      crate::async_utils::UnboundedReceiverStream(self.rx)
    }

    /// Reach into [`Self::live`] and call [`LiveStream::reset()`].
    pub fn reset(&mut self) -> Result<(), VectorscanRuntimeError> { self.live.make_mut()?.reset() }
  }

  #[cfg(feature = "tokio-impls")]
  pub(crate) mod tokio_impls {
    use super::ScratchStreamSinkChannel;
    use crate::sources::ByteSlice;
    #[cfg(feature = "vectored")]
    use crate::sources::VectoredByteSlices;

    use futures_util::TryFutureExt;
    use tokio::io;

    #[cfg(feature = "vectored")]
    use std::io::IoSlice;
    use std::{
      future::Future,
      mem,
      pin::Pin,
      task::{ready, Context, Poll},
    };

    /// A wrapper over [`ScratchStreamSinkChannel`] which implements
    /// [`tokio::io::AsyncWrite`].
    ///
    /// The reason this is separate from [`ScratchStreamSinkChannel`] is that in
    /// the case of vectored writes, [`std::io::IoSlice`] must be converted into
    /// [`VectoredByteSlices`]. As in the synchronous
    /// [`super::super::StreamWriter`], this would typically require a dynamic
    /// memory allocation, but this struct maintains an internal buffer of
    /// strings which is re-used for subsequent vectored writes to avoid
    /// repeated dynamic memory allocation. Additionally, in the async case,
    /// implementing poll methods like [`io::AsyncWrite::poll_write()`] and
    /// [`io::AsyncWrite::poll_shutdown()`] requires storing
    /// [`Waker`](std::task::Waker) state inside the object whenever it is
    /// polled.
    ///
    ///```
    /// #[cfg(feature = "compiler")]
    /// fn main() -> Result<(), vectorscan::error::VectorscanError> { tokio_test::block_on(async {
    ///   use vectorscan::{expression::*, flags::*, stream::channel::*, matchers::*};
    ///   use futures_util::StreamExt;
    ///   use tokio::io::AsyncWriteExt;
    ///   use std::ops::Range;
    ///
    ///   let expr: Expression = "a+".parse()?;
    ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_LARGE)?;
    ///   let scratch = db.allocate_scratch()?;
    ///   let live = db.allocate_stream()?;
    ///
    ///   let mut match_fn = |_: &_| MatchResult::Continue;
    ///   let sink = ScratchStreamSinkChannel::new(live, &mut match_fn, scratch);
    ///   let mut sink = AsyncStreamWriter::new(sink);
    ///
    ///   sink.write_all(b"aardvark").await.unwrap();
    ///   sink.shutdown().await.unwrap();
    ///
    ///   let matches: Vec<Range<usize>> = sink.inner.collect_matches()
    ///     .map(|m| m.range.into())
    ///     .collect()
    ///     .await;
    ///   assert_eq!(&matches, &[0..1, 0..2, 5..6]);
    ///   Ok(())
    /// })}
    /// # #[cfg(not(feature = "compiler"))]
    /// # fn main() {}
    /// ```
    pub struct AsyncStreamWriter<'code> {
      #[allow(missing_docs)]
      pub inner: ScratchStreamSinkChannel<'code>,
      #[cfg(feature = "vectored")]
      vectored_buf_cache: Vec<mem::MaybeUninit<ByteSlice<'static>>>,
      write_future: Option<Pin<Box<dyn Future<Output=io::Result<usize>>+'code>>>,
      shutdown_future: Option<Pin<Box<dyn Future<Output=io::Result<()>>+'code>>>,
    }

    impl<'code> AsyncStreamWriter<'code> {
      /// Construct a wrapper over `inner`.
      pub fn new(inner: ScratchStreamSinkChannel<'code>) -> Self {
        Self {
          inner,
          #[cfg(feature = "vectored")]
          vectored_buf_cache: Vec::new(),
          write_future: None,
          shutdown_future: None,
        }
      }
    }

    unsafe impl<'code> Send for AsyncStreamWriter<'code> {}

    impl<'code> io::AsyncWrite for AsyncStreamWriter<'code> {
      fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
      ) -> Poll<io::Result<usize>> {
        if self.write_future.is_some() {
          let ret = ready!(self
            .as_mut()
            .write_future
            .as_mut()
            .unwrap()
            .as_mut()
            .poll(cx));

          self.write_future = None;

          Poll::Ready(ret)
        } else {
          let mut fut: Pin<Box<dyn Future<Output=io::Result<usize>>+'code>> = {
            let s: &'code mut Self = unsafe { mem::transmute(self.as_mut().get_mut()) };
            let buf: &'code [u8] = unsafe { mem::transmute(buf) };
            let buf_len = buf.len();
            let buf = ByteSlice::from_slice(buf);
            Box::pin(
              s.inner
                .scan(buf)
                .map_ok(move |()| buf_len)
                .map_err(io::Error::other),
            )
          };

          if let Poll::Ready(ret) = fut.as_mut().poll(cx) {
            return Poll::Ready(ret);
          }

          self.write_future = Some(fut);

          Poll::Pending
        }
      }

      fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
      }

      fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        if self.shutdown_future.is_some() {
          let ret = ready!(self
            .as_mut()
            .shutdown_future
            .as_mut()
            .unwrap()
            .as_mut()
            .poll(cx));

          self.shutdown_future = None;

          Poll::Ready(ret)
        } else {
          let mut fut: Pin<Box<dyn Future<Output=io::Result<()>>+'code>> = {
            let s: &'code mut Self = unsafe { mem::transmute(self.as_mut().get_mut()) };
            Box::pin(s.inner.flush_eod().map_err(io::Error::other))
          };

          if let Poll::Ready(ret) = fut.as_mut().poll(cx) {
            return Poll::Ready(ret);
          }

          self.shutdown_future = Some(fut);

          Poll::Pending
        }
      }

      #[cfg(feature = "vectored")]
      #[cfg_attr(docsrs, doc(cfg(feature = "vectored")))]
      fn is_write_vectored(&self) -> bool { true }

      #[cfg(feature = "vectored")]
      #[cfg_attr(docsrs, doc(cfg(feature = "vectored")))]
      fn poll_write_vectored(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bufs: &[IoSlice<'_>],
      ) -> Poll<io::Result<usize>> {
        if self.write_future.is_some() {
          let ret = ready!(self
            .as_mut()
            .write_future
            .as_mut()
            .unwrap()
            .as_mut()
            .poll(cx));

          self.write_future = None;

          Poll::Ready(ret)
        } else {
          let mut fut: Pin<Box<dyn Future<Output=io::Result<usize>>+'code>> = {
            let s: &'code mut Self = unsafe { mem::transmute(self.as_mut().get_mut()) };
            let bufs: &'code [IoSlice<'code>] = unsafe { mem::transmute(bufs) };
            let bufs = VectoredByteSlices::from_io_slices(&mut s.vectored_buf_cache, bufs);
            let len = bufs.length_sum();
            Box::pin(
              s.inner
                .scan_vectored(bufs)
                .map_ok(move |()| len)
                .map_err(io::Error::other),
            )
          };

          if let Poll::Ready(ret) = fut.as_mut().poll(cx) {
            return Poll::Ready(ret);
          }

          self.write_future = Some(fut);

          Poll::Pending
        }
      }
    }
  }
  #[cfg(feature = "tokio-impls")]
  #[cfg_attr(docsrs, doc(cfg(feature = "tokio-impls")))]
  pub use tokio_impls::AsyncStreamWriter;
}

pub(crate) mod compress {
  use super::{LiveStream, NativeStream};
  use crate::{
    database::Database,
    error::{CompressionError, VectorscanRuntimeError},
    hs,
  };

  use std::{mem, ptr};

  /// How to allocate the buffer in [`CompressedStream::compress()`].
  pub enum CompressReserveBehavior {
    /// Allocate a new buffer of the appropriate size.
    NewBuf,
    /// Re-use the given buffer, but expand its size by reallocating if needed.
    ExpandBuf(Vec<u8>),
    /// Re-use the given buffer, and raise an error if reallocation is
    /// necessary.
    FixedSizeBuf(Vec<u8>),
  }

  impl CompressReserveBehavior {
    /// Get a mutable reference to the existing buffer contents, if available.
    ///
    /// The contents can be moved out of the resulting mutable vector with e.g.
    /// [`mem::take()`]:
    ///
    ///```
    /// use vectorscan::stream::CompressReserveBehavior;
    /// use std::mem;
    ///
    /// let mut buf = CompressReserveBehavior::NewBuf;
    /// assert!(buf.current_buf().is_none());
    /// buf = CompressReserveBehavior::FixedSizeBuf(vec![0; 32]);
    /// let b = mem::take(buf.current_buf().unwrap());
    /// assert_eq!(buf.current_buf(), Some(&mut vec![]));
    /// assert_eq!(b, vec![0; 32]);
    /// ```
    pub fn current_buf(&mut self) -> Option<&mut Vec<u8>> {
      match self {
        Self::NewBuf => None,
        Self::ExpandBuf(ref mut buf) => Some(buf),
        Self::FixedSizeBuf(ref mut buf) => Some(buf),
      }
    }
  }

  enum ReserveResponse {
    MadeSpace(Vec<u8>),
    NoSpace(Vec<u8>),
  }

  impl CompressReserveBehavior {
    fn reserve(self, n: usize) -> ReserveResponse {
      match self {
        Self::NewBuf => ReserveResponse::MadeSpace(Vec::with_capacity(n)),
        Self::ExpandBuf(mut buf) => {
          if n > buf.capacity() {
            let additional = n - buf.capacity();
            buf.reserve(additional);
          }
          ReserveResponse::MadeSpace(buf)
        },
        Self::FixedSizeBuf(buf) => {
          if buf.capacity() <= n {
            ReserveResponse::NoSpace(buf)
          } else {
            ReserveResponse::MadeSpace(buf)
          }
        },
      }
    }
  }

  /// Analogous to [`SerializedDb`](crate::database::SerializedDb), but for
  /// serializing streams!
  pub struct CompressedStream {
    buf: Box<[u8]>,
  }

  impl CompressedStream {
    /// Consume this struct and return the contents of the internal buffer.
    ///
    /// The resulting box can be converted into a [`Vec`] with
    /// [`slice::into_vec()`](prim@slice::into_vec()).
    pub fn into_buf(self) -> Box<[u8]> { self.buf }

    /// Write the stream's current state into a buffer according to the `into`
    /// strategy.
    ///
    /// The stream can later be deserialized from this state into a working
    /// in-memory stream again with methods such as [`Self::expand()`].
    ///
    /// If this method fails to allocate space due to the decision of the `into`
    /// strategy, it will return [`CompressionError::NoSpace`].
    ///
    /// [`Self::into_buf()`] can be applied similarly to
    /// [`CompressReserveBehavior::current_buf()`] in order to reuse the backing
    /// storage for a subsequent [`CompressReserveBehavior`] request:
    ///
    ///```
    /// #[cfg(feature = "compiler")]
    /// fn main() -> Result<(), vectorscan::error::VectorscanError> {
    ///   use vectorscan::{expression::*, flags::*, matchers::*, stream::*, state::*};
    ///   use std::{mem, ops::Range};
    ///
    ///   let a_expr: Expression = "a+".parse()?;
    ///   let b_expr: Expression = "b+".parse()?;
    ///   let a_db = a_expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_SMALL)?;
    ///   let b_db = b_expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_SMALL)?;
    ///   let mut scratch = Scratch::blank();
    ///   scratch.setup_for_db(&a_db)?;
    ///   scratch.setup_for_db(&b_db)?;
    ///   let a_live = a_db.allocate_stream()?;
    ///   let b_live = b_db.allocate_stream()?;
    ///
    ///   // Allocate a new buffer.
    ///   let a_compressed = a_live.compress(CompressReserveBehavior::NewBuf)?;
    ///   // We no longer need the old stream:
    ///   mem::drop(a_live);
    ///   // Deserialize the buffer into a new stream allocation:
    ///   let a_live = a_compressed.expand(&a_db)?;
    ///
    ///   // Extract the buffer: let's reuse it for another stream.
    ///   let buf: Vec<u8> = a_compressed.into_buf().into_vec();
    ///   let b_compressed = b_live.compress(CompressReserveBehavior::ExpandBuf(buf))?;
    ///   // We no longer need the old stream:
    ///   mem::drop(b_live);
    ///   // Deserialize the buffer into a new stream allocation:
    ///   let b_live = b_compressed.expand(&b_db)?;
    ///
    ///   let mut matches: Vec<StreamMatch> = Vec::new();
    ///   let mut match_fn = |m| {
    ///     matches.push(m);
    ///     MatchResult::Continue
    ///   };
    ///
    ///   // Both streams work:
    ///   {
    ///     let matcher = StreamMatcher::new(&mut match_fn);
    ///     let mut sink = ScratchStreamSink::new(a_live, matcher, scratch);
    ///
    ///     sink.scan("aardvarka".into())?;
    ///     sink.flush_eod()?;
    ///     // Overwrite the live stream object,
    ///     // but retain the scratch and match callback:
    ///     *sink.live.make_mut()? = b_live;
    ///     // The stream allocation from `a_live` has now been dropped!
    ///     sink.scan("imbibbe".into())?;
    ///     sink.flush_eod()?;
    ///   }
    ///
    ///   let matches: Vec<Range<usize>> = matches
    ///     .into_iter()
    ///     .map(|m| m.range.into())
    ///     .collect();
    ///   // The matches beginning with 2..3 are from `b_live`!
    ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 2..3, 4..5, 4..6]);
    ///   Ok(())
    /// }
    /// # #[cfg(not(feature = "compiler"))]
    /// # fn main() {}
    /// ```
    pub fn compress(
      mut into: CompressReserveBehavior,
      live: &LiveStream,
    ) -> Result<Self, CompressionError> {
      let mut required_space: usize = 0;

      /* If we already have an existing buffer to play around with, try that right
       * off to see if it was enough to avoid further allocations. */
      if let Some(buf) = into.current_buf() {
        match VectorscanRuntimeError::from_native(unsafe {
          hs::hs_compress_stream(
            live.as_ref_native(),
            mem::transmute(buf.as_mut_ptr()),
            buf.capacity(),
            &mut required_space,
          )
        }) {
          Err(VectorscanRuntimeError::InsufficientSpace) => (),
          Err(e) => return Err(e.into()),
          Ok(()) => {
            debug_assert!(buf.capacity() >= required_space);
            unsafe {
              buf.set_len(required_space);
            }
            return Ok(Self {
              buf: mem::take(buf).into_boxed_slice(),
            });
          },
        }
      } else {
        /* Otherwise (e.g. if we have a NewBuf), get the required space first
         * before trying to allocate anything by providing
         * NULL for the data pointer. */
        assert_eq!(
          Err(VectorscanRuntimeError::InsufficientSpace),
          VectorscanRuntimeError::from_native(unsafe {
            hs::hs_compress_stream(
              live.as_ref_native(),
              ptr::null_mut(),
              0,
              &mut required_space,
            )
          })
        );
      }
      /* At this point, we know some allocation is necessary, and the
       * `required_space` variable is populated with the amount of space
       * necessary to compress. */

      /* Allocate or fail allocation. */
      let buf = match into.reserve(required_space) {
        ReserveResponse::NoSpace(buf) => {
          /* This is supposed to be what ReserveResponse checks. */
          debug_assert!(required_space > buf.len());
          return Err(CompressionError::NoSpace(required_space, buf));
        },
        ReserveResponse::MadeSpace(mut buf) => {
          let mut allocated_space: usize = 0;
          VectorscanRuntimeError::from_native(unsafe {
            hs::hs_compress_stream(
              live.as_ref_native(),
              mem::transmute(buf.as_mut_ptr()),
              buf.capacity(),
              &mut allocated_space,
            )
          })?;
          /* No particular reason these values should be different across two
           * subsequent calls. */
          debug_assert_eq!(required_space, allocated_space);
          debug_assert!(allocated_space <= buf.capacity());
          unsafe {
            buf.set_len(allocated_space);
          }
          buf
        },
      };

      Ok(Self {
        buf: buf.into_boxed_slice(),
      })
    }

    /// Deserialize the current buffer into a newly allocated stream object.
    ///
    ///```
    /// #[cfg(feature = "compiler")]
    /// fn main() -> Result<(), vectorscan::error::VectorscanError> {
    ///   use vectorscan::{expression::*, flags::*, stream::*, matchers::*};
    ///   use std::ops::Range;
    ///
    ///   let expr: Expression = "a+".parse()?;
    ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_SMALL)?;
    ///   let scratch = db.allocate_scratch()?;
    ///   let live = db.allocate_stream()?;
    ///
    ///   // Allocate a new buffer.
    ///   let behavior = CompressReserveBehavior::NewBuf;
    ///   // Compress into the buffer.
    ///   let compressed = live.compress(behavior)?;
    ///
    ///   // Deserialize the stream from the buffer into a new stream allocation:
    ///   let live2 = compressed.expand(&db)?;
    ///
    ///   let mut matches: Vec<StreamMatch> = Vec::new();
    ///   let mut match_fn = |m| {
    ///     matches.push(m);
    ///     MatchResult::Continue
    ///   };
    ///
    ///   // Both streams work:
    ///   {
    ///     let matcher = StreamMatcher::new(&mut match_fn);
    ///     let mut sink = ScratchStreamSink::new(live, matcher, scratch);
    ///
    ///     sink.scan("aardvarka".into())?;
    ///     sink.flush_eod()?;
    ///     // This is safe because `live` and `live2` are for the same db!
    ///     unsafe { sink.live.make_mut()?.try_clone_from(&live2)?; }
    ///     sink.scan("aardvarka".into())?;
    ///     sink.flush_eod()?;
    ///   }
    ///
    ///   let matches: Vec<Range<usize>> = matches
    ///     .into_iter()
    ///     .map(|m| m.range.into())
    ///     .collect();
    ///   // The second round of matches starting with 0..1 is from `live2`!
    ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 0..1, 0..2, 5..6, 8..9]);
    ///   Ok(())
    /// }
    /// # #[cfg(not(feature = "compiler"))]
    /// # fn main() {}
    /// ```
    pub fn expand(&self, db: &Database) -> Result<LiveStream, VectorscanRuntimeError> {
      let mut inner = ptr::null_mut();
      VectorscanRuntimeError::from_native(unsafe {
        hs::hs_expand_stream(
          db.as_ref_native(),
          &mut inner,
          mem::transmute(self.buf.as_ptr()),
          self.buf.len(),
        )
      })?;
      Ok(unsafe { LiveStream::from_native(inner) })
    }

    /// Deserialize the current buffer into a previously allocated stream
    /// object.
    ///
    /// # Safety
    /// `self` and `to` must have been opened against the same db!
    ///
    ///```
    /// #[cfg(feature = "compiler")]
    /// fn main() -> Result<(), vectorscan::error::VectorscanError> {
    ///   use vectorscan::{expression::*, flags::*, stream::*, matchers::*};
    ///   use std::ops::Range;
    ///
    ///   let expr: Expression = "a+".parse()?;
    ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_SMALL)?;
    ///   let scratch = db.allocate_scratch()?;
    ///   let mut live = db.allocate_stream()?;
    ///
    ///   // Allocate a new buffer.
    ///   let behavior = CompressReserveBehavior::NewBuf;
    ///   // Compress into the buffer.
    ///   let compressed = live.compress(behavior)?;
    ///
    ///   // Expand into an existing live stream:
    ///   // This is safe because they were opened against the same db!
    ///   unsafe { compressed.expand_into(&mut live)?; }
    ///
    ///   let mut matches: Vec<StreamMatch> = Vec::new();
    ///   let mut match_fn = |m| {
    ///     matches.push(m);
    ///     MatchResult::Continue
    ///   };
    ///
    ///   // The stream works normally after expanding into it:
    ///   {
    ///     let matcher = StreamMatcher::new(&mut match_fn);
    ///     let mut sink = ScratchStreamSink::new(live, matcher, scratch);
    ///
    ///     sink.scan("aardvarka".into())?;
    ///     sink.flush_eod()?;
    ///   }
    ///
    ///   let matches: Vec<Range<usize>> = matches
    ///     .into_iter()
    ///     .map(|m| m.range.into())
    ///     .collect();
    ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9]);
    ///   Ok(())
    /// }
    /// # #[cfg(not(feature = "compiler"))]
    /// # fn main() {}
    /// ```
    pub unsafe fn expand_into(&self, to: &mut LiveStream) -> Result<(), VectorscanRuntimeError> {
      VectorscanRuntimeError::from_native(hs::hs_direct_expand_into(
        to.as_mut_native(),
        mem::transmute(self.buf.as_ptr()),
        self.buf.len(),
      ))
    }

    /// Deserialize the current buffer into an allocation from anywhere at all.
    ///
    /// # Safety
    /// `to` must point to an allocation of at least [`Database::stream_size()`]
    /// bytes in size given `db`!
    ///
    ///```
    /// #[cfg(feature = "compiler")]
    /// fn main() -> Result<(), vectorscan::error::VectorscanError> {
    ///   use vectorscan::{expression::*, flags::*, stream::*, matchers::*};
    ///   use std::{mem, ops::Range};
    ///
    ///   let expr: Expression = "a+".parse()?;
    ///   let db = expr.compile(Flags::SOM_LEFTMOST, Mode::STREAM | Mode::SOM_HORIZON_SMALL)?;
    ///   let scratch = db.allocate_scratch()?;
    ///   let live = db.allocate_stream()?;
    ///
    ///   // Allocate a new buffer.
    ///   let behavior = CompressReserveBehavior::NewBuf;
    ///   // Compress into the buffer.
    ///   let compressed = live.compress(behavior)?;
    ///
    ///   // Expand into a stream allocated from somewhere totally different:
    ///   let mut allocation = vec![0; db.stream_size()?];
    ///   let live2 = unsafe {
    ///     let allocation = allocation.as_mut_ptr() as *mut NativeStream;
    ///     compressed.expand_into_at(&db, allocation)?;
    ///     // ManuallyDrop avoids calling the stream dealloc method on our vector data:
    ///     mem::ManuallyDrop::new(LiveStream::from_native(allocation))
    ///   };
    ///
    ///   let mut matches: Vec<StreamMatch> = Vec::new();
    ///   let mut match_fn = |m| {
    ///     matches.push(m);
    ///     MatchResult::Continue
    ///   };
    ///
    ///   // Both streams work:
    ///   {
    ///     let matcher = StreamMatcher::new(&mut match_fn);
    ///     let mut sink = ScratchStreamSink::new(live, matcher, scratch);
    ///
    ///     sink.scan("aardvarka".into())?;
    ///     sink.flush_eod()?;
    ///     // This is safe because `live` and `live2` are for the same db!
    ///     unsafe { sink.live.make_mut()?.try_clone_from(&live2)?; }
    ///     sink.scan("aardvarka".into())?;
    ///     sink.flush_eod()?;
    ///   }
    ///
    ///   let matches: Vec<Range<usize>> = matches
    ///     .into_iter()
    ///     .map(|m| m.range.into())
    ///     .collect();
    ///   assert_eq!(&matches, &[0..1, 0..2, 5..6, 8..9, 0..1, 0..2, 5..6, 8..9]);
    ///   Ok(())
    /// }
    /// # #[cfg(not(feature = "compiler"))]
    /// # fn main() {}
    /// ```
    pub unsafe fn expand_into_at(
      &self,
      db: &Database,
      to: *mut NativeStream,
    ) -> Result<(), VectorscanRuntimeError> {
      VectorscanRuntimeError::from_native(hs::hs_expand_stream_at(
        db.as_ref_native(),
        mem::transmute(self.buf.as_ptr()),
        self.buf.len(),
        to,
      ))
    }
  }
}
pub use compress::{CompressReserveBehavior, CompressedStream};