Skip to content

Commit 97407c9

Browse files
committed
refactor(source): Switch PathSource from Vec to Option
1 parent 2b8c620 commit 97407c9

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/cargo/sources/path.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct PathSource<'gctx> {
3333
/// Whether this source has updated all package information it may contain.
3434
updated: bool,
3535
/// Packages that this sources has discovered.
36-
packages: Vec<Package>,
36+
package: Option<Package>,
3737
gctx: &'gctx GlobalContext,
3838
}
3939

@@ -47,7 +47,7 @@ impl<'gctx> PathSource<'gctx> {
4747
source_id,
4848
path: path.to_path_buf(),
4949
updated: false,
50-
packages: Vec::new(),
50+
package: None,
5151
gctx,
5252
}
5353
}
@@ -61,7 +61,7 @@ impl<'gctx> PathSource<'gctx> {
6161
source_id,
6262
path,
6363
updated: true,
64-
packages: vec![pkg],
64+
package: Some(pkg),
6565
gctx,
6666
}
6767
}
@@ -72,7 +72,7 @@ impl<'gctx> PathSource<'gctx> {
7272

7373
self.update()?;
7474

75-
match self.packages.iter().find(|p| p.root() == &*self.path) {
75+
match &self.package {
7676
Some(pkg) => Ok(pkg.clone()),
7777
None => Err(internal(format!(
7878
"no package found in source {:?}",
@@ -85,14 +85,19 @@ impl<'gctx> PathSource<'gctx> {
8585
/// filesystem if package information haven't yet updated.
8686
pub fn read_packages(&self) -> CargoResult<Vec<Package>> {
8787
if self.updated {
88-
Ok(self.packages.clone())
88+
Ok(self.package.clone().into_iter().collect())
8989
} else {
90-
let path = self.path.join("Cargo.toml");
91-
let pkg = ops::read_package(&path, self.source_id, self.gctx)?;
90+
let pkg = self.read_package()?;
9291
Ok(vec![pkg])
9392
}
9493
}
9594

95+
fn read_package(&self) -> CargoResult<Package> {
96+
let path = self.path.join("Cargo.toml");
97+
let pkg = ops::read_package(&path, self.source_id, self.gctx)?;
98+
Ok(pkg)
99+
}
100+
96101
/// List all files relevant to building this package inside this source.
97102
///
98103
/// This function will use the appropriate methods to determine the
@@ -126,8 +131,7 @@ impl<'gctx> PathSource<'gctx> {
126131
/// Discovers packages inside this source if it hasn't yet done.
127132
pub fn update(&mut self) -> CargoResult<()> {
128133
if !self.updated {
129-
let packages = self.read_packages()?;
130-
self.packages.extend(packages.into_iter());
134+
self.package = Some(self.read_package()?);
131135
self.updated = true;
132136
}
133137

@@ -149,7 +153,7 @@ impl<'gctx> Source for PathSource<'gctx> {
149153
f: &mut dyn FnMut(IndexSummary),
150154
) -> Poll<CargoResult<()>> {
151155
self.update()?;
152-
for s in self.packages.iter().map(|p| p.summary()) {
156+
if let Some(s) = self.package.as_ref().map(|p| p.summary()) {
153157
let matched = match kind {
154158
QueryKind::Exact => dep.matches(s),
155159
QueryKind::Alternatives => true,
@@ -177,7 +181,7 @@ impl<'gctx> Source for PathSource<'gctx> {
177181
fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> {
178182
trace!("getting packages; id={}", id);
179183
self.update()?;
180-
let pkg = self.packages.iter().find(|pkg| pkg.package_id() == id);
184+
let pkg = self.package.iter().find(|pkg| pkg.package_id() == id);
181185
pkg.cloned()
182186
.map(MaybePackage::Ready)
183187
.ok_or_else(|| internal(format!("failed to find {} in path source", id)))

0 commit comments

Comments
 (0)