-
Notifications
You must be signed in to change notification settings - Fork 2.2k
chore(lockfiles) avoid hand written reflection #6143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #![feature(trait_upcasting)] | ||
| #![deny(clippy::all)] | ||
|
|
||
| mod berry; | ||
|
|
@@ -7,7 +8,10 @@ mod npm; | |
| mod pnpm; | ||
| mod yarn1; | ||
|
|
||
| use std::collections::{HashMap, HashSet}; | ||
| use std::{ | ||
| any::Any, | ||
| collections::{HashMap, HashSet}, | ||
| }; | ||
|
|
||
| pub use berry::{Error as BerryError, *}; | ||
| pub use bun::BunLockfile; | ||
|
|
@@ -27,7 +31,7 @@ pub struct Package { | |
| // This trait will only be used when migrating the Go lockfile implementations | ||
| // to Rust. Once the migration is complete we will leverage petgraph for doing | ||
| // our graph calculations. | ||
| pub trait Lockfile: Send + Sync { | ||
| pub trait Lockfile: Send + Sync + Any { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a restriction on the trait, but |
||
| // Given a workspace, a package it imports and version returns the key, resolved | ||
| // version, and if it was found | ||
| fn resolve_package( | ||
|
|
@@ -53,13 +57,8 @@ pub trait Lockfile: Send + Sync { | |
| Ok(Vec::new()) | ||
| } | ||
|
|
||
| /// Present a global change key which is compared against two lockfiles | ||
| /// | ||
| /// Impl notes: please prefix this key with some magic identifier | ||
| /// to prevent clashes. we are not worried about inter-version | ||
| /// compatibility so these keys don't need to be stable. They are | ||
| /// ephemeral. | ||
| fn global_change_key(&self) -> Vec<u8>; | ||
| /// Determine if there's a global change between two lockfiles | ||
| fn global_change(&self, other: &dyn Lockfile) -> bool; | ||
| } | ||
|
|
||
| /// Takes a lockfile, and a map of workspace directory paths -> (package name, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| use std::borrow::Cow; | ||
| use std::{any::Any, borrow::Cow, collections::BTreeMap}; | ||
|
|
||
| use serde::{Deserialize, Serialize}; | ||
| use serde_json::json; | ||
| use turbopath::RelativeUnixPathBuf; | ||
|
|
||
| use super::{dep_path::DepPath, Error, LockfileVersion}; | ||
|
|
@@ -252,6 +251,27 @@ impl PnpmLockfile { | |
| } | ||
| Ok(pruned_patches) | ||
| } | ||
|
|
||
| // Create a projection of all fields in the lockfile that could affect all | ||
| // workspaces | ||
| fn global_fields(&self) -> GlobalFields { | ||
| GlobalFields { | ||
| version: &self.lockfile_version.version, | ||
| checksum: self.package_extensions_checksum.as_deref(), | ||
| overrides: self.overrides.as_ref(), | ||
| patched_dependencies: self.patched_dependencies.as_ref(), | ||
| settings: self.settings.as_ref(), | ||
| } | ||
| } | ||
|
Comment on lines
+257
to
+265
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something I took from the global cache key as I find |
||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Eq)] | ||
| struct GlobalFields<'a> { | ||
| version: &'a str, | ||
| checksum: Option<&'a str>, | ||
| overrides: Option<&'a BTreeMap<String, String>>, | ||
| patched_dependencies: Option<&'a BTreeMap<String, PatchFile>>, | ||
| settings: Option<&'a LockfileSettings>, | ||
| } | ||
|
|
||
| impl crate::Lockfile for PnpmLockfile { | ||
|
|
@@ -401,22 +421,13 @@ impl crate::Lockfile for PnpmLockfile { | |
| Ok(patches) | ||
| } | ||
|
|
||
| fn global_change_key(&self) -> Vec<u8> { | ||
| let mut buf = vec![b'p', b'n', b'p', b'm', 0]; | ||
|
|
||
| serde_json::to_writer( | ||
| &mut buf, | ||
| &json!({ | ||
| "version": self.lockfile_version.version, | ||
| "checksum": self.package_extensions_checksum, | ||
| "overrides": self.overrides, | ||
| "patched_deps": self.patched_dependencies, | ||
| "settings": self.settings, | ||
| }), | ||
| ) | ||
| .expect("writing to Vec cannot fail"); | ||
|
|
||
| buf | ||
| fn global_change(&self, other: &dyn crate::Lockfile) -> bool { | ||
| let any_other = other as &dyn Any; | ||
| if let Some(other) = any_other.downcast_ref::<Self>() { | ||
| self.global_fields() != other.global_fields() | ||
| } else { | ||
| true | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Required in order to cast
&dyn Lockfileto&dyn Anyeven throughLockfile: Any(See supertraits)(This is only a gated feature at the moment because it does have effect the vtable memory size, but it has been deemed minor enough that it will be opt-out in the future)