wedb_embed 0.1.9

Embedded database engine providing Redis-like APIs, built on fjall / 嵌入式数据库引擎,提供类似 Redis 的接口,底层基于 fjall 开发
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
use std::str;

use crate::error::{Error, Result};

/// ZADD command option flags (aligned with Apache Kvrocks ZSetFlags).
/// ZADD 选项标志(对标 Apache Kvrocks ZSetFlags)
#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::Display, strum::EnumString, strum::FromRepr)]
#[strum(ascii_case_insensitive)]
pub enum ZAdd {
  Nx,
  Xx,
  Gt,
  Lt,
  Ch,
  Incr,
}

/// Aggregation function method (aligned with Apache Kvrocks AggregateMethod).
/// 聚合函数类型(对标 Apache Kvrocks AggregateMethod)
#[derive(
  Debug, Clone, Copy, PartialEq, Eq, Default, strum::Display, strum::EnumString, strum::FromRepr,
)]
#[strum(ascii_case_insensitive)]
pub enum Aggregate {
  #[default]
  Sum,
  Min,
  Max,
}

impl Aggregate {
  #[inline]
  pub fn parse(s: &str) -> Self {
    s.parse().unwrap_or_default()
  }

  #[inline]
  pub fn apply(&self, current: f64, new_val: f64) -> f64 {
    let res = match self {
      Self::Sum => current + new_val,
      Self::Min => current.min(new_val),
      Self::Max => current.max(new_val),
    };
    if res.is_nan() { 0.0 } else { res }
  }
}

/// Score range specification (aligned with Apache Kvrocks RangeScore).
/// 分数范围规格(对标 Apache Kvrocks RangeScore)
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RangeScore {
  pub min: f64,
  pub max: f64,
  pub minex: bool,
  pub maxex: bool,
  pub offset: usize,
  pub count: Option<usize>,
}

impl Default for RangeScore {
  #[inline]
  fn default() -> Self {
    Self {
      min: f64::NEG_INFINITY,
      max: f64::INFINITY,
      minex: false,
      maxex: false,
      offset: 0,
      count: None,
    }
  }
}

impl RangeScore {
  #[inline]
  pub fn new(min: f64, max: f64) -> Self {
    Self {
      min,
      max,
      ..Default::default()
    }
  }

  #[inline]
  pub fn with_limit(min: f64, max: f64, offset: usize, count: usize) -> Self {
    Self {
      min,
      max,
      minex: false,
      maxex: false,
      offset,
      count: Some(count),
    }
  }

  #[inline]
  pub fn is_empty(&self) -> bool {
    self.min > self.max || (self.min == self.max && (self.minex || self.maxex))
  }

  #[inline]
  pub fn check(&self, score: f64) -> bool {
    let min_ok = if self.minex {
      score > self.min
    } else {
      score >= self.min
    };
    let max_ok = if self.maxex {
      score < self.max
    } else {
      score <= self.max
    };
    min_ok && max_ok
  }

  /// Constructs RangeScore from Redis score boundary string.
  /// 从 Redis 分数边界字符串构造 RangeScore
  pub fn from_bounds(
    min_bound: &str,
    max_bound: &str,
    offset: usize,
    count: Option<usize>,
  ) -> Result<Self> {
    let (min, minex) = Self::parse_bound(min_bound)?;
    let (max, maxex) = Self::parse_bound(max_bound)?;
    Ok(Self {
      min,
      max,
      minex,
      maxex,
      offset,
      count,
    })
  }

  /// Parses Redis-style score boundary string (e.g. "(1.5", "[10", "10", "-inf", "+inf").
  /// 解析 Redis 风格的分数边界字符串(例如 "(1.5", "[10", "10", "-inf", "+inf", "(-inf" 等)
  pub fn parse_bound(s: &str) -> Result<(f64, bool)> {
    let s = s.trim();
    if s.is_empty() {
      return Err(Error::invalid_data("ERR min or max is not a float"));
    }

    let (val_str, is_exclusive) = if let Some(rest) = s.strip_prefix('(') {
      (rest.trim(), true)
    } else if let Some(rest) = s.strip_prefix('[') {
      (rest.trim(), false)
    } else {
      (s, false)
    };

    if val_str.eq_ignore_ascii_case("-inf") || val_str.eq_ignore_ascii_case("-infinity") {
      return Ok((f64::NEG_INFINITY, is_exclusive));
    }
    if val_str.eq_ignore_ascii_case("+inf")
      || val_str.eq_ignore_ascii_case("+infinity")
      || val_str.eq_ignore_ascii_case("inf")
      || val_str.eq_ignore_ascii_case("infinity")
    {
      return Ok((f64::INFINITY, is_exclusive));
    }

    let val = val_str
      .parse::<f64>()
      .map_err(|_| Error::invalid_data("ERR min or max is not a float"))?;
    if val.is_nan() {
      return Err(Error::invalid_data("ERR min or max is not a float"));
    }
    Ok((val, is_exclusive))
  }
}

