-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stackable-versioned): Improve action chain generation (#784)
* feat(stackable-versioned): Improve action chain generation * Add Neighbor trait * Start to implement more robust version lookup * Finish version lookup * Add deprecation note validation * Update PR links in changelog * Fix PR number in changelog link * Adjust TODO about optional deprecation note * Move neighbor code into own file * Add comment on how to expand generated code * Update Cargo.lock file
- Loading branch information
Showing
9 changed files
with
210 additions
and
113 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::{collections::BTreeMap, ops::Bound}; | ||
|
||
pub(crate) trait Neighbors<K, V> | ||
where | ||
K: Ord + Eq, | ||
{ | ||
fn get_neighbors(&self, key: &K) -> (Option<&V>, Option<&V>); | ||
|
||
fn lo_bound(&self, bound: Bound<&K>) -> Option<(&K, &V)>; | ||
fn up_bound(&self, bound: Bound<&K>) -> Option<(&K, &V)>; | ||
} | ||
|
||
impl<K, V> Neighbors<K, V> for BTreeMap<K, V> | ||
where | ||
K: Ord + Eq, | ||
{ | ||
fn get_neighbors(&self, key: &K) -> (Option<&V>, Option<&V>) { | ||
// NOTE (@Techassi): These functions might get added to the standard | ||
// library at some point. If that's the case, we can use the ones | ||
// provided by the standard lib. | ||
// See: https://github.com/rust-lang/rust/issues/107540 | ||
match ( | ||
self.lo_bound(Bound::Excluded(key)), | ||
self.up_bound(Bound::Excluded(key)), | ||
) { | ||
(Some((k, v)), None) => { | ||
if key > k { | ||
(Some(v), None) | ||
} else { | ||
(self.lo_bound(Bound::Excluded(k)).map(|(_, v)| v), None) | ||
} | ||
} | ||
(None, Some((k, v))) => { | ||
if key < k { | ||
(None, Some(v)) | ||
} else { | ||
(None, self.up_bound(Bound::Excluded(k)).map(|(_, v)| v)) | ||
} | ||
} | ||
(Some((_, lo)), Some((_, up))) => (Some(lo), Some(up)), | ||
(None, None) => unreachable!(), | ||
} | ||
} | ||
|
||
fn lo_bound(&self, bound: Bound<&K>) -> Option<(&K, &V)> { | ||
self.range((Bound::Unbounded, bound)).next_back() | ||
} | ||
|
||
fn up_bound(&self, bound: Bound<&K>) -> Option<(&K, &V)> { | ||
self.range((bound, Bound::Unbounded)).next() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use rstest::rstest; | ||
|
||
#[rstest] | ||
#[case(0, (None, Some(&"test1")))] | ||
#[case(1, (None, Some(&"test3")))] | ||
#[case(2, (Some(&"test1"), Some(&"test3")))] | ||
#[case(3, (Some(&"test1"), None))] | ||
#[case(4, (Some(&"test3"), None))] | ||
fn test(#[case] key: i32, #[case] expected: (Option<&&str>, Option<&&str>)) { | ||
let map = BTreeMap::from([(1, "test1"), (3, "test3")]); | ||
let neigbors = map.get_neighbors(&key); | ||
|
||
assert_eq!(neigbors, expected); | ||
} | ||
} |
Oops, something went wrong.