The Dark Secrets Lurking Inside cargo doc @QuietMisdreavus 2018 - - PowerPoint PPT Presentation

the dark secrets lurking inside
SMART_READER_LITE
LIVE PREVIEW

The Dark Secrets Lurking Inside cargo doc @QuietMisdreavus 2018 - - PowerPoint PPT Presentation

The Dark Secrets Lurking Inside cargo doc @QuietMisdreavus 2018 @QuietMisdreavus 2018 hey, i just met you... @QuietMisdreavus // grey // they/them Rustdoc Team (lead), Docs Team Sharer of music Knitter of hats


slide-1
SLIDE 1

@QuietMisdreavus 2018

The Dark Secrets Lurking Inside

cargo doc

@QuietMisdreavus 2018

slide-2
SLIDE 2

@QuietMisdreavus 2018

“hey, i just met you...”

  • @QuietMisdreavus // “grey” // they/them
  • Rustdoc Team (lead), Docs Team
  • Sharer of music
  • Knitter of hats
  • Some code things, I guess

@QuietMisdreavus 2018

Lyric reference: Carly Rae Jepsen - “Call Me Maybe”

slide-3
SLIDE 3

@QuietMisdreavus 2018

i like docs

@QuietMisdreavus 2018

slide-4
SLIDE 4

@QuietMisdreavus 2018

i like docs… like, a lot

@QuietMisdreavus 2018

slide-5
SLIDE 5

@QuietMisdreavus 2018

i like docs… like, maybe too much

@QuietMisdreavus 2018

slide-6
SLIDE 6

@QuietMisdreavus 2018

“...and this is crazy...”

  • Become a Docs Power User (tm)
  • Deny lints on your doctests
  • Document all your platforms at once
  • Cover your docs in ponies
  • Peek under the hood

@QuietMisdreavus 2018

Lyric reference: Carly Rae Jepsen - “Call Me Maybe”

slide-7
SLIDE 7

@QuietMisdreavus 2018

Rustdoc output

@QuietMisdreavus 2018

slide-8
SLIDE 8

@QuietMisdreavus 2018

Want to cut to the chase?

Click this link to fold (or unfold) everything

  • n the page!
slide-9
SLIDE 9

@QuietMisdreavus 2018

Want to see how it’s done?

@QuietMisdreavus 2018

Check the source!

slide-10
SLIDE 10

@QuietMisdreavus 2018

Check the source!

The crate’s source code is shipped and highlighted alongside all its docs!

slide-11
SLIDE 11

@QuietMisdreavus 2018

Color-coded links!

@QuietMisdreavus 2018

All types in rustdoc’s generated signatures are links to their docs!

  • Magenta: Structs
  • Purple: Traits
  • Green: Enums
  • Blue: Primitives
  • Tan: Functions
slide-12
SLIDE 12

@QuietMisdreavus 2018

Want everything on one page?

@QuietMisdreavus 2018

slide-13
SLIDE 13

@QuietMisdreavus 2018

Curious how to use a type?

Tabs in the search results can show where a type is used by or returned from a function!

slide-14
SLIDE 14

@QuietMisdreavus 2018

And more!

Press “?” for keyboard shortcuts and search hints! Click the gear by the search box for doc settings! Search operators in the standard library to see their traits!

slide-15
SLIDE 15

@QuietMisdreavus 2018

introducing rustdoc

The tool behind `cargo doc`!

cargo build rustc cargo doc rustdoc

@QuietMisdreavus 2018

slide-16
SLIDE 16

@QuietMisdreavus 2018

Yo dawg, we heard you like code, so we put code in your docs, so you can read code while you read about code

@QuietMisdreavus 2018

Meme reference: “Xzibit Yo Dawg”

Put some code samples into your docs... ...and rustdoc can run them with your tests!

slide-17
SLIDE 17

@QuietMisdreavus 2018

The journey of a doctest

@QuietMisdreavus 2018

Rustdoc wants to compile your doctest as an executable… …so it wraps your code in a main function… …and adds in a reference to your crate!

slide-18
SLIDE 18

@QuietMisdreavus 2018

Guiding doctests on their journey

@QuietMisdreavus 2018

Not everything goes inside fn main(), though! Let’s extend that test some… Crate attributes and extern crate statements are preserved

  • utside the generated main
slide-19
SLIDE 19

@QuietMisdreavus 2018

Doctests and Lints

@QuietMisdreavus 2018

By default, doctests also get #![allow(unused)] But you can change that! Add this attribute to your crate…

slide-20
SLIDE 20

@QuietMisdreavus 2018

Doctests and Lints

@QuietMisdreavus 2018

…and change that attribute with whatever you want!

slide-21
SLIDE 21

@QuietMisdreavus 2018

Doctests and Lints

@QuietMisdreavus 2018

slide-22
SLIDE 22

@QuietMisdreavus 2018

Doc comments are special!

@QuietMisdreavus 2018

Lyric reference: Insane Clown Posse - “Miracles”

hecking docs, how do they work

