Skip to content

Commit

Permalink
rustdoc: json: add support for variances
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Mar 30, 2024
1 parent 7406e00 commit 3781173
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,11 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
use clean::GenericParamDefKind::*;
match kind {
Lifetime(param) => GenericParamDefKind::Lifetime {
variance: param.variance.map(|var| var.into_tcx(tcx)),
outlives: param.outlives.into_iter().map(convert_lifetime).collect(),
},
Type(param) => GenericParamDefKind::Type {
variance: param.variance.map(|var| var.into_tcx(tcx)),
bounds: param.bounds.into_tcx(tcx),
default: param.default.map(|ty| ty.into_tcx(tcx)),
synthetic: param.synthetic,
Expand All @@ -468,6 +470,17 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
}
}

impl FromWithTcx<ty::Variance> for Variance {
fn from_tcx(variance: ty::Variance, _tcx: TyCtxt<'_>) -> Self {
match variance {
ty::Variance::Covariant => Self::Covariant,
ty::Variance::Invariant => Self::Invariant,
ty::Variance::Contravariant => Self::Contravariant,
ty::Variance::Bivariant => Self::Bivariant,
}
}
}

impl FromWithTcx<clean::WherePredicate> for WherePredicate {
fn from_tcx(predicate: clean::WherePredicate, tcx: TyCtxt<'_>) -> Self {
use clean::WherePredicate::*;
Expand Down
12 changes: 12 additions & 0 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,11 @@ pub struct GenericParamDef {
#[serde(rename_all = "snake_case")]
pub enum GenericParamDefKind {
Lifetime {
variance: Option<Variance>,
outlives: Vec<String>,
},
Type {
variance: Option<Variance>,
bounds: Vec<GenericBound>,
default: Option<Type>,
/// This is normally `false`, which means that this generic parameter is
Expand Down Expand Up @@ -480,6 +482,16 @@ pub enum GenericParamDefKind {
},
}

// FIXME(fmease): docs
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Variance {
Covariant,
Invariant,
Contravariant,
Bivariant,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WherePredicate {
Expand Down

0 comments on commit 3781173

Please sign in to comment.