-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move bounded_traversal from mononoke directory to common/rust/shed
Summary: I'd like to make use of the `bounded_traversal` crate outside of mononoke's codebase to limit memory usage. Reviewed By: mitrandir77, liubov-dmitrieva Differential Revision: D70098058 fbshipit-source-id: 3f0cf6d007ef7bc3d37b4bbf4ecc28b62005708d
- Loading branch information
1 parent
15b8f94
commit cfe000f
Showing
13 changed files
with
3,220 additions
and
0 deletions.
There are no files selected for viewing
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,26 @@ | ||
# @generated by autocargo from //common/rust/shed/bounded_traversal:bounded_traversal | ||
|
||
[package] | ||
name = "bounded_traversal" | ||
version = "0.1.0" | ||
authors = ["Facebook <opensource+rust-shed@fb.com>"] | ||
edition = "2021" | ||
readme = "../../README.md" | ||
repository = "https://github.com/facebookexperimental/rust-shed" | ||
license = "MIT OR Apache-2.0" | ||
|
||
[dependencies] | ||
either = "1.5" | ||
futures = { version = "0.3.30", features = ["async-await", "compat"] } | ||
smallvec = { version = "1.13.2", features = ["serde", "specialization", "union"] } | ||
thiserror = "2" | ||
|
||
[dev-dependencies] | ||
anyhow = "1.0.95" | ||
cloned = { version = "0.1.0", path = "../cloned" } | ||
lock_ext = { version = "0.1.0", path = "../lock_ext" } | ||
maplit = "1.0" | ||
pretty_assertions = { version = "1.2", features = ["alloc"], default-features = false } | ||
quickcheck = "1.0" | ||
quickcheck_async = "0.1.1" | ||
tokio = { version = "1.41.0", features = ["full", "test-util", "tracing"] } |
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 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under both the MIT license found in the | ||
* LICENSE-MIT file in the root directory of this source tree and the Apache | ||
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory | ||
* of this source tree. | ||
*/ | ||
|
||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::Context; | ||
use std::task::Poll; | ||
|
||
use either::Either; | ||
use futures::ready; | ||
|
||
/// Return value from `unfold` callbacks for ordered traversals. Each element | ||
/// in the unfolded vector can be either an item to output from the traversal, | ||
/// or a recursive step with an associated weight. | ||
/// | ||
/// The associated weight should be an estimate of the number of eventual | ||
/// output items this recursive step will expand into. | ||
pub enum OrderedTraversal<Out, In> { | ||
Output(Out), | ||
Recurse(usize, In), | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
pub(crate) struct NodeLocation<Index> { | ||
pub node_index: Index, // node index inside execution tree | ||
pub child_index: usize, // index inside parents children list | ||
} | ||
|
||
impl<Index> NodeLocation<Index> { | ||
pub fn new(node_index: Index, child_index: usize) -> Self { | ||
NodeLocation { | ||
node_index, | ||
child_index, | ||
} | ||
} | ||
} | ||
|
||
/// Equivalent of `futures::future::Either` but with heterogeneous output | ||
/// types using `either::Either`. | ||
#[must_use = "futures do nothing unless you `.await` or poll them"] | ||
pub(crate) enum Either2<A, B> { | ||
Left(A), | ||
Right(B), | ||
} | ||
|
||
impl<A, B> Future for Either2<A, B> | ||
where | ||
A: Future, | ||
B: Future, | ||
{ | ||
type Output = Either<A::Output, B::Output>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { | ||
// see `impl<Left, Right> Future for Either<Left, Right>` | ||
unsafe { | ||
let result = match self.get_unchecked_mut() { | ||
Either2::Left(future) => Either::Left(ready!(Pin::new_unchecked(future).poll(cx))), | ||
Either2::Right(future) => { | ||
Either::Right(ready!(Pin::new_unchecked(future).poll(cx))) | ||
} | ||
}; | ||
Poll::Ready(result) | ||
} | ||
} | ||
} |
Oops, something went wrong.