Skip to content

Commit

Permalink
Moved to a custom Array type so we can automatically get length info
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Sep 17, 2024
1 parent d0f4a0a commit 5d1c76f
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions crates/turborepo-lib/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ impl Query {
}
}

#[derive(Debug, SimpleObject)]
struct Array<T: OutputType> {
items: Vec<T>,
length: usize,
}

unsafe impl<T: Send + OutputType> Send for Array<T> {}
unsafe impl<T: Sync + OutputType> Sync for Array<T> {}

impl<T: OutputType> FromIterator<T> for Array<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let items: Vec<_> = iter.into_iter().collect();
let length = items.len();
Self { items, length }
}
}

struct Package {
run: Arc<Run>,
name: PackageName,
Expand Down Expand Up @@ -293,7 +310,7 @@ impl Query {
&self,
base: Option<String>,
head: Option<String>,
) -> Result<Vec<Package>, Error> {
) -> Result<Array<Package>, Error> {
let mut opts = self.run.opts().clone();
opts.scope_opts.affected_range = Some((base, head));

Expand Down Expand Up @@ -322,7 +339,7 @@ impl Query {
}

/// Gets a list of packages that match the given filter
async fn packages(&self, filter: Option<PackagePredicate>) -> Result<Vec<Package>, Error> {
async fn packages(&self, filter: Option<PackagePredicate>) -> Result<Array<Package>, Error> {
let Some(filter) = filter else {
return Ok(self
.run
Expand Down Expand Up @@ -369,7 +386,7 @@ impl Package {
}

/// The upstream packages that have this package as a direct dependency
async fn direct_dependents(&self) -> Result<Vec<Package>, Error> {
async fn direct_dependents(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
Ok(self
.run
Expand All @@ -386,7 +403,7 @@ impl Package {
}

/// The downstream packages that directly depend on this package
async fn direct_dependencies(&self) -> Result<Vec<Package>, Error> {
async fn direct_dependencies(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
Ok(self
.run
Expand All @@ -402,7 +419,8 @@ impl Package {
.collect())
}

async fn all_dependents(&self) -> Result<Vec<Package>, Error> {
/// The downstream packages that depend on this package, transitively
async fn all_dependents(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
Ok(self
.run
Expand All @@ -417,7 +435,8 @@ impl Package {
.collect())
}

async fn all_dependencies(&self) -> Result<Vec<Package>, Error> {
/// The upstream packages that this package depends on, transitively
async fn all_dependencies(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
Ok(self
.run
Expand Down

0 comments on commit 5d1c76f

Please sign in to comment.