Rustdoc compiles your crate to scrape out these attributes

slide-23
SLIDE 23

@QuietMisdreavus 2018

The #[doc] attribute does a lot!

  • #![doc(html_root_url)]
  • #![doc(test(attr))]
  • #[doc(inline)], #[doc(no_inline)]
  • #[doc(hidden)]
  • #[doc(include)]
  • #[doc(cfg)]

@QuietMisdreavus 2018

slide-24
SLIDE 24

@QuietMisdreavus 2018

#[doc(cfg)]: All your platforms at once

@QuietMisdreavus 2018

slide-25
SLIDE 25

@QuietMisdreavus 2018

Conditional compilation vs. your docs

Rust handles conditional compilation before rustdoc can make your docs!

slide-26
SLIDE 26

@QuietMisdreavus 2018

Forcing rustdoc to see the items

By compiling them in whenever rustdoc is running, we can show everything! But…

(Note: #[cfg(rustdoc)] is not available yet! There’s an open PR for it!)

slide-27
SLIDE 27

@QuietMisdreavus 2018

Just making rustdoc see the item means it will try to run its doctests on the wrong platforms!

slide-28
SLIDE 28

@QuietMisdreavus 2018

Enter #[doc(cfg)]

Telling rustdoc specifically about the platform… …means it knows when to run (and to ignore) the doctests!

slide-29
SLIDE 29

@QuietMisdreavus 2018

Bonus!

Now rustdoc can tell your users about the platform in the docs for you!

slide-30
SLIDE 30

@QuietMisdreavus 2018

CLI Flags

  • Compile flags
  • --cfg, --extern, -C, --target, --edition
  • Content modification
  • --html-in-header, --html-before-content, --html-after-content
  • --document-private-items, --sort-modules-by-appearance
  • Process modification
  • --passes, --no-defaults, --resource-suffix, --disable-minification
slide-31
SLIDE 31

@QuietMisdreavus 2018

Splicing new content into your docs

slide-32
SLIDE 32

@QuietMisdreavus 2018

…but add a flag to rustdoc…

slide-33
SLIDE 33

@QuietMisdreavus 2018

Want some KaTeX in your docs?

slide-34
SLIDE 34

@QuietMisdreavus 2018

No? How about some ponies?

https://docs.rs/pwnies

slide-35
SLIDE 35

@QuietMisdreavus 2018

A peek behind the curtain

Rustdoc’s code lives in the main “rust-lang/rust” repo, right next to the compiler and standard library!

slide-36
SLIDE 36

@QuietMisdreavus 2018

Rustdoc is old!

  • July 2009: Rust begins
  • April 2011: Self-

hosting

  • December 2011:

Rustdoc is added

slide-37
SLIDE 37

@QuietMisdreavus 2018

Data gathering practices

  • Asking the compiler nicely for what we want
  • Going around the compiler’s back to get what we want
  • Breaking the compiler to get what we want
  • Breaking the compiler’s friends to get what we want
slide-38
SLIDE 38

@QuietMisdreavus 2018

Thanks @DebugSteven!

slide-39
SLIDE 39

@QuietMisdreavus 2018

Documentation flow (jargon-filled version)

  • Source code is first handed directly to the compiler
  • After macro expansion, the name resolver is saved to handle “intra-

doc links” later

  • After crate analysis, while the TyCtxt is still active, scan the HIR to

collect all items in the crate

  • “Clean” up all these items so we can have an AST more suited to

rustdoc’s purposes

slide-40
SLIDE 40

@QuietMisdreavus 2018

Documentation flow (jargon-filled version)

  • Run the cleaned AST through several “passes” to strip out private

items, massage doc comments, and otherwise process the crate for later doc generation

  • The TyCtxt is dropped here, leaving the compiler context
  • Scan through the crate again to collect all the trait impls,

gather/highlight source code, generate search index

  • Run through the crate one last time to generate a file for each item

and module

slide-41
SLIDE 41

@QuietMisdreavus 2018

Highlights of rustdoc internals

These “Auto Trait Implementations” don’t come directly from the code, so rustdoc has to make them up on the spot

slide-42
SLIDE 42

@QuietMisdreavus 2018

Highlights of rustdoc internals

Rustdoc “templating engine” is a massive write!() call and a series of Display impls

slide-43
SLIDE 43

@QuietMisdreavus 2018

Highlights of rustdoc internals

Rustdoc has its own test suite, to make sure we

  • utput files and their

content correctly

slide-44
SLIDE 44

@QuietMisdreavus 2018

The Rustdoc Team

@QuietMisdreavus @GuillaumeGomez @steveklabnik @ollie27 @onur

slide-45
SLIDE 45

@QuietMisdreavus 2018

  • @QuietMisdreavus
  • quietmisdreavus.net
  • “misdreavus” on Mozilla IRC
  • Fill your world with love today

@QuietMisdreavus 2018

“...but here’s my number”

Lyric reference: Carly Rae Jepsen - “Call Me Maybe”