Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contribute Doctests to Tokio #54

Open
yoshuawuyts opened this Issue Aug 29, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@yoshuawuyts
Copy link
Collaborator

yoshuawuyts commented Aug 29, 2018

Contribute Doctests to Tokio

Summary

We propose to improve Tokio's documentation by adding doctests for each public
method.

Motivation

Tokio is a highly important library in Rust's ecosystem. It provides async
counterparts for many of stdlib's methods. However, unlike stdlib not every
method has usage examples yet (also known as doctests).

We propose to make a coordinated effort to track down missing usage examples,
and make pull requests to add them. This will hopefully both help people get
involved with Tokio, as help people new to the project find their way on how to
the libraries.

Implementation

I ran a script to find all pub fn names in tokio, and added them to the
overview
at the end of this issue. To contribute a doctest:

  1. Clone tokio-rs/tokio.
  2. Choose a file from the overview.
  3. Comment on this issue which file you've chosen to work on.
  4. Write a few doctests for the file.
  5. Read Tokio's contributing guidelines.
  6. Make a PR, and link back to this issue.
  7. Once the PR is approved, we can cross off doctests from the file.

Example from current documentation

Script

#!/bin/bash

base_url="https://github.com/tokio-rs/tokio"
curr_commit="cc3b6af7a3927751b12a82c61fae97b4cca30c12"

# Find all public methods
matches="$(rg -g '!names' --vimgrep 'pub fn' .)"
matches="$(echo "$matches" | grep -v main | grep -v tests | grep -v examples)"

# Get all method names, file names, and merge them on one line
fn_names="$(echo "$matches" | perl -nE '/pub fn (\w+)/ and say "$1";')"
line_nums="$(echo "$matches" | perl -nE '/\:(\d+)\:/ and say "$1";')"
lines="$(echo "$matches" | awk '{print $1}' | sed 's/\:.*$//')"
files="$(paste <(printf "$lines") <(printf "$fn_names") <(printf "$line_nums"))"

# Iterate over all file-method names combinations, and create a markdown list
echo "$files" | while read line; do
  file="$(echo "$line" | awk '{print $1}')"
  fn_name="$(echo "$line" | awk '{print $2}')"
  num="$(echo "$line" | awk '{print $3}')"
  url="$(printf '%s/blob/%s/%s#L%s' "$base_url" "$curr_commit" "$file" "$num")"
  echo "- [ ] [$file#$fn_name]($url)"
done

Drawbacks

There seems to be some duplication in the APIs between tokio and tokio-*
modules. If both modules are going to be merged, perhaps we should prioritize
one implementation first.

Secondly Tokio doesn't support futures@0.3.0 yet, which means async/await
support is in somewhat of a transitioning phase. This might be an argument to
hold off until tokio upgrades to futures@0.3.0. But there doesn't seem to be a
clear timeline yet for when such a transition might be done, and there seem to
be clear benefits to improve documentation today.

There are over 600 API methods in tokio. That means there's probably quite a
few examples to be written. Therefor helping out with reviewing doctest PRs
would be fantastic too!

Rationale and alternatives

The idea of doctests is that they exist in harmony with more tutorials and
examples. It does not replace any of the above, but instead makes the existing

Unresolved Questions

We should probably figure out which modules to prioritize to prevent duplicate
work.


cc/ @carllerche @seanmonstar Your input would be valued a lot!

Overview

Tokio

tokio-channel

tokio-codec

tokio-executor

tokio-current-thread

tokio-fs

tokio-io

tokio-reactor

tokio-tcp

tokio-threadpool

tokio-timer

tokio-tls

tokio-udp

tokio-uds

tokio-io

tokio-threadpool

tokio-timer

tokio-async-await

tokio-fs

@Thomasdezeeuw

This comment has been minimized.

Copy link

Thomasdezeeuw commented Aug 29, 2018

Why is this not an issue in the tokio repo?

@yoshuawuyts

This comment has been minimized.

Copy link
Collaborator Author

yoshuawuyts commented Aug 30, 2018

@Thomasdezeeuw good question! I guess it's mostly because I was approaching it from a Net WG perspective, but you're right that it would also have worked as an issue on Tokio. I'm not sure which one works best, but open for arguments pro / con.

@carllerche

This comment has been minimized.

Copy link

carllerche commented Aug 30, 2018

@yoshuawuyts I added a doc test section to the contributing guide:

https://github.com/tokio-rs/tokio/blob/master/CONTRIBUTING.md#documentation-tests

@yoshuawuyts

This comment has been minimized.

Copy link
Collaborator Author

yoshuawuyts commented Aug 30, 2018

@carllerche thanks so much; it looks great!

mattgathu added a commit to mattgathu/tokio that referenced this issue Nov 26, 2018

tcp: add usage examples to TcpListener and TcpStream
Adding doc tests and usage examples to the public methods
of `TcpListener` and `TcpStream`. The documented methods are:
- tokio-tcp/src/listener.rs#bind
- tokio-tcp/src/listener.rs#accept
- tokio-tcp/src/listener.rs#poll_accept
- tokio-tcp/src/listener.rs#accept_std
- tokio-tcp/src/listener.rs#poll_accept_std
- tokio-tcp/src/listener.rs#from_std
- tokio-tcp/src/listener.rs#local_addr
- tokio-tcp/src/listener.rs#incoming
- tokio-tcp/src/listener.rs#ttl
- tokio-tcp/src/listener.rs#set_ttl
- tokio-tcp/src/stream.rs#connect
- tokio-tcp/src/stream.rs#from_std
- tokio-tcp/src/stream.rs#poll_read_ready
- tokio-tcp/src/stream.rs#poll_write_ready
- tokio-tcp/src/stream.rs#local_addr
- tokio-tcp/src/stream.rs#peer_addr
- tokio-tcp/src/stream.rs#peek
- tokio-tcp/src/stream.rs#poll_peek
- tokio-tcp/src/stream.rs#shutdown
- tokio-tcp/src/stream.rs#nodelay
- tokio-tcp/src/stream.rs#set_nodelay
- tokio-tcp/src/stream.rs#recv_buffer_size
- tokio-tcp/src/stream.rs#set_recv_buffer_size
- tokio-tcp/src/stream.rs#send_buffer_size
- tokio-tcp/src/stream.rs#set_send_buffer_size
- tokio-tcp/src/stream.rs#keepalive
- tokio-tcp/src/stream.rs#set_keepalive
- tokio-tcp/src/stream.rs#ttl
- tokio-tcp/src/stream.rs#set_ttl
- tokio-tcp/src/stream.rs#linger
- tokio-tcp/src/stream.rs#set_linger
- tokio-tcp/src/stream.rs#try_clone

Refs: rustasync/team#54

tobz added a commit to tokio-rs/tokio that referenced this issue Nov 28, 2018

mattgathu added a commit to mattgathu/tokio that referenced this issue Dec 1, 2018

fs: added usage examples/doctests to File
Motivation
--
Part of [this](rustasync/team#54) networking working group issue.

Solution
--
Add doctests to public methods of the `tokio::fs::File` struct.

- file::File
- file::File::from_std
- file::File::create
- file::File
- file::File::into_std
- file::File::metadata
- file::File::open
- file::File::poll_metadata
- file::File::poll_seek
- file::File::poll_set_len
- file::File::poll_set_permissions
- file::File::poll_sync_all
- file::File::poll_sync_data
- file::File::poll_try_clone
- file::File::seek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.