requirements.references.selector_impl_and_trait
Requirements index · View definition
Example
In
src/lib.rs, a methodruninimpl Thinghas selectorimpl[Thing]/fn[run]; a methodruninimpl Trait for Thinghas selectorimpl[Thing/Trait]/fn[run]; and a methodreaddeclared intrait Readhas selectortrait[Read]/fn[read].
Rationale
The example has three scenarios, and two tests in src/outline/tests.rs carry {test} references to it. Together they cover all three.
rust_selectors_follow_syntactic_ancestry(lines 35–100) builds an outline for the pathsrc/lib.rs, the same file the example names. Its source declaresstruct Thing;,impl Thing { fn run() {} },impl Trait for Thing { fn run() {} }andtrait Trait { fn read(&self); ... }.- It asserts that the outline contains
impl[Thing]/fn[run],impl[Thing/Trait]/fn[run]andtrait[Trait]/fn[read]. - It also checks that each selector resolves back to a node carrying the same selector, using
outline.resolve(selector).
- It asserts that the outline contains
selectors_name_syntactic_ancestry_and_duplicates(lines 662–706) repeats the same three assertions on a separate source with the pathlib.rs.
The tests name the trait Trait where the example names it Read. The selector shape is the same, trait[<Name>]/fn[<method>], so this does not change what is covered. The inherent-impl and trait-impl cases differ only by the /Trait suffix, and both are asserted. The tests also cover related edge cases: an impl on a non-path self type (impl[%5Bu8;%204%5D]/fn[edge]) and duplicate names (fn[repeated]#1 and #2).
The test setup and assertions cover every scenario and expected outcome in the example, so I judge it satisfied.
Inputs
Tests
Input 3: outline src/outline/tests.rs fn[rust_selectors_follow_syntactic_ancestry]
- File:
src/outline/tests.rs - Selector:
fn[rust_selectors_follow_syntactic_ancestry] - Kind:
fn src/outline/tests.rs:192–258
// {test}`requirements.references.outline_selectors`
// {test}`requirements.references.rust_item_selectors`
// {test}`requirements.references.selector_nested`
// {test}`requirements.references.selector_impl_and_trait`
// {test}`requirements.references.selector_inline_and_foreign`
// {test}`requirements.references.selector_duplicate_names`
// {test}`requirements.references.outlines`
#[test]
fn rust_selectors_follow_syntactic_ancestry() {
let source = r#"
mod local { fn run() {} }
mod external;
struct Thing;
impl Thing { fn run() {} }
impl Trait for Thing { fn run() {} }
impl [u8; 4] { fn edge() {} }
trait Trait { fn read(&self); type Item; }
extern "C" { fn puts(); }
extern { fn other(); }
fn outer() { fn inner() {} }
#[cfg(a)] fn repeated() {}
#[cfg(b)] fn repeated() {}
macro_rules! make { () => {} }
union U { a: u8 }
enum E { A }
const C: u8 = 1;
static S: u8 = 1;
type Alias = u8;
extern crate core;
"#;
let outline = outline("src/lib.rs", source);
let selectors = selectors(&outline);
for selector in [
"mod[local]/fn[run]",
"mod[external]",
"struct[Thing]",
"impl[Thing]/fn[run]",
"impl[Thing/Trait]/fn[run]",
"impl[%5Bu8;%204%5D]/fn[edge]",
"trait[Trait]/fn[read]",
"trait[Trait]/type[Item]",
"extern[C]/fn[puts]",
"extern[Rust]/fn[other]",
"fn[outer]/fn[inner]",
"fn[repeated]#1",
"fn[repeated]#2",
"macro[make]",
"union[U]",
"enum[E]",
"const[C]",
"static[S]",
"type[Alias]",
"extern-crate[core]",
] {
assert!(
selectors.contains(&selector.to_owned()),
"{selector}: {selectors:?}"
);
assert_eq!(
outline.nodes[outline.resolve(selector).unwrap()].selector,
selector
);
}
assert_eq!(outline.resolve("").unwrap(), 0);
assert!(outline.resolve("fn[repeated]").is_err());
assert!(outline.resolve("fn[missing]").is_err());
}
Input 4: outline src/outline/tests.rs fn[selectors_name_syntactic_ancestry_and_duplicates]
- File:
src/outline/tests.rs - Selector:
fn[selectors_name_syntactic_ancestry_and_duplicates] - Kind:
fn src/outline/tests.rs:840–885
// {test}`requirements.references.rust_item_selectors`
// {test}`requirements.references.selector_nested`
// {test}`requirements.references.selector_impl_and_trait`
// {test}`requirements.references.selector_inline_and_foreign`
// {test}`requirements.references.selector_duplicate_names`
// {test}`requirements.references.selector_use`
// {test}`requirements.references.outlines`
#[test]
fn selectors_name_syntactic_ancestry_and_duplicates() {
let source = r#"
use alpha::A;
use beta::B;
mod local { fn run() {} }
mod external;
struct Thing;
impl Thing { fn run() {} }
impl Trait for Thing { fn run() {} }
impl [u8; 4] { fn edge() {} }
trait Trait { fn read(&self); }
extern "C" { fn puts(); }
fn outer() { fn inner() {} }
#[cfg(a)] fn repeated() {}
#[cfg(b)] fn repeated() {}
"#;
let selectors = selectors(&outline("lib.rs", source));
assert!(selectors[0].starts_with("use@") && selectors[1].starts_with("use@"));
assert_ne!(selectors[0], selectors[1]);
for selector in [
"mod[local]/fn[run]",
"mod[external]",
"struct[Thing]",
"impl[Thing]/fn[run]",
"impl[Thing/Trait]/fn[run]",
"impl[%5Bu8;%204%5D]/fn[edge]",
"trait[Trait]/fn[read]",
"extern[C]/fn[puts]",
"fn[outer]/fn[inner]",
"fn[repeated]#1",
"fn[repeated]#2",
] {
assert!(
selectors.contains(&selector.to_owned()),
"{selector}: {selectors:?}"
);
}
}
Referenced definitions
Input 2: requirements selection requirements.references.rust_item_selectors
requirements.references.rust_item_selectors:md/requirements/references.md:344–354
:::{req} rust_item_selectors
The Rust outline profile under {cite}`requirements.references.outlines` MUST
identify items by segments describing their syntactic ancestry within the file,
such as `fn[run]`, `struct[Packet]`, and `mod[local]/fn[run]`, and MUST NOT
resolve Rust modules across files. Functions, methods, structs, enums, unions,
traits, impls, modules, macros, constants, statics, type aliases, and extern
blocks MUST be outline nodes. Impl segments MUST be named by their syntactic self
type and optional trait path, as `impl[Thing]` and `impl[Thing/Trait]`. `use`
declarations and other items without a suitable name MUST be anonymous. Selector
spelling MUST be documented.
:::
Other sources
Input 1: requirements selection requirements.references.selector_impl_and_trait
requirements.references.selector_impl_and_trait:md/requirements/references.md:445–447
:::{example} selector_impl_and_trait
In `src/lib.rs`, a method `run` in `impl Thing` has selector `impl[Thing]/fn[run]`; a method `run` in `impl Trait for Thing` has selector `impl[Thing/Trait]/fn[run]`; and a method `read` declared in `trait Read` has selector `trait[Read]/fn[read]`. {cite}`requirements.references.rust_item_selectors`
:::