Skip to main content

select_latest_for_existence

Function select_latest_for_existence 

Source
pub fn select_latest_for_existence<T>(
    versions: &[T],
    as_version: impl Fn(&T) -> &dyn Version,
) -> Option<usize>
Expand description

Index of the version an existence check should report as “latest”, ignoring req entirely.

This is the shared 3-rung fallback ladder used under a wildcard/empty requirement, where every version satisfies by definition and the question is only which one to prefer for display:

  1. The newest version that is neither flagged (RemovalStatus::is_flagged) nor a pre-release (Version::is_prerelease).
  2. Else, the newest version that does not block resolution (RemovalStatus::blocks_resolution) — an AdvisoryDeprecated version counts here.
  3. Else, index 0 unconditionally — the newest version overall, however it is flagged. A yanked-or-prerelease-only package still exists; this rung is what turns that case into “here is its newest version” instead of a false “Unknown package” (#347, #364).

versions must be sorted newest-first, as returned by Registry::get_versions. Returns None only when versions is empty.

§Requirement-blindness is deliberate and dangerous

This function takes no req parameter and does not check whether the caller is under a wildcard requirement — it always returns rung 3 as a last resort, regardless of what a concrete requirement might demand. It is only correct once the caller has already confirmed the requirement is a wildcard via is_existence_wildcard. Calling it ungated — e.g. under a concrete ^1.2 requirement — can return a version that does not satisfy that requirement at all, silently corrupting upgrade resolution.

§Examples

use deps_core::registry::{RemovalStatus, Version, select_latest_for_existence};
use deps_core::ConcreteVersion;
use std::any::Any;

struct MyVersion { version: ConcreteVersion, status: RemovalStatus, prerelease: bool }

impl Version for MyVersion {
    fn version_string(&self) -> &ConcreteVersion { &self.version }
    fn removal_status(&self) -> RemovalStatus { self.status }
    fn is_prerelease(&self) -> bool { self.prerelease }
    fn as_any(&self) -> &dyn Any { self }
}

// Newest version is yanked; rung 3 still returns it rather than `None`.
let versions = vec![
    MyVersion { version: "2.0.0".into(), status: RemovalStatus::Yanked, prerelease: false },
    MyVersion { version: "1.5.0".into(), status: RemovalStatus::Yanked, prerelease: false },
];

let idx = select_latest_for_existence(&versions, |v| v as &dyn Version);
assert_eq!(idx, Some(0));

assert_eq!(select_latest_for_existence::<MyVersion>(&[], |v| v as &dyn Version), None);