/// Lexicographical range specification (aligned with Apache Kvrocks RangeLex).
/// 字典序范围规格(对标 Apache Kvrocks RangeLex,统一用于 ZSet + Hash)
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct RangeLex {
  pub min: Vec<u8>,
  pub max: Vec<u8>,
  pub minex: bool,
  pub maxex: bool,
  pub min_infinite: bool,
  pub max_infinite: bool,
  pub offset: usize,
  pub count: Option<usize>,
  pub reversed: bool,
}

impl RangeLex {
  #[inline]
  pub fn new(min: impl Into<Vec<u8>>, max: impl Into<Vec<u8>>) -> Self {
    Self {
      min: min.into(),
      max: max.into(),
      minex: false,
      maxex: false,
      min_infinite: false,
      max_infinite: false,
      offset: 0,
      count: None,
      reversed: false,
    }
  }

  #[inline]
  pub fn unbounded() -> Self {
    Self {
      min_infinite: true,
      max_infinite: true,
      ..Default::default()
    }
  }

  #[inline]
  pub fn is_empty(&self) -> bool {
    !self.min_infinite
      && !self.max_infinite
      && (self.min > self.max || (self.min == self.max && (self.minex || self.maxex)))
  }

  #[inline]
  pub fn check(&self, member: &[u8]) -> bool {
    let min_ok = if self.min_infinite {
      true
    } else if self.minex {
      member > self.min.as_slice()
    } else {
      member >= self.min.as_slice()
    };

    let max_ok = if self.max_infinite {
      true
    } else if self.maxex {
      member < self.max.as_slice()
    } else {
      member <= self.max.as_slice()
    };

    min_ok && max_ok
  }

  /// Constructs RangeLex from Redis boundary string with strict validation.
  /// 从 Redis 边界字符串或字节切片构造 RangeLex(严格校验 min 为 -/(/[,max 为 +/(/[)
  pub fn from_bounds(
    min_bound: &[u8],
    max_bound: &[u8],
    offset: usize,
    count: Option<usize>,
  ) -> Result<Self> {
    let (min, minex, min_infinite) = Self::parse_min_bound(min_bound)?;
    let (max, maxex, max_infinite) = Self::parse_max_bound(max_bound)?;
    Ok(Self {
      min,
      max,
      minex,
      maxex,
      min_infinite,
      max_infinite,
      offset,
      count,
      reversed: false,
    })
  }

  /// Parses Redis lexicographical lower bound (valid min is "-" or starts with '(' / '[').
  /// 解析 Redis 字典序下界(合法的 min 为 "-" 或以 '(' / '[' 开头)
  pub fn parse_min_bound(bound: &[u8]) -> Result<(Vec<u8>, bool, bool)> {
    if bound == b"-" {
      return Ok((Vec::new(), false, true));
    }
    if bound == b"+" {
      return Err(Error::invalid_data(
        "ERR min or max not valid string range item",
      ));
    }
    if let Some(rest) = bound.strip_prefix(b"(") {
      Ok((rest.to_vec(), true, false))
    } else if let Some(rest) = bound.strip_prefix(b"[") {
      Ok((rest.to_vec(), false, false))
    } else {
      Err(Error::invalid_data(
        "ERR min or max not valid string range item",
      ))
    }
  }

  /// Parses Redis lexicographical upper bound (valid max is "+" or starts with '(' / '[').
  /// 解析 Redis 字典序上界(合法的 max 为 "+" 或以 '(' / '[' 开头)
  pub fn parse_max_bound(bound: &[u8]) -> Result<(Vec<u8>, bool, bool)> {
    if bound == b"+" {
      return Ok((Vec::new(), false, true));
    }
    if bound == b"-" {
      return Err(Error::invalid_data(
        "ERR min or max not valid string range item",
      ));
    }
    if let Some(rest) = bound.strip_prefix(b"(") {
      Ok((rest.to_vec(), true, false))
    } else if let Some(rest) = bound.strip_prefix(b"[") {
      Ok((rest.to_vec(), false, false))
    } else {
      Err(Error::invalid_data(
        "ERR min or max not valid string range item",
      ))
    }
  }

