Skip to main content

mysql_handler/engine/
range_key.rs

1// Copyright (C) 2026 ren-yamanashi
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of this program hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, see <https://www.gnu.org/licenses/>.
22
23//! One endpoint of a range scan, mirroring MySQL's `key_range`.
24
25use crate::engine::RKeyFunction;
26
27/// One endpoint of a range scan, mirroring the relevant fields of MySQL's
28/// `key_range`. The shim resolves the original `key_part_map` to the leading
29/// key bytes before crossing the FFI boundary, so [`key`](Self::key) is already
30/// length-resolved; the borrow may not be retained past the callback that
31/// supplied it.
32///
33/// A range endpoint is optional at the call site — MySQL passes a null
34/// `key_range` for an open-ended bound — so the trait methods receive an
35/// `Option<RangeKey<'_>>` and `None` denotes "no bound on this side".
36#[derive(Debug, Clone, Copy)]
37#[non_exhaustive]
38pub struct RangeKey<'a> {
39    key: &'a [u8],
40    flag: RKeyFunction,
41}
42
43impl<'a> RangeKey<'a> {
44    pub(crate) fn new(key: &'a [u8], flag: RKeyFunction) -> Self {
45        Self { key, flag }
46    }
47
48    /// Leading key bytes that position the scan at this endpoint
49    #[must_use]
50    pub fn key(&self) -> &[u8] {
51        self.key
52    }
53
54    /// Search semantics MySQL attached to this endpoint
55    #[must_use]
56    pub fn flag(&self) -> RKeyFunction {
57        self.flag
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn exposes_key_and_flag() {
67        let key = [1u8, 2, 3];
68        let endpoint = RangeKey::new(&key, RKeyFunction::KeyOrNext);
69        assert_eq!(endpoint.key(), &key);
70        assert_eq!(endpoint.flag(), RKeyFunction::KeyOrNext);
71    }
72
73    #[test]
74    fn empty_key_is_preserved() {
75        let endpoint = RangeKey::new(&[], RKeyFunction::KeyExact);
76        assert!(endpoint.key().is_empty());
77    }
78}