Skip to content

Commit 3781173

Browse files
committed
rustdoc: json: add support for variances
1 parent 7406e00 commit 3781173

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/librustdoc/json/conversions.rs

+13
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,11 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
454454
use clean::GenericParamDefKind::*;
455455
match kind {
456456
Lifetime(param) => GenericParamDefKind::Lifetime {
457+
variance: param.variance.map(|var| var.into_tcx(tcx)),
457458
outlives: param.outlives.into_iter().map(convert_lifetime).collect(),
458459
},
459460
Type(param) => GenericParamDefKind::Type {
461+
variance: param.variance.map(|var| var.into_tcx(tcx)),
460462
bounds: param.bounds.into_tcx(tcx),
461463
default: param.default.map(|ty| ty.into_tcx(tcx)),
462464
synthetic: param.synthetic,
@@ -468,6 +470,17 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
468470
}
469471
}
470472

473+
impl FromWithTcx<ty::Variance> for Variance {
474+
fn from_tcx(variance: ty::Variance, _tcx: TyCtxt<'_>) -> Self {
475+
match variance {
476+
ty::Variance::Covariant => Self::Covariant,
477+
ty::Variance::Invariant => Self::Invariant,
478+
ty::Variance::Contravariant => Self::Contravariant,
479+
ty::Variance::Bivariant => Self::Bivariant,
480+
}
481+
}
482+
}
483+
471484
impl FromWithTcx<clean::WherePredicate> for WherePredicate {
472485
fn from_tcx(predicate: clean::WherePredicate, tcx: TyCtxt<'_>) -> Self {
473486
use clean::WherePredicate::*;

src/rustdoc-json-types/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,11 @@ pub struct GenericParamDef {
443443
#[serde(rename_all = "snake_case")]
444444
pub enum GenericParamDefKind {
445445
Lifetime {
446+
variance: Option<Variance>,
446447
outlives: Vec<String>,
447448
},
448449
Type {
450+
variance: Option<Variance>,
449451
bounds: Vec<GenericBound>,
450452
default: Option<Type>,
451453
/// This is normally `false`, which means that this generic parameter is
@@ -480,6 +482,16 @@ pub enum GenericParamDefKind {
480482
},
481483
}
482484

485+
// FIXME(fmease): docs
486+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
487+
#[serde(rename_all = "snake_case")]
488+
pub enum Variance {
489+
Covariant,
490+
Invariant,
491+
Contravariant,
492+
Bivariant,
493+
}
494+
483495
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
484496
#[serde(rename_all = "snake_case")]
485497
pub enum WherePredicate {

0 commit comments

Comments
 (0)