  /// Parses generic Redis lexicographical boundary (supports "-", "+", "(abc", "[abc").
  /// 通用解析 Redis 字典序边界(支持 "-", "+", "(abc", "[abc")
  pub fn parse_bound(bound: &[u8]) -> Result<(Vec<u8>, bool, bool)> {
    if bound == b"-" {
      return Ok((Vec::new(), false, true));
    }
    if bound == b"+" {
      return Ok((Vec::new(), false, true));
    }
    if let Some(rest) = bound.strip_prefix(b"(") {
      Ok((rest.to_vec(), true, false))
    } else if let Some(rest) = bound.strip_prefix(b"[") {
      Ok((rest.to_vec(), false, false))
    } else {
      Err(Error::invalid_data(
        "ERR min or max not valid string range item",
      ))
    }
  }
}

/// Rank range specification (aligned with Apache Kvrocks RangeRank).
/// 排名范围规格(对标 Apache Kvrocks RangeRank)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RangeRank {
  pub start: i64,
  pub stop: i64,
  pub reversed: bool,
}

impl RangeRank {
  #[inline]
  pub fn new(start: i64, stop: i64) -> Self {
    Self {
      start,
      stop,
      reversed: false,
    }
  }

  #[inline]
  pub fn rev(start: i64, stop: i64) -> Self {
    Self {
      start,
      stop,
      reversed: true,
    }
  }
}

/// ZRANGE command options enumeration.
/// ZRANGE 选项枚举(统一 *Opt 后缀)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZRange {
  ByScore,
  ByLex,
  Rev,
  WithScores,
  Limit(usize, usize),
}

use std::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

pub trait IntoRangeScore {
  fn into_range_score(self) -> RangeScore;
}

impl IntoRangeScore for RangeScore {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    self
  }
}

impl IntoRangeScore for &RangeScore {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    *self
  }
}

impl IntoRangeScore for (f64, f64) {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    RangeScore {
      min: self.0,
      max: self.1,
      minex: false,
      maxex: false,
      offset: 0,
      count: None,
    }
  }
}

impl IntoRangeScore for Range<f64> {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    RangeScore {
      min: self.start,
      max: self.end,
      minex: false,
      maxex: true,
      offset: 0,
      count: None,
    }
  }
}

impl IntoRangeScore for RangeInclusive<f64> {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    RangeScore {
      min: *self.start(),
      max: *self.end(),
      minex: false,
      maxex: false,
      offset: 0,
      count: None,
    }
  }
}

impl IntoRangeScore for RangeFrom<f64> {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    RangeScore {
      min: self.start,
      max: f64::INFINITY,
      minex: false,
      maxex: false,
      offset: 0,
      count: None,
    }
  }
}

impl IntoRangeScore for RangeTo<f64> {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    RangeScore {
      min: f64::NEG_INFINITY,
      max: self.end,
      minex: false,
      maxex: true,
      offset: 0,
      count: None,
    }
  }
}

impl IntoRangeScore for RangeToInclusive<f64> {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    RangeScore {
      min: f64::NEG_INFINITY,
      max: self.end,
      minex: false,
      maxex: false,
      offset: 0,
      count: None,
    }
  }
}

impl IntoRangeScore for RangeFull {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    RangeScore {
      min: f64::NEG_INFINITY,
      max: f64::INFINITY,
      minex: false,
      maxex: false,
      offset: 0,
      count: None,
    }
  }
}

impl IntoRangeScore for (Bound<f64>, Bound<f64>) {
  #[inline]
  fn into_range_score(self) -> RangeScore {
    let (min, minex) = match self.0 {
      Bound::Included(v) => (v, false),
      Bound::Excluded(v) => (v, true),
      Bound::Unbounded => (f64::NEG_INFINITY, false),
    };
    let (max, maxex) = match self.1 {
      Bound::Included(v) => (v, false),
      Bound::Excluded(v) => (v, true),
      Bound::Unbounded => (f64::INFINITY, false),
    };
    RangeScore {
      min,
      max,
      minex,
      maxex,
      offset: 0,
      count: None,
    }
  }
}

pub trait IntoRangeLex {
  fn into_range_lex(self) -> RangeLex;
}

impl IntoRangeLex for RangeLex {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    self
  }
}

impl IntoRangeLex for &RangeLex {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    self.clone()
  }
}

