Skip to content

Indexes 2: Add new_unchecked() constructors to spk schema objects#1337

Open
dcookspi wants to merge 2 commits intoindex-1-package-trait-changesfrom
index-2-new-unchecked-additions
Open

Indexes 2: Add new_unchecked() constructors to spk schema objects#1337
dcookspi wants to merge 2 commits intoindex-1-package-trait-changesfrom
index-2-new-unchecked-additions

Conversation

@dcookspi
Copy link
Copy Markdown
Collaborator

@dcookspi dcookspi commented Mar 19, 2026

Note: for info on benefits of indexing for spk solves see #1340 (5 of 5). Maybe start there and work back down to this PR if you prefer to review PRs top down.

This adds new_unchecked() constructor methods to various spk schema objects. These allow direct creation of those objects from other existing object pieces, e.g. from other pieces of those objects in index data. This is one of the changes that supports adding indexes and index based packages to Spk repositories. It allows indexes to avoid reparsing data from text for some objects.

This is 2 of 5 chained PRs for adding indexes to spk solves:

  1. Indexes 1: Change Package and related traits to not return references to fields #1336
  2. this PR
  3. Indexes 3: Adds flatbuffers schema and SolverPackageSpec for indexes to spk #1338
  4. Indexes 4: Adds Indexes for SPK repositories #1339
  5. Indexes 5: Adds spk repo index subcommand for index generation and updates #1340

@dcookspi dcookspi self-assigned this Mar 19, 2026
@dcookspi dcookspi added SPI AOI Area of interest for SPI pr-chain This PR doesn't target the main branch, don't merge! labels Mar 19, 2026
Comment on lines +58 to +62
// TODO: CompatRules that allow ::None are used in Compat
// structs. CompatRules that do not allow ::None are used in
// required_compat's in Requests. They should be separate types,
// perhaps one wrapping the other, to clarify where ::None is and is
// not a valid value.
Copy link
Copy Markdown
Collaborator Author

@dcookspi dcookspi Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refers to something that is worked around in later Indexing changes. But it could also be something that gets addressed in future, by separating the two uses into distinct types.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the idea of there being a newtype that wraps the other and giving them their own distinct Display implementation, to get away from the "magic" alternate() mechanism.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turned this into an issue #1347

}

// Allow tests to manufacture owned instances with known good values.
// Allow tests and indexes to manufacture owned instances with known good values.
Copy link
Copy Markdown
Collaborator Author

@dcookspi dcookspi Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No new_checked() methods were added for these String based types here. But in a later indexes PRs there are several uses of unsafe { ... } around an existing method on these types. It may be we want to fold those into a new_unchecked() method for non-test cases use.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big picture I think we want to be able to say that values in the index were already validated before put into the index and therefore should be trusted without having to re-validate them when reading the index.

However we need a way to introduce changes to spk that may change validation rules. For example let's say we stop allowing '-' in package names. How do we want to put some kind of version number in the index metadata so at runtime the spk process can check that the index conforms to its expectations?

I'm interested in having a way to validate the whole index at once, in a sense, instead of having to validate every individual value found in the index.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added unsafe's to the new_checked methods and updated the callers to use unsafe blocks around them.

Validating the index will be addressed in PR3 (#1338).

@dcookspi dcookspi added the enhancement New feature or request label Mar 19, 2026
@dcookspi dcookspi requested review from jrray and rydrman March 19, 2026 22:53
Copy link
Copy Markdown
Collaborator

@jrray jrray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is gonna hurt...

}

// Allow tests to manufacture owned instances with known good values.
// Allow tests and indexes to manufacture owned instances with known good values.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big picture I think we want to be able to say that values in the index were already validated before put into the index and therefore should be trusted without having to re-validate them when reading the index.

However we need a way to introduce changes to spk that may change validation rules. For example let's say we stop allowing '-' in package names. How do we want to put some kind of version number in the index metadata so at runtime the spk process can check that the index conforms to its expectations?

I'm interested in having a way to validate the whole index at once, in a sense, instead of having to validate every individual value found in the index.

Comment on lines +58 to +62
// TODO: CompatRules that allow ::None are used in Compat
// structs. CompatRules that do not allow ::None are used in
// required_compat's in Requests. They should be separate types,
// perhaps one wrapping the other, to clarify where ::None is and is
// not a valid value.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the idea of there being a newtype that wraps the other and giving them their own distinct Display implementation, to get away from the "magic" alternate() mechanism.

@dcookspi dcookspi force-pushed the index-1-package-trait-changes branch from fb6e0dd to 9bd9685 Compare March 26, 2026 23:06
These allow for directly creating those objects from other existing
object pieces, e.g. from index data objects.

Signed-off-by: David Gilligan-Cook <dcook@imageworks.com>
@dcookspi dcookspi force-pushed the index-2-new-unchecked-additions branch from a59523b to 5c8e4db Compare March 27, 2026 00:48
Signed-off-by: David Gilligan-Cook <dcook@imageworks.com>
Comment on lines +704 to +707
/// # Safety
///
/// The caller must ensure the string parses as a valid compat.
pub unsafe fn new_unchecked(compat: &str) -> Result<Self> {
Copy link
Copy Markdown
Collaborator

@jrray jrray Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses from_str and returns a Result. Is it really unsafe? It has to successfully parse so I don't think using this skips any validation.

/// The caller must make sure the string can be parsed as a valid
/// Version.
pub unsafe fn new_unchecked(version_str: &str) -> Result<Self> {
Version::try_from(version_str)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thought here, really unsafe?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add to this, the followup question would be could the current users of Version::new_unchecked just switch to Version::try_from and then this constructor is not needed?

Comment on lines +112 to +113
/// The caller must make sure the pieces combine to make a valid
/// EmbeddedPackageSpec.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the need to say that this safety message (and the others) amount to saying "you have responsibilities to use this correctly" but without giving any details on how to use it correctly. I get it that you don't necessarily know what those are. I couldn't tell you what they are right now on the spot either.

///
/// # Safety
///
/// The caller must make sure the requests are valid and the they
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The caller must make sure the requests are valid and the they
/// The caller must make sure the requests are valid and they

Copy link
Copy Markdown
Collaborator

@jrray jrray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, and maybe some things are unsafe that don't have to be.

I'm not expecting you to update the safety statements but can you create an issue for it to acknowledge it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pr-chain This PR doesn't target the main branch, don't merge! SPI AOI Area of interest for SPI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants