Skip to main content

dag/set/
legacy.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8use super::super::Dag;
9use super::IdStaticSet;
10use super::Set;
11use crate::IdSet;
12
13/// A legacy token that enables conversion between IdSet (id-based)
14/// and Set (hash-based). It should not be used for new Rust code.
15#[derive(Copy, Clone)]
16pub struct LegacyCodeNeedIdAccess;
17
18// This is ideally not provided.  However revision numbers in revset still have
19// large use-cases in Python and for now we provide this way to convert IdStaticSet
20// to IdSet using "revision" numbers.
21impl<'a> From<(LegacyCodeNeedIdAccess, &'a IdStaticSet)> for IdSet {
22    fn from(value: (LegacyCodeNeedIdAccess, &'a IdStaticSet)) -> IdSet {
23        let set = value.1;
24        // TODO: find users of this and potentially migrate them off the legacy access.
25        // It is unclear whether it is safe to lose iteration order here.
26        set.id_set_losing_order().clone()
27    }
28}
29
30impl<'a> From<(LegacyCodeNeedIdAccess, IdSet, &'a Dag)> for Set {
31    fn from(value: (LegacyCodeNeedIdAccess, IdSet, &'a Dag)) -> Set {
32        Set::from_id_set_dag(value.1, value.2).unwrap()
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use nonblocking::non_blocking_result as r;
39
40    use super::super::id_static::tests::with_dag;
41    use super::*;
42    use crate::tests::dbg;
43    use crate::DagAlgorithm;
44    use crate::Result;
45
46    #[test]
47    fn test_legacy_convert() -> Result<()> {
48        use LegacyCodeNeedIdAccess as L;
49        with_dag(|dag| -> Result<()> {
50            let set1 = r(dag.ancestors("G".into()))?;
51            let spans: IdSet = (L, set1.as_any().downcast_ref::<IdStaticSet>().unwrap()).into();
52            let set2: Set = (L, spans.clone(), dag).into();
53            assert_eq!(dbg(&set1), "<spans [E:G+4:6, A:B+0:1]>");
54            assert_eq!(dbg(&set2), "<spans [E:G+4:6, A:B+0:1]>");
55            assert_eq!(dbg(&spans), "0 1 4 5 6");
56            Ok(())
57        })
58    }
59}