impl IntoRangeLex for (&[u8], &[u8]) {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    RangeLex {
      min: self.0.to_vec(),
      max: self.1.to_vec(),
      minex: false,
      maxex: false,
      min_infinite: false,
      max_infinite: false,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl IntoRangeLex for (&str, &str) {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    RangeLex {
      min: self.0.as_bytes().to_vec(),
      max: self.1.as_bytes().to_vec(),
      minex: false,
      maxex: false,
      min_infinite: false,
      max_infinite: false,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl IntoRangeLex for (Vec<u8>, Vec<u8>) {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    RangeLex {
      min: self.0,
      max: self.1,
      minex: false,
      maxex: false,
      min_infinite: false,
      max_infinite: false,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl<B: AsRef<[u8]>> IntoRangeLex for Range<B> {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    RangeLex {
      min: self.start.as_ref().to_vec(),
      max: self.end.as_ref().to_vec(),
      minex: false,
      maxex: true,
      min_infinite: false,
      max_infinite: false,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl<B: AsRef<[u8]>> IntoRangeLex for RangeInclusive<B> {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    let (start, end) = self.into_inner();
    RangeLex {
      min: start.as_ref().to_vec(),
      max: end.as_ref().to_vec(),
      minex: false,
      maxex: false,
      min_infinite: false,
      max_infinite: false,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl<B: AsRef<[u8]>> IntoRangeLex for RangeFrom<B> {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    RangeLex {
      min: self.start.as_ref().to_vec(),
      max: Vec::new(),
      minex: false,
      maxex: false,
      min_infinite: false,
      max_infinite: true,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl<B: AsRef<[u8]>> IntoRangeLex for RangeTo<B> {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    RangeLex {
      min: Vec::new(),
      max: self.end.as_ref().to_vec(),
      minex: false,
      maxex: true,
      min_infinite: true,
      max_infinite: false,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl<B: AsRef<[u8]>> IntoRangeLex for RangeToInclusive<B> {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    RangeLex {
      min: Vec::new(),
      max: self.end.as_ref().to_vec(),
      minex: false,
      maxex: false,
      min_infinite: true,
      max_infinite: false,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl IntoRangeLex for RangeFull {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    RangeLex {
      min: Vec::new(),
      max: Vec::new(),
      minex: false,
      maxex: false,
      min_infinite: true,
      max_infinite: true,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

impl<B: AsRef<[u8]>> IntoRangeLex for (Bound<B>, Bound<B>) {
  #[inline]
  fn into_range_lex(self) -> RangeLex {
    let (min, minex, min_infinite) = match self.0 {
      Bound::Included(v) => (v.as_ref().to_vec(), false, false),
      Bound::Excluded(v) => (v.as_ref().to_vec(), true, false),
      Bound::Unbounded => (Vec::new(), false, true),
    };
    let (max, maxex, max_infinite) = match self.1 {
      Bound::Included(v) => (v.as_ref().to_vec(), false, false),
      Bound::Excluded(v) => (v.as_ref().to_vec(), true, false),
      Bound::Unbounded => (Vec::new(), false, true),
    };
    RangeLex {
      min,
      max,
      minex,
      maxex,
      min_infinite,
      max_infinite,
      offset: 0,
      count: None,
      reversed: false,
    }
  }
}

pub trait IntoRangeRank {
  fn into_range_rank(self) -> RangeRank;
}

impl IntoRangeRank for RangeRank {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    self
  }
}

impl IntoRangeRank for &RangeRank {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    *self
  }
}

impl IntoRangeRank for (i64, i64) {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    RangeRank {
      start: self.0,
      stop: self.1,
      reversed: false,
    }
  }
}

impl IntoRangeRank for Range<i64> {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    RangeRank {
      start: self.start,
      stop: self.end.saturating_sub(1),
      reversed: false,
    }
  }
}

impl IntoRangeRank for RangeInclusive<i64> {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    RangeRank {
      start: *self.start(),
      stop: *self.end(),
      reversed: false,
    }
  }
}

impl IntoRangeRank for RangeFrom<i64> {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    RangeRank {
      start: self.start,
      stop: -1,
      reversed: false,
    }
  }
}

impl IntoRangeRank for RangeTo<i64> {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    RangeRank {
      start: 0,
      stop: self.end.saturating_sub(1),
      reversed: false,
    }
  }
}

impl IntoRangeRank for RangeToInclusive<i64> {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    RangeRank {
      start: 0,
      stop: self.end,
      reversed: false,
    }
  }
}

impl IntoRangeRank for RangeFull {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    RangeRank {
      start: 0,
      stop: -1,
      reversed: false,
    }
  }
}

impl IntoRangeRank for &(i64, i64) {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    RangeRank {
      start: self.0,
      stop: self.1,
      reversed: false,
    }
  }
}

impl IntoRangeRank for (Bound<i64>, Bound<i64>) {
  #[inline]
  fn into_range_rank(self) -> RangeRank {
    let start = match self.0 {
      Bound::Included(v) => v,
      Bound::Excluded(v) => v.saturating_add(1),
      Bound::Unbounded => 0,
    };
    let stop = match self.1 {
      Bound::Included(v) => v,
      Bound::Excluded(v) => v.saturating_sub(1),
      Bound::Unbounded => -1,
    };
    RangeRank {
      start,
      stop,
      reversed: false,
    }
  }
}