Skip to content

Commit df90691

Browse files
committed
Add from docs for tools
1 parent 3b022d8 commit df90691

File tree

23 files changed

+96
-1
lines changed

23 files changed

+96
-1
lines changed

src/tools/clippy/clippy_lints/src/attrs/mixed_attributes_style.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ enum SimpleAttrKind {
1515
}
1616

1717
impl From<&AttrKind> for SimpleAttrKind {
18+
/// Convert an `AttrKind` to a `SimpleAttrKind`, if it's a `DocComment` then `Doc` is returned with no conversion.
19+
///
20+
/// ## Cost
21+
/// If `AttrKind` is `DocComment` it's free, however if it's `Normal` their is heep allocation
1822
fn from(value: &AttrKind) -> Self {
1923
match value {
2024
AttrKind::Normal(attr) => {

src/tools/miri/src/concurrency/thread.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl Idx for ThreadId {
8080
}
8181

8282
impl From<ThreadId> for u64 {
83+
/// Return inner `u32` converted to `u64`
8384
fn from(t: ThreadId) -> Self {
8485
t.0.into()
8586
}

src/tools/miri/src/concurrency/vector_clock.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl Idx for VectorIdx {
3434
}
3535

3636
impl From<u32> for VectorIdx {
37+
/// Create new `VectorIdx` with `u32` as inner
3738
#[inline]
3839
fn from(id: u32) -> Self {
3940
Self(id)

src/tools/miri/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ extern crate rustc_middle;
7474
extern crate rustc_session;
7575
extern crate rustc_span;
7676
extern crate rustc_target;
77-
// Linking `rustc_driver` pulls in the required object code as the rest of the rustc crates are
77+
// Linking `rustc_driver` pulls in the required object code as the rest of the rustc crates are
7878
// shipped only as rmeta files.
7979
#[allow(unused_extern_crates)]
8080
extern crate rustc_driver;

src/tools/miri/src/shims/io_error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ pub enum IoError {
1414
pub use self::IoError::*;
1515

1616
impl From<io::Error> for IoError {
17+
/// Wrap `io::Error` in `HostError`
1718
fn from(value: io::Error) -> Self {
1819
IoError::HostError(value)
1920
}
2021
}
2122

2223
impl From<io::ErrorKind> for IoError {
24+
/// Convert a `io::ErrorKind` to a `io::Error` then wrap in `HostError`
2325
fn from(value: io::ErrorKind) -> Self {
2426
IoError::HostError(value.into())
2527
}
2628
}
2729

2830
impl From<Scalar> for IoError {
31+
/// Wrap `Scalar` in `Raw`
2932
fn from(value: Scalar) -> Self {
3033
IoError::Raw(value)
3134
}

src/tools/rust-analyzer/crates/base-db/src/input.rs

+10
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ pub enum LangCrateOrigin {
173173
}
174174

175175
impl From<&str> for LangCrateOrigin {
176+
/// Match a string slice for "alloc", "core", "proc-macro", "proc_macro", "std", "test",
177+
/// mapping them to variants of the same name; if none match then it's `Other`
176178
fn from(s: &str) -> Self {
177179
match s {
178180
"alloc" => LangCrateOrigin::Alloc,
@@ -217,6 +219,10 @@ impl CrateDisplayName {
217219
}
218220

219221
impl From<CrateName> for CrateDisplayName {
222+
/// Creates a `CrateDisplayName` from `crate_name` and `crate_name.0`(inner)
223+
///
224+
/// ## Cost
225+
/// This clones `CrateName`
220226
fn from(crate_name: CrateName) -> CrateDisplayName {
221227
let canonical_name = crate_name.0.clone();
222228
CrateDisplayName { crate_name, canonical_name }
@@ -634,6 +640,10 @@ impl Env {
634640
}
635641

636642
impl From<Env> for Vec<(String, String)> {
643+
/// Iterates the hash map entries collects them in to a `Vec` then sorts it.
644+
///
645+
/// ## Cost
646+
/// This is expensive as it `collect`s and `sort`s env
637647
fn from(env: Env) -> Vec<(String, String)> {
638648
let mut entries: Vec<_> = env.entries.into_iter().collect();
639649
entries.sort();

src/tools/rust-analyzer/crates/cfg/src/cfg_expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub enum CfgExpr {
3838
}
3939

4040
impl From<CfgAtom> for CfgExpr {
41+
/// Wrap `CfgAtom` in `CfgExpr::Atom`
4142
fn from(atom: CfgAtom) -> Self {
4243
CfgExpr::Atom(atom)
4344
}

src/tools/rust-analyzer/crates/hir-def/src/attr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ pub enum DocExpr {
245245
}
246246

247247
impl From<DocAtom> for DocExpr {
248+
/// Creates `Atom` with `DocAtom`
248249
fn from(atom: DocAtom) -> Self {
249250
DocExpr::Atom(atom)
250251
}

src/tools/rust-analyzer/crates/hir-def/src/hir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ impl Literal {
146146
}
147147

148148
impl From<ast::LiteralKind> for Literal {
149+
/// Create a `Literal` by matching `LiteralKind`
149150
fn from(ast_lit_kind: ast::LiteralKind) -> Self {
150151
use ast::LiteralKind;
151152
match ast_lit_kind {

src/tools/rust-analyzer/crates/hir-def/src/hir/type_ref.rs

+2
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ impl LiteralConstRef {
637637
}
638638

639639
impl From<Literal> for LiteralConstRef {
640+
/// Make a `LiteralConstRef` from `Literal`
641+
/// - Note: `Char`, `Bool`, `Int`, `Uint` match, other types are not const and return a `Unknown`
640642
fn from(literal: Literal) -> Self {
641643
match literal {
642644
Literal::Char(c) => Self::Char(c),

src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub enum ImportOrExternCrate {
3737
}
3838

3939
impl From<ImportOrGlob> for ImportOrExternCrate {
40+
/// Matches a `ImportOrGlob` to it's corresponding `ImportOrExternCrate` variant, of the same name
4041
fn from(value: ImportOrGlob) -> Self {
4142
match value {
4243
ImportOrGlob::Glob(it) => ImportOrExternCrate::Glob(it),
@@ -101,6 +102,7 @@ pub enum ImportOrDef {
101102
}
102103

103104
impl From<ImportOrExternCrate> for ImportOrDef {
105+
/// Matches a `ImportOrExternCrate` to it's corresponding `ImportOrDef` variant, of the same name
104106
fn from(value: ImportOrExternCrate) -> Self {
105107
match value {
106108
ImportOrExternCrate::Import(it) => ImportOrDef::Import(it),
@@ -111,6 +113,7 @@ impl From<ImportOrExternCrate> for ImportOrDef {
111113
}
112114

113115
impl From<ImportOrGlob> for ImportOrDef {
116+
/// Matches a `ImportOrGlob` to it's corresponding `ImportOrDef` variant, of the same name
114117
fn from(value: ImportOrGlob) -> Self {
115118
match value {
116119
ImportOrGlob::Import(it) => ImportOrDef::Import(it),

src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs

+23
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ macro_rules! from_attrs {
516516
( $( $var:ident($t:ty) ),+ $(,)? ) => {
517517
$(
518518
impl From<$t> for AttrOwner {
519+
#[doc = concat!(
520+
"Create a `", stringify!($var), "` from a `", stringify!($t), "`",
521+
"\n\n## Cost",
522+
"This is free as it puts `", stringify!($t), "` into `", stringify!($var), "`"
523+
)]
519524
fn from(t: $t) -> AttrOwner {
520525
AttrOwner::$var(t)
521526
}
@@ -722,6 +727,10 @@ macro_rules! mod_items {
722727
}
723728

724729
impl From<GenericModItem> for ModItem {
730+
/// Create a `ModItem` from a `GenericModItem` variant of the same name
731+
///
732+
/// ## Cost
733+
/// Just a `match`
725734
fn from(id: GenericModItem) -> ModItem {
726735
match id {
727736
$(
@@ -735,20 +744,26 @@ macro_rules! mod_items {
735744
}
736745

737746
impl From<GenericModItem> for AttrOwner {
747+
/// Create a `AttrOwner::ModItem` with `GenericModItem` as inner
748+
///
749+
/// ## Cost
750+
/// The cost is that of the `t.into()` call
738751
fn from(t: GenericModItem) -> AttrOwner {
739752
AttrOwner::ModItem(t.into())
740753
}
741754
}
742755

743756
$(
744757
impl From<FileItemTreeId<$typ>> for ModItem {
758+
#[doc = concat!("Create a `ModItem<", stringify!($typ), ">` from a `FileItemTreeId<", stringify!($typ), ">` variant of the same name")]
745759
fn from(id: FileItemTreeId<$typ>) -> ModItem {
746760
ModItem::$typ(id)
747761
}
748762
}
749763
$(
750764
#[cfg_attr(ignore_fragment, $generic_params)]
751765
impl From<FileItemTreeId<$typ>> for GenericModItem {
766+
#[doc = concat!("Create a `GenericModItem` from a `FileItemTreeId<", stringify!($typ), ">` variant of the same name")]
752767
fn from(id: FileItemTreeId<$typ>) -> GenericModItem {
753768
GenericModItem::$typ(id)
754769
}
@@ -1248,6 +1263,10 @@ macro_rules! impl_froms {
12481263
($e:ident { $($v:ident ($t:ty)),* $(,)? }) => {
12491264
$(
12501265
impl From<$t> for $e {
1266+
#[doc = concat!(
1267+
"Create a `", stringify!($e), "` with `", stringify!($t), "` as the inner",
1268+
"\n\n## Cost\n", stringify!($t), " gets move into ", stringify!($v)
1269+
)]
12511270
fn from(it: $t) -> $e {
12521271
$e::$v(it)
12531272
}
@@ -1296,6 +1315,10 @@ impl_froms!(AssocItem {
12961315
});
12971316

12981317
impl From<AssocItem> for ModItem {
1318+
/// Convert the `AssocItem`s inner to a `ModItem`
1319+
///
1320+
/// ## Cost
1321+
/// The cost is that of the inner items `into` call
12991322
fn from(item: AssocItem) -> Self {
13001323
match item {
13011324
AssocItem::Function(it) => it.into(),

src/tools/rust-analyzer/crates/hir-def/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,21 @@ impl PartialEq<CrateRootModuleId> for ModuleId {
410410
}
411411

412412
impl From<CrateRootModuleId> for ModuleId {
413+
/// Make a `ModuleId` with a krate, no block, and local_id of `ROOT`
413414
fn from(CrateRootModuleId { krate }: CrateRootModuleId) -> Self {
414415
ModuleId { krate, block: None, local_id: DefMap::ROOT }
415416
}
416417
}
417418

418419
impl From<CrateRootModuleId> for ModuleDefId {
420+
/// Make a `ModuleId` with `CrateRootModuleId` as inner
419421
fn from(value: CrateRootModuleId) -> Self {
420422
ModuleDefId::ModuleId(value.into())
421423
}
422424
}
423425

424426
impl From<CrateId> for CrateRootModuleId {
427+
/// Create a `CrateRootModuleId` with `CrateId` as inner
425428
fn from(krate: CrateId) -> Self {
426429
CrateRootModuleId { krate }
427430
}
@@ -564,6 +567,7 @@ impl TypeParamId {
564567
}
565568

566569
impl From<TypeParamId> for TypeOrConstParamId {
570+
/// Get the inner `TypeOrConstParamId` of `TypeParamId`
567571
fn from(it: TypeParamId) -> Self {
568572
it.0
569573
}
@@ -590,6 +594,7 @@ impl ConstParamId {
590594
}
591595

592596
impl From<ConstParamId> for TypeOrConstParamId {
597+
/// Get the inner `TypeOrConstParamId` of `ConstParamId`
593598
fn from(it: ConstParamId) -> Self {
594599
it.0
595600
}
@@ -729,6 +734,7 @@ impl_from!(
729734

730735
// Every `DefWithBodyId` is a type owner, since bodies can contain type (e.g. `{ let it: Type = _; }`)
731736
impl From<DefWithBodyId> for TypeOwnerId {
737+
/// Convert `DefWithBodyId` inner into `TypeOwnerId`
732738
fn from(value: DefWithBodyId) -> Self {
733739
match value {
734740
DefWithBodyId::FunctionId(it) => it.into(),
@@ -741,6 +747,7 @@ impl From<DefWithBodyId> for TypeOwnerId {
741747
}
742748

743749
impl From<GenericDefId> for TypeOwnerId {
750+
/// Convert `GenericDefId` inner into `TypeOwnerId`
744751
fn from(value: GenericDefId) -> Self {
745752
match value {
746753
GenericDefId::FunctionId(it) => it.into(),
@@ -896,6 +903,7 @@ pub enum DefWithBodyId {
896903
impl_from!(FunctionId, ConstId, StaticId, InTypeConstId for DefWithBodyId);
897904

898905
impl From<EnumVariantId> for DefWithBodyId {
906+
/// Make a `VariantId` with `EnumVariantId`, as inner
899907
fn from(id: EnumVariantId) -> Self {
900908
DefWithBodyId::VariantId(id)
901909
}
@@ -1008,6 +1016,7 @@ impl GenericDefId {
10081016
}
10091017

10101018
impl From<AssocItemId> for GenericDefId {
1019+
/// Convert `AssocItemId` inner to `GenericDefId`
10111020
fn from(item: AssocItemId) -> Self {
10121021
match item {
10131022
AssocItemId::FunctionId(f) => f.into(),
@@ -1028,6 +1037,7 @@ impl InternValueTrivial for CallableDefId {}
10281037

10291038
impl_from!(FunctionId, StructId, EnumVariantId for CallableDefId);
10301039
impl From<CallableDefId> for ModuleDefId {
1040+
/// Match `CallableDefId` to `ModuleDefId`
10311041
fn from(def: CallableDefId) -> ModuleDefId {
10321042
match def {
10331043
CallableDefId::FunctionId(f) => ModuleDefId::FunctionId(f),
@@ -1089,6 +1099,10 @@ impl_from!(
10891099
impl TryFrom<ModuleDefId> for AttrDefId {
10901100
type Error = ();
10911101

1102+
/// Convert the inner of `ModuleDefId` into `Ok(AttrDefId)`
1103+
///
1104+
/// # Errors
1105+
/// `BuiltinType` results in `Err`
10921106
fn try_from(value: ModuleDefId) -> Result<Self, Self::Error> {
10931107
match value {
10941108
ModuleDefId::ModuleId(it) => Ok(it.into()),
@@ -1107,6 +1121,7 @@ impl TryFrom<ModuleDefId> for AttrDefId {
11071121
}
11081122

11091123
impl From<ItemContainerId> for AttrDefId {
1124+
/// Match `ItemContainerId` to the `AttrDefId` variant of the same name
11101125
fn from(acid: ItemContainerId) -> Self {
11111126
match acid {
11121127
ItemContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
@@ -1117,6 +1132,7 @@ impl From<ItemContainerId> for AttrDefId {
11171132
}
11181133
}
11191134
impl From<AssocItemId> for AttrDefId {
1135+
/// Match `AssocItemId` to the `AttrDefId` variant of the same name
11201136
fn from(assoc: AssocItemId) -> Self {
11211137
match assoc {
11221138
AssocItemId::FunctionId(it) => AttrDefId::FunctionId(it),
@@ -1126,6 +1142,7 @@ impl From<AssocItemId> for AttrDefId {
11261142
}
11271143
}
11281144
impl From<VariantId> for AttrDefId {
1145+
/// Convert the inner of `VariantId` into `AttrDefId`
11291146
fn from(vid: VariantId) -> Self {
11301147
match vid {
11311148
VariantId::EnumVariantId(id) => id.into(),

src/tools/rust-analyzer/crates/hir-def/src/path.rs

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ impl GenericArgs {
328328
}
329329

330330
impl From<Name> for Path {
331+
/// Make a `BarePath` with a new `Interned` containing a `ModPath` using the `Name`
331332
fn from(name: Name) -> Path {
332333
Path::BarePath(Interned::new(ModPath::from_segments(PathKind::Plain, iter::once(name))))
333334
}

src/tools/rust-analyzer/crates/hir-ty/src/consteval.rs

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ impl ConstEvalError {
7272
}
7373

7474
impl From<MirLowerError> for ConstEvalError {
75+
/// Convert `MirLowerError` to `ConstEvalError` by getting inner of `MirLowerError::ConstEvalError`
76+
/// or wrapping it in the `MirLowerError` variant
77+
///
78+
/// ## Cost
79+
/// Just a `match`
7580
fn from(value: MirLowerError) -> Self {
7681
match value {
7782
MirLowerError::ConstEvalError(_, e) => *e,
@@ -81,6 +86,10 @@ impl From<MirLowerError> for ConstEvalError {
8186
}
8287

8388
impl From<MirEvalError> for ConstEvalError {
89+
/// Convert a `MirEvalError` by wrapping it in the `ConstEvalError::MirEvalError` variant
90+
///
91+
/// ## Cost
92+
/// Free
8493
fn from(value: MirEvalError) -> Self {
8594
ConstEvalError::MirEvalError(value)
8695
}

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ pub enum HirDisplayError {
438438
FmtError,
439439
}
440440
impl From<fmt::Error> for HirDisplayError {
441+
/// Ignore the `fmt::Error` and return `Self::FmtError`
441442
fn from(_: fmt::Error) -> Self {
442443
Self::FmtError
443444
}

src/tools/rust-analyzer/crates/hir-ty/src/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl fmt::Display for LayoutError {
110110
}
111111

112112
impl<F> From<LayoutCalculatorError<F>> for LayoutError {
113+
/// Make a `BadCalc` error from a `LayoutCalculatorError` without payload
113114
fn from(err: LayoutCalculatorError<F>) -> Self {
114115
LayoutError::BadCalc(err.without_payload())
115116
}

0 commit comments

Comments
 (0)