diff --git a/.cargo/config.in b/.cargo/config.in index eeee947668a2..6455aa50b737 100644 --- a/.cargo/config.in +++ b/.cargo/config.in @@ -10,7 +10,7 @@ replace-with = "vendored-sources" [source."https://github.com/mozilla/neqo"] git = "https://github.com/mozilla/neqo" replace-with = "vendored-sources" -tag = "v0.4.8" +tag = "v0.4.9" [source."https://github.com/mozilla/mp4parse-rust"] git = "https://github.com/mozilla/mp4parse-rust" diff --git a/Cargo.lock b/Cargo.lock index 78d78f2e47c1..19c8542db5b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.7", +] + [[package]] name = "anyhow" version = "1.0.30" @@ -215,7 +224,7 @@ dependencies = [ "bindgen", "cranelift-codegen", "cranelift-wasm", - "env_logger", + "env_logger 0.6.2", "log", "smallvec", ] @@ -275,14 +284,18 @@ dependencies = [ "cexpr", "cfg-if", "clang-sys", + "clap", + "env_logger 0.7.1", "lazy_static", "lazycell", + "log", "peeking_take_while", "proc-macro2", "quote", "regex", "rustc-hash", "shlex", + "which", ] [[package]] @@ -589,11 +602,14 @@ version = "2.31.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536" dependencies = [ + "ansi_term", + "atty", "bitflags", "strsim", "term_size", "textwrap", "unicode-width", + "vec_map", ] [[package]] @@ -1280,6 +1296,19 @@ dependencies = [ "termcolor", ] +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "error-chain" version = "0.11.0" @@ -1680,7 +1709,7 @@ name = "gecko_logger" version = "0.1.0" dependencies = [ "app_services_logger", - "env_logger", + "env_logger 0.6.2", "lazy_static", "log", ] @@ -2381,7 +2410,7 @@ version = "0.1.4" dependencies = [ "bindgen", "cmake", - "env_logger", + "env_logger 0.6.2", "glob", "lazy_static", "libc", @@ -3129,11 +3158,11 @@ dependencies = [ [[package]] name = "neqo-common" -version = "0.4.8" -source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e" +version = "0.4.9" +source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6" dependencies = [ "chrono", - "env_logger", + "env_logger 0.6.2", "lazy_static", "log", "num-traits", @@ -3142,8 +3171,8 @@ dependencies = [ [[package]] name = "neqo-crypto" -version = "0.4.8" -source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e" +version = "0.4.9" +source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6" dependencies = [ "bindgen", "log", @@ -3155,8 +3184,8 @@ dependencies = [ [[package]] name = "neqo-http3" -version = "0.4.8" -source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e" +version = "0.4.9" +source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6" dependencies = [ "log", "neqo-common", @@ -3170,8 +3199,8 @@ dependencies = [ [[package]] name = "neqo-qpack" -version = "0.4.8" -source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e" +version = "0.4.9" +source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6" dependencies = [ "lazy_static", "log", @@ -3185,9 +3214,10 @@ dependencies = [ [[package]] name = "neqo-transport" -version = "0.4.8" -source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e" +version = "0.4.9" +source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6" dependencies = [ + "indexmap", "lazy_static", "log", "neqo-common", @@ -3446,7 +3476,7 @@ version = "0.1.4" dependencies = [ "byteorder", "core-foundation", - "env_logger", + "env_logger 0.6.2", "lazy_static", "libloading 0.5.2", "log", @@ -4502,7 +4532,7 @@ name = "smoosh" version = "0.1.0" dependencies = [ "bumpalo", - "env_logger", + "env_logger 0.6.2", "jsparagus", "log", ] @@ -4702,7 +4732,7 @@ dependencies = [ "atomic_refcell", "cssparser", "cstr", - "env_logger", + "env_logger 0.6.2", "geckoservo", "libc", "log", @@ -5681,6 +5711,15 @@ dependencies = [ "wgpu-types", ] +[[package]] +name = "which" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" +dependencies = [ + "libc", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/netwerk/socket/neqo_glue/Cargo.toml b/netwerk/socket/neqo_glue/Cargo.toml index c26d981a7091..9c77eac455e4 100644 --- a/netwerk/socket/neqo_glue/Cargo.toml +++ b/netwerk/socket/neqo_glue/Cargo.toml @@ -8,17 +8,17 @@ edition = "2018" name = "neqo_glue" [dependencies] -neqo-http3 = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" } -neqo-transport = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" } -neqo-common = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" } -neqo-qpack = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" } +neqo-http3 = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" } +neqo-transport = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" } +neqo-common = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" } +neqo-qpack = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" } nserror = { path = "../../../xpcom/rust/nserror" } nsstring = { path = "../../../xpcom/rust/nsstring" } xpcom = { path = "../../../xpcom/rust/xpcom" } thin-vec = { version = "0.1.0", features = ["gecko-ffi"] } [dependencies.neqo-crypto] -tag = "v0.4.8" +tag = "v0.4.9" git = "https://github.com/mozilla/neqo" default-features = false features = ["gecko"] diff --git a/netwerk/test/http3server/Cargo.toml b/netwerk/test/http3server/Cargo.toml index 016b85ea5870..b99b95e5095c 100644 --- a/netwerk/test/http3server/Cargo.toml +++ b/netwerk/test/http3server/Cargo.toml @@ -5,16 +5,16 @@ authors = ["Dragana Damjanovic "] edition = "2018" [dependencies] -neqo-transport = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" } -neqo-common = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" } -neqo-http3 = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" } -neqo-qpack = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" } +neqo-transport = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" } +neqo-common = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" } +neqo-http3 = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" } +neqo-qpack = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" } mio = "0.6.17" mio-extras = "2.0.5" log = "0.4.0" [dependencies.neqo-crypto] -tag = "v0.4.8" +tag = "v0.4.9" git = "https://github.com/mozilla/neqo" default-features = false features = ["gecko"] diff --git a/third_party/rust/ansi_term/.cargo-checksum.json b/third_party/rust/ansi_term/.cargo-checksum.json new file mode 100644 index 000000000000..c1b9d1b6e276 --- /dev/null +++ b/third_party/rust/ansi_term/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"48df4570f3bbfba5b3c19731a54abf70ac8608e4dab6f4dae6a694b18d6ad102","LICENCE":"a24742368cf773bbb8b6f0fcbe86ca4b802c2b7c081bc8bebf14ac38618e7c63","README.md":"779b02ebacd6f4d08e01ef289bd7976a4467054f40355593817fd6df7e8c9dd4","examples/colours.rs":"e4870671adb9574607e37a0e4145643f9047c881c310113de114ec20d76aaf4b","src/ansi.rs":"b8f5de966e7ec2fba7a4d5a373d0aceafe19ea6e20a3f4daaf448e119c989ae7","src/debug.rs":"0ab28b65c39538825707d8b7e81c6f91c78310856c936bba0ee609e06d138543","src/difference.rs":"da68156310cbaf57a3619160d0fb966f496f970c32a2e57601127cc8f54a2fbf","src/display.rs":"a43f19b7cf4d95e90e4f3954399405d8350523d423f0beed9f01399e17527b17","src/lib.rs":"b6df00ab61ca0d82c9f7b3798d516384dd2617fe73e8981f37125ecccc970dd7","src/style.rs":"7c5c2524428f0dfbe3b8d5876ddb81c47d8635704ee31ef63ddeb2429a67f457","src/windows.rs":"3b52469eed89fdc258139e4fd978f0e30c72168863966247df781da8715f0841","src/write.rs":"247c518f8b0c103c970bbe7bc70caba3ee961ab0d37095e2da5c69db98d2fc24"},"package":"ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"} \ No newline at end of file diff --git a/third_party/rust/ansi_term/Cargo.toml b/third_party/rust/ansi_term/Cargo.toml new file mode 100644 index 000000000000..56a7f2d36fa0 --- /dev/null +++ b/third_party/rust/ansi_term/Cargo.toml @@ -0,0 +1,27 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g. crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "ansi_term" +version = "0.11.0" +authors = ["ogham@bsago.me", "Ryan Scheel (Havvy) ", "Josh Triplett "] +description = "Library for ANSI terminal colours and styles (bold, underline)" +homepage = "https://github.com/ogham/rust-ansi-term" +documentation = "https://docs.rs/ansi_term" +readme = "README.md" +license = "MIT" + +[lib] +name = "ansi_term" +[target."cfg(target_os=\"windows\")".dependencies.winapi] +version = "0.3.4" +features = ["errhandlingapi", "consoleapi", "processenv"] diff --git a/third_party/rust/ansi_term/LICENCE b/third_party/rust/ansi_term/LICENCE new file mode 100644 index 000000000000..3f39e72bd6be --- /dev/null +++ b/third_party/rust/ansi_term/LICENCE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Benjamin Sago + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/rust/ansi_term/README.md b/third_party/rust/ansi_term/README.md new file mode 100644 index 000000000000..1cb56a19a20b --- /dev/null +++ b/third_party/rust/ansi_term/README.md @@ -0,0 +1,174 @@ +# rust-ansi-term [![ansi-term on crates.io](http://meritbadge.herokuapp.com/ansi-term)](https://crates.io/crates/ansi_term) [![Build status](https://travis-ci.org/ogham/rust-ansi-term.svg?branch=master)](https://travis-ci.org/ogham/rust-ansi-term) [![Coverage status](https://coveralls.io/repos/ogham/rust-ansi-term/badge.svg?branch=master&service=github)](https://coveralls.io/github/ogham/rust-ansi-term?branch=master) + +This is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals. + +### [View the Rustdoc](https://docs.rs/ansi_term/0.9.0/ansi_term/) + + +# Installation + +This crate works with [Cargo](http://crates.io). Add the following to your `Cargo.toml` dependencies section: + +```toml +[dependencies] +ansi_term = "0.9" +``` + + +## Basic usage + +There are two main data structures in this crate that you need to be concerned with: `ANSIString` and `Style`. +A `Style` holds stylistic information: colours, whether the text should be bold, or blinking, or whatever. +There are also `Colour` variants that represent simple foreground colour styles. +An `ANSIString` is a string paired with a `Style`. + +(Yes, it’s British English, but you won’t have to write “colour” very often. `Style` is used the majority of the time.) + +To format a string, call the `paint` method on a `Style` or a `Colour`, passing in the string you want to format as the argument. +For example, here’s how to get some red text: + +```rust +use ansi_term::Colour::Red; +println!("This is in red: {}", Red.paint("a red string")); +``` + +It’s important to note that the `paint` method does *not* actually return a string with the ANSI control characters surrounding it. +Instead, it returns an `ANSIString` value that has a `Display` implementation that, when formatted, returns the characters. +This allows strings to be printed with a minimum of `String` allocations being performed behind the scenes. + +If you *do* want to get at the escape codes, then you can convert the `ANSIString` to a string as you would any other `Display` value: + +```rust +use ansi_term::Colour::Red; +use std::string::ToString; +let red_string = Red.paint("a red string").to_string(); +``` + +**Note for Windows 10 users:** On Windows 10, the application must enable ANSI support first: + +```rust +let enabled = ansi_term::enable_ansi_support(); +``` + +## Bold, underline, background, and other styles + +For anything more complex than plain foreground colour changes, you need to construct `Style` objects themselves, rather than beginning with a `Colour`. +You can do this by chaining methods based on a new `Style`, created with `Style::new()`. +Each method creates a new style that has that specific property set. +For example: + +```rust +use ansi_term::Style; +println!("How about some {} and {}?", + Style::new().bold().paint("bold"), + Style::new().underline().paint("underline")); +``` + +For brevity, these methods have also been implemented for `Colour` values, so you can give your styles a foreground colour without having to begin with an empty `Style` value: + +```rust +use ansi_term::Colour::{Blue, Yellow}; +println!("Demonstrating {} and {}!", + Blue.bold().paint("blue bold"), + Yellow.underline().paint("yellow underline")); +println!("Yellow on blue: {}", Yellow.on(Blue).paint("wow!")); +``` + +The complete list of styles you can use are: +`bold`, `dimmed`, `italic`, `underline`, `blink`, `reverse`, `hidden`, and `on` for background colours. + +In some cases, you may find it easier to change the foreground on an existing `Style` rather than starting from the appropriate `Colour`. +You can do this using the `fg` method: + +```rust + use ansi_term::Style; + use ansi_term::Colour::{Blue, Cyan, Yellow}; + println!("Yellow on blue: {}", Style::new().on(Blue).fg(Yellow).paint("yow!")); + println!("Also yellow on blue: {}", Cyan.on(Blue).fg(Yellow).paint("zow!")); +``` + +Finally, you can turn a `Colour` into a `Style` with the `normal` method. +This will produce the exact same `ANSIString` as if you just used the `paint` method on the `Colour` directly, but it’s useful in certain cases: for example, you may have a method that returns `Styles`, and need to represent both the “red bold” and “red, but not bold” styles with values of the same type. The `Style` struct also has a `Default` implementation if you want to have a style with *nothing* set. + +```rust +use ansi_term::Style; +use ansi_term::Colour::Red; +Red.normal().paint("yet another red string"); +Style::default().paint("a completely regular string"); +``` + + +## Extended colours + +You can access the extended range of 256 colours by using the `Fixed` colour variant, which takes an argument of the colour number to use. +This can be included wherever you would use a `Colour`: + +```rust +use ansi_term::Colour::Fixed; +Fixed(134).paint("A sort of light purple"); +Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup"); +``` + +The first sixteen of these values are the same as the normal and bold standard colour variants. +There’s nothing stopping you from using these as `Fixed` colours instead, but there’s nothing to be gained by doing so either. + +You can also access full 24-bit color by using the `RGB` colour variant, which takes separate `u8` arguments for red, green, and blue: + +```rust + use ansi_term::Colour::RGB; + RGB(70, 130, 180).paint("Steel blue"); +``` + +## Combining successive coloured strings + +The benefit of writing ANSI escape codes to the terminal is that they *stack*: you do not need to end every coloured string with a reset code if the text that follows it is of a similar style. +For example, if you want to have some blue text followed by some blue bold text, it’s possible to send the ANSI code for blue, followed by the ANSI code for bold, and finishing with a reset code without having to have an extra one between the two strings. + +This crate can optimise the ANSI codes that get printed in situations like this, making life easier for your terminal renderer. +The `ANSIStrings` struct takes a slice of several `ANSIString` values, and will iterate over each of them, printing only the codes for the styles that need to be updated as part of its formatting routine. + +The following code snippet uses this to enclose a binary number displayed in red bold text inside some red, but not bold, brackets: + +```rust +use ansi_term::Colour::Red; +use ansi_term::{ANSIString, ANSIStrings}; +let some_value = format!("{:b}", 42); +let strings: &[ANSIString<'static>] = &[ + Red.paint("["), + Red.bold().paint(some_value), + Red.paint("]"), +]; +println!("Value: {}", ANSIStrings(strings)); +``` + +There are several things to note here. +Firstly, the `paint` method can take *either* an owned `String` or a borrowed `&str`. +Internally, an `ANSIString` holds a copy-on-write (`Cow`) string value to deal with both owned and borrowed strings at the same time. +This is used here to display a `String`, the result of the `format!` call, using the same mechanism as some statically-available `&str` slices. +Secondly, that the `ANSIStrings` value works in the same way as its singular counterpart, with a `Display` implementation that only performs the formatting when required. + +## Byte strings + +This library also supports formatting `[u8]` byte strings; this supports +applications working with text in an unknown encoding. `Style` and +`Color` support painting `[u8]` values, resulting in an `ANSIByteString`. +This type does not implement `Display`, as it may not contain UTF-8, but +it does provide a method `write_to` to write the result to any +`io::Write`: + +```rust +use ansi_term::Colour::Green; +Green.paint("user data".as_bytes()).write_to(&mut std::io::stdout()).unwrap(); +``` + +Similarly, the type `ANSIByteStrings` supports writing a list of +`ANSIByteString` values with minimal escape sequences: + +```rust +use ansi_term::Colour::Green; +use ansi_term::ANSIByteStrings; +ANSIByteStrings(&[ + Green.paint("user data 1\n".as_bytes()), + Green.bold().paint("user data 2\n".as_bytes()), +]).write_to(&mut std::io::stdout()).unwrap(); +``` diff --git a/third_party/rust/ansi_term/examples/colours.rs b/third_party/rust/ansi_term/examples/colours.rs new file mode 100644 index 000000000000..1870310631ff --- /dev/null +++ b/third_party/rust/ansi_term/examples/colours.rs @@ -0,0 +1,13 @@ +extern crate ansi_term; +use ansi_term::Colour::*; + +fn main() { + println!("{}", Black.paint("Black")); + println!("{}", Red.paint("Red")); + println!("{}", Green.paint("Green")); + println!("{}", Yellow.paint("Yellow")); + println!("{}", Blue.paint("Blue")); + println!("{}", Purple.paint("Purple")); + println!("{}", Cyan.paint("Cyan")); + println!("{}", White.paint("White")); +} diff --git a/third_party/rust/ansi_term/src/ansi.rs b/third_party/rust/ansi_term/src/ansi.rs new file mode 100644 index 000000000000..009043ff7599 --- /dev/null +++ b/third_party/rust/ansi_term/src/ansi.rs @@ -0,0 +1,258 @@ +use style::{Colour, Style}; + +use std::fmt; + +use write::AnyWrite; + + +// ---- generating ANSI codes ---- + +impl Style { + + /// Write any ANSI codes that go *before* a piece of text. These should be + /// the codes to set the terminal to a different colour or font style. + fn write_prefix(&self, f: &mut W) -> Result<(), W::Error> { + + // If there are actually no styles here, then don’t write *any* codes + // as the prefix. An empty ANSI code may not affect the terminal + // output at all, but a user may just want a code-free string. + if self.is_plain() { + return Ok(()); + } + + // Write the codes’ prefix, then write numbers, separated by + // semicolons, for each text style we want to apply. + write!(f, "\x1B[")?; + let mut written_anything = false; + + { + let mut write_char = |c| { + if written_anything { write!(f, ";")?; } + written_anything = true; + write!(f, "{}", c)?; + Ok(()) + }; + + if self.is_bold { write_char('1')? } + if self.is_dimmed { write_char('2')? } + if self.is_italic { write_char('3')? } + if self.is_underline { write_char('4')? } + if self.is_blink { write_char('5')? } + if self.is_reverse { write_char('7')? } + if self.is_hidden { write_char('8')? } + if self.is_strikethrough { write_char('9')? } + } + + // The foreground and background colours, if specified, need to be + // handled specially because the number codes are more complicated. + // (see `write_background_code` and `write_foreground_code`) + if let Some(bg) = self.background { + if written_anything { write!(f, ";")?; } + written_anything = true; + bg.write_background_code(f)?; + } + + if let Some(fg) = self.foreground { + if written_anything { write!(f, ";")?; } + fg.write_foreground_code(f)?; + } + + // All the codes end with an `m`, because reasons. + write!(f, "m")?; + + Ok(()) + } + + /// Write any ANSI codes that go *after* a piece of text. These should be + /// the codes to *reset* the terminal back to its normal colour and style. + fn write_suffix(&self, f: &mut W) -> Result<(), W::Error> { + if self.is_plain() { + Ok(()) + } + else { + write!(f, "{}", RESET) + } + } +} + + +/// The code to send to reset all styles and return to `Style::default()`. +pub static RESET: &str = "\x1B[0m"; + + + +impl Colour { + fn write_foreground_code(&self, f: &mut W) -> Result<(), W::Error> { + match *self { + Colour::Black => write!(f, "30"), + Colour::Red => write!(f, "31"), + Colour::Green => write!(f, "32"), + Colour::Yellow => write!(f, "33"), + Colour::Blue => write!(f, "34"), + Colour::Purple => write!(f, "35"), + Colour::Cyan => write!(f, "36"), + Colour::White => write!(f, "37"), + Colour::Fixed(num) => write!(f, "38;5;{}", &num), + Colour::RGB(r,g,b) => write!(f, "38;2;{};{};{}", &r, &g, &b), + } + } + + fn write_background_code(&self, f: &mut W) -> Result<(), W::Error> { + match *self { + Colour::Black => write!(f, "40"), + Colour::Red => write!(f, "41"), + Colour::Green => write!(f, "42"), + Colour::Yellow => write!(f, "43"), + Colour::Blue => write!(f, "44"), + Colour::Purple => write!(f, "45"), + Colour::Cyan => write!(f, "46"), + Colour::White => write!(f, "47"), + Colour::Fixed(num) => write!(f, "48;5;{}", &num), + Colour::RGB(r,g,b) => write!(f, "48;2;{};{};{}", &r, &g, &b), + } + } +} + + +/// Like `ANSIString`, but only displays the style prefix. +#[derive(Clone, Copy, Debug)] +pub struct Prefix(Style); + +/// Like `ANSIString`, but only displays the difference between two +/// styles. +#[derive(Clone, Copy, Debug)] +pub struct Infix(Style, Style); + +/// Like `ANSIString`, but only displays the style suffix. +#[derive(Clone, Copy, Debug)] +pub struct Suffix(Style); + + +impl Style { + + /// The prefix for this style. + pub fn prefix(self) -> Prefix { + Prefix(self) + } + + /// The infix between this style and another. + pub fn infix(self, other: Style) -> Infix { + Infix(self, other) + } + + /// The suffix for this style. + pub fn suffix(self) -> Suffix { + Suffix(self) + } +} + + +impl Colour { + + /// The prefix for this colour. + pub fn prefix(self) -> Prefix { + Prefix(self.normal()) + } + + /// The infix between this colour and another. + pub fn infix(self, other: Colour) -> Infix { + Infix(self.normal(), other.normal()) + } + + /// The suffix for this colour. + pub fn suffix(self) -> Suffix { + Suffix(self.normal()) + } +} + + +impl fmt::Display for Prefix { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let f: &mut fmt::Write = f; + self.0.write_prefix(f) + } +} + + +impl fmt::Display for Infix { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use difference::Difference; + + match Difference::between(&self.0, &self.1) { + Difference::ExtraStyles(style) => { + let f: &mut fmt::Write = f; + style.write_prefix(f) + }, + Difference::Reset => { + let f: &mut fmt::Write = f; + write!(f, "{}{}", RESET, self.0.prefix()) + }, + Difference::NoDifference => { + Ok(()) // nothing to write + }, + } + } +} + + +impl fmt::Display for Suffix { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let f: &mut fmt::Write = f; + self.0.write_suffix(f) + } +} + + + +#[cfg(test)] +mod test { + use style::Style; + use style::Colour::*; + + macro_rules! test { + ($name: ident: $style: expr; $input: expr => $result: expr) => { + #[test] + fn $name() { + assert_eq!($style.paint($input).to_string(), $result.to_string()); + + let mut v = Vec::new(); + $style.paint($input.as_bytes()).write_to(&mut v).unwrap(); + assert_eq!(v.as_slice(), $result.as_bytes()); + } + }; + } + + test!(plain: Style::default(); "text/plain" => "text/plain"); + test!(red: Red; "hi" => "\x1B[31mhi\x1B[0m"); + test!(black: Black.normal(); "hi" => "\x1B[30mhi\x1B[0m"); + test!(yellow_bold: Yellow.bold(); "hi" => "\x1B[1;33mhi\x1B[0m"); + test!(yellow_bold_2: Yellow.normal().bold(); "hi" => "\x1B[1;33mhi\x1B[0m"); + test!(blue_underline: Blue.underline(); "hi" => "\x1B[4;34mhi\x1B[0m"); + test!(green_bold_ul: Green.bold().underline(); "hi" => "\x1B[1;4;32mhi\x1B[0m"); + test!(green_bold_ul_2: Green.underline().bold(); "hi" => "\x1B[1;4;32mhi\x1B[0m"); + test!(purple_on_white: Purple.on(White); "hi" => "\x1B[47;35mhi\x1B[0m"); + test!(purple_on_white_2: Purple.normal().on(White); "hi" => "\x1B[47;35mhi\x1B[0m"); + test!(yellow_on_blue: Style::new().on(Blue).fg(Yellow); "hi" => "\x1B[44;33mhi\x1B[0m"); + test!(yellow_on_blue_2: Cyan.on(Blue).fg(Yellow); "hi" => "\x1B[44;33mhi\x1B[0m"); + test!(cyan_bold_on_white: Cyan.bold().on(White); "hi" => "\x1B[1;47;36mhi\x1B[0m"); + test!(cyan_ul_on_white: Cyan.underline().on(White); "hi" => "\x1B[4;47;36mhi\x1B[0m"); + test!(cyan_bold_ul_on_white: Cyan.bold().underline().on(White); "hi" => "\x1B[1;4;47;36mhi\x1B[0m"); + test!(cyan_ul_bold_on_white: Cyan.underline().bold().on(White); "hi" => "\x1B[1;4;47;36mhi\x1B[0m"); + test!(fixed: Fixed(100); "hi" => "\x1B[38;5;100mhi\x1B[0m"); + test!(fixed_on_purple: Fixed(100).on(Purple); "hi" => "\x1B[45;38;5;100mhi\x1B[0m"); + test!(fixed_on_fixed: Fixed(100).on(Fixed(200)); "hi" => "\x1B[48;5;200;38;5;100mhi\x1B[0m"); + test!(rgb: RGB(70,130,180); "hi" => "\x1B[38;2;70;130;180mhi\x1B[0m"); + test!(rgb_on_blue: RGB(70,130,180).on(Blue); "hi" => "\x1B[44;38;2;70;130;180mhi\x1B[0m"); + test!(blue_on_rgb: Blue.on(RGB(70,130,180)); "hi" => "\x1B[48;2;70;130;180;34mhi\x1B[0m"); + test!(rgb_on_rgb: RGB(70,130,180).on(RGB(5,10,15)); "hi" => "\x1B[48;2;5;10;15;38;2;70;130;180mhi\x1B[0m"); + test!(bold: Style::new().bold(); "hi" => "\x1B[1mhi\x1B[0m"); + test!(underline: Style::new().underline(); "hi" => "\x1B[4mhi\x1B[0m"); + test!(bunderline: Style::new().bold().underline(); "hi" => "\x1B[1;4mhi\x1B[0m"); + test!(dimmed: Style::new().dimmed(); "hi" => "\x1B[2mhi\x1B[0m"); + test!(italic: Style::new().italic(); "hi" => "\x1B[3mhi\x1B[0m"); + test!(blink: Style::new().blink(); "hi" => "\x1B[5mhi\x1B[0m"); + test!(reverse: Style::new().reverse(); "hi" => "\x1B[7mhi\x1B[0m"); + test!(hidden: Style::new().hidden(); "hi" => "\x1B[8mhi\x1B[0m"); + test!(stricken: Style::new().strikethrough(); "hi" => "\x1B[9mhi\x1B[0m"); + +} diff --git a/third_party/rust/ansi_term/src/debug.rs b/third_party/rust/ansi_term/src/debug.rs new file mode 100644 index 000000000000..da30c625cd08 --- /dev/null +++ b/third_party/rust/ansi_term/src/debug.rs @@ -0,0 +1,122 @@ +use std::fmt; + +use style::Style; + + +/// Styles have a special `Debug` implementation that only shows the fields that +/// are set. Fields that haven’t been touched aren’t included in the output. +/// +/// This behaviour gets bypassed when using the alternate formatting mode +/// `format!("{:#?}")`. +/// +/// use ansi_term::Colour::{Red, Blue}; +/// assert_eq!("Style { fg(Red), on(Blue), bold, italic }", +/// format!("{:?}", Red.on(Blue).bold().italic())); +impl fmt::Debug for Style { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + if fmt.alternate() { + fmt.debug_struct("Style") + .field("foreground", &self.foreground) + .field("background", &self.background) + .field("blink", &self.is_blink) + .field("bold", &self.is_bold) + .field("dimmed", &self.is_dimmed) + .field("hidden", &self.is_hidden) + .field("italic", &self.is_italic) + .field("reverse", &self.is_reverse) + .field("strikethrough", &self.is_strikethrough) + .field("underline", &self.is_underline) + .finish() + } + else if self.is_plain() { + fmt.write_str("Style {}") + } + else { + fmt.write_str("Style { ")?; + + let mut written_anything = false; + + if let Some(fg) = self.foreground { + if written_anything { fmt.write_str(", ")? } + written_anything = true; + write!(fmt, "fg({:?})", fg)? + } + + if let Some(bg) = self.background { + if written_anything { fmt.write_str(", ")? } + written_anything = true; + write!(fmt, "on({:?})", bg)? + } + + { + let mut write_flag = |name| { + if written_anything { fmt.write_str(", ")? } + written_anything = true; + fmt.write_str(name) + }; + + if self.is_blink { write_flag("blink")? } + if self.is_bold { write_flag("bold")? } + if self.is_dimmed { write_flag("dimmed")? } + if self.is_hidden { write_flag("hidden")? } + if self.is_italic { write_flag("italic")? } + if self.is_reverse { write_flag("reverse")? } + if self.is_strikethrough { write_flag("strikethrough")? } + if self.is_underline { write_flag("underline")? } + } + + write!(fmt, " }}") + } + } +} + + +#[cfg(test)] +mod test { + use style::Colour::*; + use style::Style; + + fn style() -> Style { + Style::new() + } + + macro_rules! test { + ($name: ident: $obj: expr => $result: expr) => { + #[test] + fn $name() { + assert_eq!($result, format!("{:?}", $obj)); + } + }; + } + + test!(empty: style() => "Style {}"); + test!(bold: style().bold() => "Style { bold }"); + test!(italic: style().italic() => "Style { italic }"); + test!(both: style().bold().italic() => "Style { bold, italic }"); + + test!(red: Red.normal() => "Style { fg(Red) }"); + test!(redblue: Red.normal().on(RGB(3, 2, 4)) => "Style { fg(Red), on(RGB(3, 2, 4)) }"); + + test!(everything: + Red.on(Blue).blink().bold().dimmed().hidden().italic().reverse().strikethrough().underline() => + "Style { fg(Red), on(Blue), blink, bold, dimmed, hidden, italic, reverse, strikethrough, underline }"); + + #[test] + fn long_and_detailed() { + let debug = r##"Style { + foreground: Some( + Blue + ), + background: None, + blink: false, + bold: true, + dimmed: false, + hidden: false, + italic: false, + reverse: false, + strikethrough: false, + underline: false +}"##; + assert_eq!(debug, format!("{:#?}", Blue.bold())); + } +} diff --git a/third_party/rust/ansi_term/src/difference.rs b/third_party/rust/ansi_term/src/difference.rs new file mode 100644 index 000000000000..571574634fda --- /dev/null +++ b/third_party/rust/ansi_term/src/difference.rs @@ -0,0 +1,179 @@ +use super::Style; + + +/// When printing out one coloured string followed by another, use one of +/// these rules to figure out which *extra* control codes need to be sent. +#[derive(PartialEq, Clone, Copy, Debug)] +pub enum Difference { + + /// Print out the control codes specified by this style to end up looking + /// like the second string's styles. + ExtraStyles(Style), + + /// Converting between these two is impossible, so just send a reset + /// command and then the second string's styles. + Reset, + + /// The before style is exactly the same as the after style, so no further + /// control codes need to be printed. + NoDifference, +} + + +impl Difference { + + /// Compute the 'style difference' required to turn an existing style into + /// the given, second style. + /// + /// For example, to turn green text into green bold text, it's redundant + /// to write a reset command then a second green+bold command, instead of + /// just writing one bold command. This method should see that both styles + /// use the foreground colour green, and reduce it to a single command. + /// + /// This method returns an enum value because it's not actually always + /// possible to turn one style into another: for example, text could be + /// made bold and underlined, but you can't remove the bold property + /// without also removing the underline property. So when this has to + /// happen, this function returns None, meaning that the entire set of + /// styles should be reset and begun again. + pub fn between(first: &Style, next: &Style) -> Difference { + use self::Difference::*; + + // XXX(Havvy): This algorithm is kind of hard to replicate without + // having the Plain/Foreground enum variants, so I'm just leaving + // it commented out for now, and defaulting to Reset. + + if first == next { + return NoDifference; + } + + // Cannot un-bold, so must Reset. + if first.is_bold && !next.is_bold { + return Reset; + } + + if first.is_dimmed && !next.is_dimmed { + return Reset; + } + + if first.is_italic && !next.is_italic { + return Reset; + } + + // Cannot un-underline, so must Reset. + if first.is_underline && !next.is_underline { + return Reset; + } + + if first.is_blink && !next.is_blink { + return Reset; + } + + if first.is_reverse && !next.is_reverse { + return Reset; + } + + if first.is_hidden && !next.is_hidden { + return Reset; + } + + if first.is_strikethrough && !next.is_strikethrough { + return Reset; + } + + // Cannot go from foreground to no foreground, so must Reset. + if first.foreground.is_some() && next.foreground.is_none() { + return Reset; + } + + // Cannot go from background to no background, so must Reset. + if first.background.is_some() && next.background.is_none() { + return Reset; + } + + let mut extra_styles = Style::default(); + + if first.is_bold != next.is_bold { + extra_styles.is_bold = true; + } + + if first.is_dimmed != next.is_dimmed { + extra_styles.is_dimmed = true; + } + + if first.is_italic != next.is_italic { + extra_styles.is_italic = true; + } + + if first.is_underline != next.is_underline { + extra_styles.is_underline = true; + } + + if first.is_blink != next.is_blink { + extra_styles.is_blink = true; + } + + if first.is_reverse != next.is_reverse { + extra_styles.is_reverse = true; + } + + if first.is_hidden != next.is_hidden { + extra_styles.is_hidden = true; + } + + if first.is_strikethrough != next.is_strikethrough { + extra_styles.is_strikethrough = true; + } + + if first.foreground != next.foreground { + extra_styles.foreground = next.foreground; + } + + if first.background != next.background { + extra_styles.background = next.background; + } + + ExtraStyles(extra_styles) + } +} + + +#[cfg(test)] +mod test { + use super::*; + use super::Difference::*; + use style::Colour::*; + use style::Style; + + fn style() -> Style { + Style::new() + } + + macro_rules! test { + ($name: ident: $first: expr; $next: expr => $result: expr) => { + #[test] + fn $name() { + assert_eq!($result, Difference::between(&$first, &$next)); + } + }; + } + + test!(nothing: Green.normal(); Green.normal() => NoDifference); + test!(uppercase: Green.normal(); Green.bold() => ExtraStyles(style().bold())); + test!(lowercase: Green.bold(); Green.normal() => Reset); + test!(nothing2: Green.bold(); Green.bold() => NoDifference); + + test!(colour_change: Red.normal(); Blue.normal() => ExtraStyles(Blue.normal())); + + test!(addition_of_blink: style(); style().blink() => ExtraStyles(style().blink())); + test!(addition_of_dimmed: style(); style().dimmed() => ExtraStyles(style().dimmed())); + test!(addition_of_hidden: style(); style().hidden() => ExtraStyles(style().hidden())); + test!(addition_of_reverse: style(); style().reverse() => ExtraStyles(style().reverse())); + test!(addition_of_strikethrough: style(); style().strikethrough() => ExtraStyles(style().strikethrough())); + + test!(removal_of_strikethrough: style().strikethrough(); style() => Reset); + test!(removal_of_reverse: style().reverse(); style() => Reset); + test!(removal_of_hidden: style().hidden(); style() => Reset); + test!(removal_of_dimmed: style().dimmed(); style() => Reset); + test!(removal_of_blink: style().blink(); style() => Reset); +} diff --git a/third_party/rust/ansi_term/src/display.rs b/third_party/rust/ansi_term/src/display.rs new file mode 100644 index 000000000000..8314ab85fad2 --- /dev/null +++ b/third_party/rust/ansi_term/src/display.rs @@ -0,0 +1,279 @@ +use std::borrow::Cow; +use std::fmt; +use std::io; +use std::ops::Deref; + +use ansi::RESET; +use difference::Difference; +use style::{Style, Colour}; +use write::AnyWrite; + + +/// An `ANSIGenericString` includes a generic string type and a `Style` to +/// display that string. `ANSIString` and `ANSIByteString` are aliases for +/// this type on `str` and `[u8]`, respectively. +#[derive(PartialEq, Debug)] +pub struct ANSIGenericString<'a, S: 'a + ToOwned + ?Sized> +where ::Owned: fmt::Debug { + style: Style, + string: Cow<'a, S>, +} + + +/// Cloning an `ANSIGenericString` will clone its underlying string. +/// +/// ### Examples +/// +/// ``` +/// use ansi_term::ANSIString; +/// +/// let plain_string = ANSIString::from("a plain string"); +/// let clone_string = plain_string.clone(); +/// assert_eq!(clone_string, plain_string); +/// ``` +impl<'a, S: 'a + ToOwned + ?Sized> Clone for ANSIGenericString<'a, S> +where ::Owned: fmt::Debug { + fn clone(&self) -> ANSIGenericString<'a, S> { + ANSIGenericString { + style: self.style, + string: self.string.clone(), + } + } +} + +// You might think that the hand-written Clone impl above is the same as the +// one that gets generated with #[derive]. But it’s not *quite* the same! +// +// `str` is not Clone, and the derived Clone implementation puts a Clone +// constraint on the S type parameter (generated using --pretty=expanded): +// +// ↓_________________↓ +// impl <'a, S: ::std::clone::Clone + 'a + ToOwned + ?Sized> ::std::clone::Clone +// for ANSIGenericString<'a, S> where +// ::Owned: fmt::Debug { ... } +// +// This resulted in compile errors when you tried to derive Clone on a type +// that used it: +// +// #[derive(PartialEq, Debug, Clone, Default)] +// pub struct TextCellContents(Vec>); +// ^^^^^^^^^^^^^^^^^^^^^^^^^ +// error[E0277]: the trait `std::clone::Clone` is not implemented for `str` +// +// The hand-written impl above can ignore that constraint and still compile. + + + +/// An ANSI String is a string coupled with the `Style` to display it +/// in a terminal. +/// +/// Although not technically a string itself, it can be turned into +/// one with the `to_string` method. +/// +/// ### Examples +/// +/// ```no_run +/// use ansi_term::ANSIString; +/// use ansi_term::Colour::Red; +/// +/// let red_string = Red.paint("a red string"); +/// println!("{}", red_string); +/// ``` +/// +/// ``` +/// use ansi_term::ANSIString; +/// +/// let plain_string = ANSIString::from("a plain string"); +/// assert_eq!(&*plain_string, "a plain string"); +/// ``` +pub type ANSIString<'a> = ANSIGenericString<'a, str>; + +/// An `ANSIByteString` represents a formatted series of bytes. Use +/// `ANSIByteString` when styling text with an unknown encoding. +pub type ANSIByteString<'a> = ANSIGenericString<'a, [u8]>; + +impl<'a, I, S: 'a + ToOwned + ?Sized> From for ANSIGenericString<'a, S> +where I: Into>, + ::Owned: fmt::Debug { + fn from(input: I) -> ANSIGenericString<'a, S> { + ANSIGenericString { + string: input.into(), + style: Style::default(), + } + } +} + +impl<'a, S: 'a + ToOwned + ?Sized> Deref for ANSIGenericString<'a, S> +where ::Owned: fmt::Debug { + type Target = S; + + fn deref(&self) -> &S { + self.string.deref() + } +} + + +/// A set of `ANSIGenericString`s collected together, in order to be +/// written with a minimum of control characters. +pub struct ANSIGenericStrings<'a, S: 'a + ToOwned + ?Sized> + (pub &'a [ANSIGenericString<'a, S>]) + where ::Owned: fmt::Debug; + +/// A set of `ANSIString`s collected together, in order to be written with a +/// minimum of control characters. +pub type ANSIStrings<'a> = ANSIGenericStrings<'a, str>; + +/// A function to construct an `ANSIStrings` instance. +#[allow(non_snake_case)] +pub fn ANSIStrings<'a>(arg: &'a [ANSIString<'a>]) -> ANSIStrings<'a> { + ANSIGenericStrings(arg) +} + +/// A set of `ANSIByteString`s collected together, in order to be +/// written with a minimum of control characters. +pub type ANSIByteStrings<'a> = ANSIGenericStrings<'a, [u8]>; + +/// A function to construct an `ANSIByteStrings` instance. +#[allow(non_snake_case)] +pub fn ANSIByteStrings<'a>(arg: &'a [ANSIByteString<'a>]) -> ANSIByteStrings<'a> { + ANSIGenericStrings(arg) +} + + +// ---- paint functions ---- + +impl Style { + + /// Paints the given text with this colour, returning an ANSI string. + pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>(self, input: I) -> ANSIGenericString<'a, S> + where I: Into>, + ::Owned: fmt::Debug { + ANSIGenericString { + string: input.into(), + style: self, + } + } +} + + +impl Colour { + + /// Paints the given text with this colour, returning an ANSI string. + /// This is a short-cut so you don’t have to use `Blue.normal()` just + /// to get blue text. + /// + /// ``` + /// use ansi_term::Colour::Blue; + /// println!("{}", Blue.paint("da ba dee")); + /// ``` + pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>(self, input: I) -> ANSIGenericString<'a, S> + where I: Into>, + ::Owned: fmt::Debug { + ANSIGenericString { + string: input.into(), + style: self.normal(), + } + } +} + + +// ---- writers for individual ANSI strings ---- + +impl<'a> fmt::Display for ANSIString<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let w: &mut fmt::Write = f; + self.write_to_any(w) + } +} + +impl<'a> ANSIByteString<'a> { + /// Write an `ANSIByteString` to an `io::Write`. This writes the escape + /// sequences for the associated `Style` around the bytes. + pub fn write_to(&self, w: &mut W) -> io::Result<()> { + let w: &mut io::Write = w; + self.write_to_any(w) + } +} + +impl<'a, S: 'a + ToOwned + ?Sized> ANSIGenericString<'a, S> +where ::Owned: fmt::Debug, &'a S: AsRef<[u8]> { + fn write_to_any + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> { + write!(w, "{}", self.style.prefix())?; + w.write_str(self.string.as_ref())?; + write!(w, "{}", self.style.suffix()) + } +} + + +// ---- writers for combined ANSI strings ---- + +impl<'a> fmt::Display for ANSIStrings<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let f: &mut fmt::Write = f; + self.write_to_any(f) + } +} + +impl<'a> ANSIByteStrings<'a> { + /// Write `ANSIByteStrings` to an `io::Write`. This writes the minimal + /// escape sequences for the associated `Style`s around each set of + /// bytes. + pub fn write_to(&self, w: &mut W) -> io::Result<()> { + let w: &mut io::Write = w; + self.write_to_any(w) + } +} + +impl<'a, S: 'a + ToOwned + ?Sized> ANSIGenericStrings<'a, S> +where ::Owned: fmt::Debug, &'a S: AsRef<[u8]> { + fn write_to_any + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> { + use self::Difference::*; + + let first = match self.0.first() { + None => return Ok(()), + Some(f) => f, + }; + + write!(w, "{}", first.style.prefix())?; + w.write_str(first.string.as_ref())?; + + for window in self.0.windows(2) { + match Difference::between(&window[0].style, &window[1].style) { + ExtraStyles(style) => write!(w, "{}", style.prefix())?, + Reset => write!(w, "{}{}", RESET, window[1].style.prefix())?, + NoDifference => {/* Do nothing! */}, + } + + w.write_str(&window[1].string)?; + } + + // Write the final reset string after all of the ANSIStrings have been + // written, *except* if the last one has no styles, because it would + // have already been written by this point. + if let Some(last) = self.0.last() { + if !last.style.is_plain() { + write!(w, "{}", RESET)?; + } + } + + Ok(()) + } +} + + +// ---- tests ---- + +#[cfg(test)] +mod tests { + pub use super::super::ANSIStrings; + pub use style::Style; + pub use style::Colour::*; + + #[test] + fn no_control_codes_for_plain() { + let one = Style::default().paint("one"); + let two = Style::default().paint("two"); + let output = format!("{}", ANSIStrings( &[ one, two ] )); + assert_eq!(&*output, "onetwo"); + } +} diff --git a/third_party/rust/ansi_term/src/lib.rs b/third_party/rust/ansi_term/src/lib.rs new file mode 100644 index 000000000000..f2a676a1c5e8 --- /dev/null +++ b/third_party/rust/ansi_term/src/lib.rs @@ -0,0 +1,205 @@ +//! This is a library for controlling colours and formatting, such as +//! red bold text or blue underlined text, on ANSI terminals. +//! +//! +//! ## Basic usage +//! +//! There are two main data structures in this crate that you need to be +//! concerned with: `ANSIString` and `Style`. A `Style` holds stylistic +//! information: colours, whether the text should be bold, or blinking, or +//! whatever. There are also `Colour` variants that represent simple foreground +//! colour styles. An `ANSIString` is a string paired with a `Style`. +//! +//! (Yes, it’s British English, but you won’t have to write “colour” very often. +//! `Style` is used the majority of the time.) +//! +//! To format a string, call the `paint` method on a `Style` or a `Colour`, +//! passing in the string you want to format as the argument. For example, +//! here’s how to get some red text: +//! +//! use ansi_term::Colour::Red; +//! println!("This is in red: {}", Red.paint("a red string")); +//! +//! It’s important to note that the `paint` method does *not* actually return a +//! string with the ANSI control characters surrounding it. Instead, it returns +//! an `ANSIString` value that has a `Display` implementation that, when +//! formatted, returns the characters. This allows strings to be printed with a +//! minimum of `String` allocations being performed behind the scenes. +//! +//! If you *do* want to get at the escape codes, then you can convert the +//! `ANSIString` to a string as you would any other `Display` value: +//! +//! use ansi_term::Colour::Red; +//! use std::string::ToString; +//! let red_string = Red.paint("a red string").to_string(); +//! +//! +//! ## Bold, underline, background, and other styles +//! +//! For anything more complex than plain foreground colour changes, you need to +//! construct `Style` objects themselves, rather than beginning with a `Colour`. +//! You can do this by chaining methods based on a new `Style`, created with +//! `Style::new()`. Each method creates a new style that has that specific +//! property set. For example: +//! +//! use ansi_term::Style; +//! println!("How about some {} and {}?", +//! Style::new().bold().paint("bold"), +//! Style::new().underline().paint("underline")); +//! +//! For brevity, these methods have also been implemented for `Colour` values, +//! so you can give your styles a foreground colour without having to begin with +//! an empty `Style` value: +//! +//! use ansi_term::Colour::{Blue, Yellow}; +//! println!("Demonstrating {} and {}!", +//! Blue.bold().paint("blue bold"), +//! Yellow.underline().paint("yellow underline")); +//! println!("Yellow on blue: {}", Yellow.on(Blue).paint("wow!")); +//! +//! The complete list of styles you can use are: `bold`, `dimmed`, `italic`, +//! `underline`, `blink`, `reverse`, `hidden`, `strikethrough`, and `on` for +//! background colours. +//! +//! In some cases, you may find it easier to change the foreground on an +//! existing `Style` rather than starting from the appropriate `Colour`. +//! You can do this using the `fg` method: +//! +//! use ansi_term::Style; +//! use ansi_term::Colour::{Blue, Cyan, Yellow}; +//! println!("Yellow on blue: {}", Style::new().on(Blue).fg(Yellow).paint("yow!")); +//! println!("Also yellow on blue: {}", Cyan.on(Blue).fg(Yellow).paint("zow!")); +//! +//! Finally, you can turn a `Colour` into a `Style` with the `normal` method. +//! This will produce the exact same `ANSIString` as if you just used the +//! `paint` method on the `Colour` directly, but it’s useful in certain cases: +//! for example, you may have a method that returns `Styles`, and need to +//! represent both the “red bold” and “red, but not bold” styles with values of +//! the same type. The `Style` struct also has a `Default` implementation if you +//! want to have a style with *nothing* set. +//! +//! use ansi_term::Style; +//! use ansi_term::Colour::Red; +//! Red.normal().paint("yet another red string"); +//! Style::default().paint("a completely regular string"); +//! +//! +//! ## Extended colours +//! +//! You can access the extended range of 256 colours by using the `Fixed` colour +//! variant, which takes an argument of the colour number to use. This can be +//! included wherever you would use a `Colour`: +//! +//! use ansi_term::Colour::Fixed; +//! Fixed(134).paint("A sort of light purple"); +//! Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup"); +//! +//! The first sixteen of these values are the same as the normal and bold +//! standard colour variants. There’s nothing stopping you from using these as +//! `Fixed` colours instead, but there’s nothing to be gained by doing so +//! either. +//! +//! You can also access full 24-bit color by using the `RGB` colour variant, +//! which takes separate `u8` arguments for red, green, and blue: +//! +//! use ansi_term::Colour::RGB; +//! RGB(70, 130, 180).paint("Steel blue"); +//! +//! ## Combining successive coloured strings +//! +//! The benefit of writing ANSI escape codes to the terminal is that they +//! *stack*: you do not need to end every coloured string with a reset code if +//! the text that follows it is of a similar style. For example, if you want to +//! have some blue text followed by some blue bold text, it’s possible to send +//! the ANSI code for blue, followed by the ANSI code for bold, and finishing +//! with a reset code without having to have an extra one between the two +//! strings. +//! +//! This crate can optimise the ANSI codes that get printed in situations like +//! this, making life easier for your terminal renderer. The `ANSIStrings` +//! struct takes a slice of several `ANSIString` values, and will iterate over +//! each of them, printing only the codes for the styles that need to be updated +//! as part of its formatting routine. +//! +//! The following code snippet uses this to enclose a binary number displayed in +//! red bold text inside some red, but not bold, brackets: +//! +//! use ansi_term::Colour::Red; +//! use ansi_term::{ANSIString, ANSIStrings}; +//! let some_value = format!("{:b}", 42); +//! let strings: &[ANSIString<'static>] = &[ +//! Red.paint("["), +//! Red.bold().paint(some_value), +//! Red.paint("]"), +//! ]; +//! println!("Value: {}", ANSIStrings(strings)); +//! +//! There are several things to note here. Firstly, the `paint` method can take +//! *either* an owned `String` or a borrowed `&str`. Internally, an `ANSIString` +//! holds a copy-on-write (`Cow`) string value to deal with both owned and +//! borrowed strings at the same time. This is used here to display a `String`, +//! the result of the `format!` call, using the same mechanism as some +//! statically-available `&str` slices. Secondly, that the `ANSIStrings` value +//! works in the same way as its singular counterpart, with a `Display` +//! implementation that only performs the formatting when required. +//! +//! ## Byte strings +//! +//! This library also supports formatting `[u8]` byte strings; this supports +//! applications working with text in an unknown encoding. `Style` and +//! `Color` support painting `[u8]` values, resulting in an `ANSIByteString`. +//! This type does not implement `Display`, as it may not contain UTF-8, but +//! it does provide a method `write_to` to write the result to any +//! `io::Write`: +//! +//! use ansi_term::Colour::Green; +//! Green.paint("user data".as_bytes()).write_to(&mut std::io::stdout()).unwrap(); +//! +//! Similarly, the type `ANSIByteStrings` supports writing a list of +//! `ANSIByteString` values with minimal escape sequences: +//! +//! use ansi_term::Colour::Green; +//! use ansi_term::ANSIByteStrings; +//! ANSIByteStrings(&[ +//! Green.paint("user data 1\n".as_bytes()), +//! Green.bold().paint("user data 2\n".as_bytes()), +//! ]).write_to(&mut std::io::stdout()).unwrap(); + + +#![crate_name = "ansi_term"] +#![crate_type = "rlib"] +#![crate_type = "dylib"] + +#![warn(missing_copy_implementations)] +#![warn(missing_docs)] +#![warn(trivial_casts, trivial_numeric_casts)] +#![warn(unused_extern_crates, unused_qualifications)] + +#[cfg(target_os="windows")] +extern crate winapi; + +mod ansi; +pub use ansi::{Prefix, Infix, Suffix}; + +mod style; +pub use style::{Colour, Style}; + +/// Color is a type alias for Colour for those who can't be bothered. +pub use Colour as Color; + +// I'm not beyond calling Colour Colour, rather than Color, but I did +// purposefully name this crate 'ansi-term' so people wouldn't get +// confused when they tried to install it. +// +// Only *after* they'd installed it. + +mod difference; +mod display; +pub use display::*; + +mod write; + +mod windows; +pub use windows::*; + +mod debug; diff --git a/third_party/rust/ansi_term/src/style.rs b/third_party/rust/ansi_term/src/style.rs new file mode 100644 index 000000000000..b9fb52326a1b --- /dev/null +++ b/third_party/rust/ansi_term/src/style.rs @@ -0,0 +1,259 @@ +/// A style is a collection of properties that can format a string +/// using ANSI escape codes. +#[derive(PartialEq, Clone, Copy)] +pub struct Style { + + /// The style's foreground colour, if it has one. + pub foreground: Option, + + /// The style's background colour, if it has one. + pub background: Option, + + /// Whether this style is bold. + pub is_bold: bool, + + /// Whether this style is dimmed. + pub is_dimmed: bool, + + /// Whether this style is italic. + pub is_italic: bool, + + /// Whether this style is underlined. + pub is_underline: bool, + + /// Whether this style is blinking. + pub is_blink: bool, + + /// Whether this style has reverse colours. + pub is_reverse: bool, + + /// Whether this style is hidden. + pub is_hidden: bool, + + /// Whether this style is struckthrough. + pub is_strikethrough: bool +} + +impl Style { + /// Creates a new Style with no differences. + pub fn new() -> Style { + Style::default() + } + + /// Returns a `Style` with the bold property set. + pub fn bold(&self) -> Style { + Style { is_bold: true, .. *self } + } + + /// Returns a `Style` with the dimmed property set. + pub fn dimmed(&self) -> Style { + Style { is_dimmed: true, .. *self } + } + + /// Returns a `Style` with the italic property set. + pub fn italic(&self) -> Style { + Style { is_italic: true, .. *self } + } + + /// Returns a `Style` with the underline property set. + pub fn underline(&self) -> Style { + Style { is_underline: true, .. *self } + } + + /// Returns a `Style` with the blink property set. + pub fn blink(&self) -> Style { + Style { is_blink: true, .. *self } + } + + /// Returns a `Style` with the reverse property set. + pub fn reverse(&self) -> Style { + Style { is_reverse: true, .. *self } + } + + /// Returns a `Style` with the hidden property set. + pub fn hidden(&self) -> Style { + Style { is_hidden: true, .. *self } + } + + /// Returns a `Style` with the hidden property set. + pub fn strikethrough(&self) -> Style { + Style { is_strikethrough: true, .. *self } + } + + /// Returns a `Style` with the foreground colour property set. + pub fn fg(&self, foreground: Colour) -> Style { + Style { foreground: Some(foreground), .. *self } + } + + /// Returns a `Style` with the background colour property set. + pub fn on(&self, background: Colour) -> Style { + Style { background: Some(background), .. *self } + } + + /// Return true if this `Style` has no actual styles, and can be written + /// without any control characters. + pub fn is_plain(self) -> bool { + self == Style::default() + } +} + +impl Default for Style { + + /// Returns a style with *no* properties set. Formatting text using this + /// style returns the exact same text. + /// + /// ``` + /// use ansi_term::Style; + /// assert_eq!(None, Style::default().foreground); + /// assert_eq!(None, Style::default().background); + /// assert_eq!(false, Style::default().is_bold); + /// assert_eq!("txt", Style::default().paint("txt").to_string()); + /// ``` + fn default() -> Style { + Style { + foreground: None, + background: None, + is_bold: false, + is_dimmed: false, + is_italic: false, + is_underline: false, + is_blink: false, + is_reverse: false, + is_hidden: false, + is_strikethrough: false, + } + } +} + + +// ---- colours ---- + +/// A colour is one specific type of ANSI escape code, and can refer +/// to either the foreground or background colour. +/// +/// These use the standard numeric sequences. +/// See +#[derive(PartialEq, Clone, Copy, Debug)] +pub enum Colour { + + /// Colour #0 (foreground code `30`, background code `40`). + /// + /// This is not necessarily the background colour, and using it as one may + /// render the text hard to read on terminals with dark backgrounds. + Black, + + /// Colour #1 (foreground code `31`, background code `41`). + Red, + + /// Colour #2 (foreground code `32`, background code `42`). + Green, + + /// Colour #3 (foreground code `33`, background code `43`). + Yellow, + + /// Colour #4 (foreground code `34`, background code `44`). + Blue, + + /// Colour #5 (foreground code `35`, background code `45`). + Purple, + + /// Colour #6 (foreground code `36`, background code `46`). + Cyan, + + /// Colour #7 (foreground code `37`, background code `47`). + /// + /// As above, this is not necessarily the foreground colour, and may be + /// hard to read on terminals with light backgrounds. + White, + + /// A colour number from 0 to 255, for use in 256-colour terminal + /// environments. + /// + /// - Colours 0 to 7 are the `Black` to `White` variants respectively. + /// These colours can usually be changed in the terminal emulator. + /// - Colours 8 to 15 are brighter versions of the eight colours above. + /// These can also usually be changed in the terminal emulator, or it + /// could be configured to use the original colours and show the text in + /// bold instead. It varies depending on the program. + /// - Colours 16 to 231 contain several palettes of bright colours, + /// arranged in six squares measuring six by six each. + /// - Colours 232 to 255 are shades of grey from black to white. + /// + /// It might make more sense to look at a [colour chart][cc]. + /// + /// [cc]: https://upload.wikimedia.org/wikipedia/en/1/15/Xterm_256color_chart.svg + Fixed(u8), + + /// A 24-bit RGB color, as specified by ISO-8613-3. + RGB(u8, u8, u8), +} + + +impl Colour { + /// Return a `Style` with the foreground colour set to this colour. + pub fn normal(self) -> Style { + Style { foreground: Some(self), .. Style::default() } + } + + /// Returns a `Style` with the bold property set. + pub fn bold(self) -> Style { + Style { foreground: Some(self), is_bold: true, .. Style::default() } + } + + /// Returns a `Style` with the dimmed property set. + pub fn dimmed(self) -> Style { + Style { foreground: Some(self), is_dimmed: true, .. Style::default() } + } + + /// Returns a `Style` with the italic property set. + pub fn italic(self) -> Style { + Style { foreground: Some(self), is_italic: true, .. Style::default() } + } + + /// Returns a `Style` with the underline property set. + pub fn underline(self) -> Style { + Style { foreground: Some(self), is_underline: true, .. Style::default() } + } + + /// Returns a `Style` with the blink property set. + pub fn blink(self) -> Style { + Style { foreground: Some(self), is_blink: true, .. Style::default() } + } + + /// Returns a `Style` with the reverse property set. + pub fn reverse(self) -> Style { + Style { foreground: Some(self), is_reverse: true, .. Style::default() } + } + + /// Returns a `Style` with the hidden property set. + pub fn hidden(self) -> Style { + Style { foreground: Some(self), is_hidden: true, .. Style::default() } + } + + /// Returns a `Style` with the strikethrough property set. + pub fn strikethrough(self) -> Style { + Style { foreground: Some(self), is_strikethrough: true, .. Style::default() } + } + + /// Returns a `Style` with the background colour property set. + pub fn on(self, background: Colour) -> Style { + Style { foreground: Some(self), background: Some(background), .. Style::default() } + } +} + +impl From for Style { + + /// You can turn a `Colour` into a `Style` with the foreground colour set + /// with the `From` trait. + /// + /// ``` + /// use ansi_term::{Style, Colour}; + /// let green_foreground = Style::default().fg(Colour::Green); + /// assert_eq!(green_foreground, Colour::Green.normal()); + /// assert_eq!(green_foreground, Colour::Green.into()); + /// assert_eq!(green_foreground, Style::from(Colour::Green)); + /// ``` + fn from(colour: Colour) -> Style { + colour.normal() + } +} diff --git a/third_party/rust/ansi_term/src/windows.rs b/third_party/rust/ansi_term/src/windows.rs new file mode 100644 index 000000000000..ff6fa683d8ba --- /dev/null +++ b/third_party/rust/ansi_term/src/windows.rs @@ -0,0 +1,40 @@ +/// Enables ANSI code support on Windows 10. +/// +/// This uses Windows API calls to alter the properties of the console that +/// the program is running in. +/// +/// https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx +/// +/// Returns a `Result` with the Windows error code if unsuccessful. +#[cfg(windows)] +pub fn enable_ansi_support() -> Result<(), u32> { + use winapi::um::processenv::GetStdHandle; + use winapi::um::errhandlingapi::GetLastError; + use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode}; + + const STD_OUT_HANDLE: u32 = -11i32 as u32; + const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004; + + unsafe { + // https://docs.microsoft.com/en-us/windows/console/getstdhandle + let std_out_handle = GetStdHandle(STD_OUT_HANDLE); + let error_code = GetLastError(); + if error_code != 0 { return Err(error_code); } + + // https://docs.microsoft.com/en-us/windows/console/getconsolemode + let mut console_mode: u32 = 0; + GetConsoleMode(std_out_handle, &mut console_mode); + let error_code = GetLastError(); + if error_code != 0 { return Err(error_code); } + + // VT processing not already enabled? + if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 { + // https://docs.microsoft.com/en-us/windows/console/setconsolemode + SetConsoleMode(std_out_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); + let error_code = GetLastError(); + if error_code != 0 { return Err(error_code); } + } + } + + return Ok(()); +} diff --git a/third_party/rust/ansi_term/src/write.rs b/third_party/rust/ansi_term/src/write.rs new file mode 100644 index 000000000000..bb146ac1567a --- /dev/null +++ b/third_party/rust/ansi_term/src/write.rs @@ -0,0 +1,40 @@ +use std::fmt; +use std::io; + + +pub trait AnyWrite { + type wstr: ?Sized; + type Error; + + fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error>; + + fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error>; +} + + +impl<'a> AnyWrite for fmt::Write + 'a { + type wstr = str; + type Error = fmt::Error; + + fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error> { + fmt::Write::write_fmt(self, fmt) + } + + fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error> { + fmt::Write::write_str(self, s) + } +} + + +impl<'a> AnyWrite for io::Write + 'a { + type wstr = [u8]; + type Error = io::Error; + + fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error> { + io::Write::write_fmt(self, fmt) + } + + fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error> { + io::Write::write_all(self, s) + } +} diff --git a/third_party/rust/env_logger-0.6.2/.cargo-checksum.json b/third_party/rust/env_logger-0.6.2/.cargo-checksum.json new file mode 100644 index 000000000000..e5514ae7e219 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"7c044d74477515ab39287a4caff27eb96daebaed8b9f9b6a1d1c081a7b42d4a7","Cargo.lock":"132c1f881b80a79314567a6993141c6204495fec144cdcec1729f2a3e0fec18b","Cargo.toml":"b60137f1fd54001ca4d8be1d0bbec154225a44c8f4fa3576078bdad55216d357","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"0e231c1c4ad51ff0239062297bdaa69aeb34a8692e3f814188ce1e0ade8583d5","examples/custom_default_format.rs":"799c439f61cb711078f8aa584db537a5758c25b90d44767849dae2ad3822885c","examples/custom_format.rs":"ac8323e2febf8b8ff7238bd254fbbbfb3183da5af84f7f3a261fd9ad892c9ab6","examples/custom_logger.rs":"99fb3c9761ad4c5fe73f4ec2a2bd44b4acf6d1f7b7cfaa16bf0373665d3e2a4b","examples/default.rs":"ac96427611784d310704f738c7a29ebddd7930c8a70ad3c464c4d3eae4cf74a3","examples/direct_logger.rs":"549f6a10e0903d06aca2cc7ba82415b07a23392676101c9bc7aa72b4a9b0b9e2","examples/filters_from_code.rs":"84bd82803683d19ae96f85edcf4ee38cda028c2dbde923dddecc8563453b18e2","src/filter/mod.rs":"de471579c5db400c5ed11b9d7c9fc62686068b42798c58f7165806319ab7ec09","src/filter/regex.rs":"5fff47d1d4d0aa3f2bab90636127d3e72aebf800c3b78faba99637220ffdf865","src/filter/string.rs":"52bbd047c31a1afdb3cd1c11629b956f21b3f47bf22e06421baf3d693a045e59","src/fmt/humantime/extern_impl.rs":"cd2538e7a03fd3ad6c843af3c3d4016ca96cadaefee32cf9b37329c4787e6552","src/fmt/humantime/mod.rs":"408496eb21344c654b9e06da2a2df86de56e427147bb7f7b47851e0da976c003","src/fmt/humantime/shim_impl.rs":"7c2fdf4031f5568b716df14842b0d32bc03ff398763f4849960df7f9632a5bb2","src/fmt/mod.rs":"5104dad2fd14bc18ab6ab46e7c2bc5752b509d9fc934fb99f0ebc126728f8f04","src/fmt/writer/atty.rs":"3e9fd61d291d0919f7aa7119a26dd15d920df8783b4ae57bcf2c3cb6f3ff06b5","src/fmt/writer/mod.rs":"583f6616e0cf21955a530baa332fb7a99bf4fcd418a2367bbd1e733a06a22318","src/fmt/writer/termcolor/extern_impl.rs":"15e048be128568abcdd0ce99dafffe296df26131d4aa05921585761d31c11db5","src/fmt/writer/termcolor/mod.rs":"a3cf956aec030e0f940e4eaefe58d7703857eb900022286e328e05e5f61de183","src/fmt/writer/termcolor/shim_impl.rs":"bdd479c4e933b14ba02a3c1a9fe30eb51bcdf600e23cebd044d68683fdaad037","src/lib.rs":"2c5ab92ee141022f3e657b0f81e84e5ee4e7fad9fb648204e00ed4fb03d4166f","tests/init-twice-retains-filter.rs":"00524ce0f6779981b695bad1fdd244f87b76c126aeccd8b4ff77ef9e6325478b","tests/log-in-log.rs":"41126910998adfbac771c2a1237fecbc5437344f8e4dfc2f93235bab764a087e","tests/regexp_filter.rs":"44aa6c39de894be090e37083601e501cfffb15e3c0cd36209c48abdf3e2cb120"},"package":"aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3"} \ No newline at end of file diff --git a/third_party/rust/env_logger-0.6.2/CHANGELOG.md b/third_party/rust/env_logger-0.6.2/CHANGELOG.md new file mode 100644 index 000000000000..f849eefff6eb --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/CHANGELOG.md @@ -0,0 +1,3 @@ +Changes to this crate are tracked via [GitHub Releases][releases]. + +[releases]: https://github.com/sebasmagri/env_logger/releases diff --git a/third_party/rust/env_logger-0.6.2/Cargo.lock b/third_party/rust/env_logger-0.6.2/Cargo.lock new file mode 100644 index 000000000000..565fc3a87645 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/Cargo.lock @@ -0,0 +1,212 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "atty" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cfg-if" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "env_logger" +version = "0.6.2" +dependencies = [ + "atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "humantime" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memchr" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quick-error" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_syscall" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "termcolor" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ucd-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "utf8-ranges" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "version_check" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "wincolor" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" +"checksum atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "af80143d6f7608d746df1520709e5d141c96f240b0e62b0aa41bdfb53374d9d4" +"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" +"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" +"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b" +"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" +"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16" +"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" +"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" +"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f" +"checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861" +"checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" +"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" +"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" +"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "561ed901ae465d6185fa7864d63fbd5720d0ef718366c9a4dc83cf6170d7e9ba" diff --git a/third_party/rust/env_logger-0.6.2/Cargo.toml b/third_party/rust/env_logger-0.6.2/Cargo.toml new file mode 100644 index 000000000000..1d028baaaa11 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/Cargo.toml @@ -0,0 +1,57 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "env_logger" +version = "0.6.2" +authors = ["The Rust Project Developers"] +description = "A logging implementation for `log` which is configured via an environment\nvariable.\n" +documentation = "https://docs.rs/env_logger" +readme = "README.md" +keywords = ["logging", "log", "logger"] +categories = ["development-tools::debugging"] +license = "MIT/Apache-2.0" +repository = "https://github.com/sebasmagri/env_logger/" + +[[test]] +name = "regexp_filter" +harness = false + +[[test]] +name = "log-in-log" +harness = false + +[[test]] +name = "init-twice-retains-filter" +harness = false +[dependencies.atty] +version = "0.2.5" +optional = true + +[dependencies.humantime] +version = "1.1" +optional = true + +[dependencies.log] +version = "0.4" +features = ["std"] + +[dependencies.regex] +version = "1.0.3" +optional = true + +[dependencies.termcolor] +version = "1.0.2" +optional = true + +[features] +default = ["termcolor", "atty", "humantime", "regex"] diff --git a/third_party/rust/env_logger-0.6.2/LICENSE-APACHE b/third_party/rust/env_logger-0.6.2/LICENSE-APACHE new file mode 100644 index 000000000000..16fe87b06e80 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/third_party/rust/env_logger-0.6.2/LICENSE-MIT b/third_party/rust/env_logger-0.6.2/LICENSE-MIT new file mode 100644 index 000000000000..39d4bdb5acd3 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2014 The Rust Project Developers + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/env_logger-0.6.2/README.md b/third_party/rust/env_logger-0.6.2/README.md new file mode 100644 index 000000000000..8dc92f12a811 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/README.md @@ -0,0 +1,152 @@ +env_logger [![Build Status](https://travis-ci.org/sebasmagri/env_logger.svg?branch=master)](https://travis-ci.org/sebasmagri/env_logger) [![Maintenance](https://img.shields.io/badge/maintenance-actively%20maintained-brightgreen.svg)](https://github.com/sebasmagri/env_logger) [![crates.io](https://img.shields.io/crates/v/env_logger.svg)](https://crates.io/crates/env_logger) [![Documentation](https://img.shields.io/badge/docs-current-blue.svg)](https://docs.rs/env_logger) +========== + +Implements a logger that can be configured via environment variables. + +## Usage + +### In libraries + +`env_logger` makes sense when used in executables (binary projects). Libraries should use the [`log`](https://doc.rust-lang.org/log) crate instead. + +### In executables + +It must be added along with `log` to the project dependencies: + +```toml +[dependencies] +log = "0.4.0" +env_logger = "0.6.2" +``` + +`env_logger` must be initialized as early as possible in the project. After it's initialized, you can use the `log` macros to do actual logging. + +```rust +#[macro_use] +extern crate log; +extern crate env_logger; + +fn main() { + env_logger::init(); + + info!("starting up"); + + // ... +} +``` + +Then when running the executable, specify a value for the `RUST_LOG` +environment variable that corresponds with the log messages you want to show. + +```bash +$ RUST_LOG=info ./main +[2018-11-03T06:09:06Z INFO default] starting up +``` + +`env_logger` can be configured in other ways besides an environment variable. See [the examples](https://github.com/sebasmagri/env_logger/tree/master/examples) for more approaches. + +### In tests + +Tests can use the `env_logger` crate to see log messages generated during that test: + +```toml +[dependencies] +log = "0.4.0" + +[dev-dependencies] +env_logger = "0.6.2" +``` + +```rust +#[macro_use] +extern crate log; + +fn add_one(num: i32) -> i32 { + info!("add_one called with {}", num); + num + 1 +} + +#[cfg(test)] +mod tests { + use super::*; + extern crate env_logger; + + fn init() { + let _ = env_logger::builder().is_test(true).try_init(); + } + + #[test] + fn it_adds_one() { + init(); + + info!("can log from the test too"); + assert_eq!(3, add_one(2)); + } + + #[test] + fn it_handles_negative_numbers() { + init(); + + info!("logging from another test"); + assert_eq!(-7, add_one(-8)); + } +} +``` + +Assuming the module under test is called `my_lib`, running the tests with the +`RUST_LOG` filtering to info messages from this module looks like: + +```bash +$ RUST_LOG=my_lib=info cargo test + Running target/debug/my_lib-... + +running 2 tests +[INFO my_lib::tests] logging from another test +[INFO my_lib] add_one called with -8 +test tests::it_handles_negative_numbers ... ok +[INFO my_lib::tests] can log from the test too +[INFO my_lib] add_one called with 2 +test tests::it_adds_one ... ok + +test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured +``` + +Note that `env_logger::try_init()` needs to be called in each test in which you +want to enable logging. Additionally, the default behavior of tests to +run in parallel means that logging output may be interleaved with test output. +Either run tests in a single thread by specifying `RUST_TEST_THREADS=1` or by +running one test by specifying its name as an argument to the test binaries as +directed by the `cargo test` help docs: + +```bash +$ RUST_LOG=my_lib=info cargo test it_adds_one + Running target/debug/my_lib-... + +running 1 test +[INFO my_lib::tests] can log from the test too +[INFO my_lib] add_one called with 2 +test tests::it_adds_one ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured +``` + +## Configuring log target + +By default, `env_logger` logs to stderr. If you want to log to stdout instead, +you can use the `Builder` to change the log target: + +```rust +use std::env; +use env_logger::{Builder, Target}; + +let mut builder = Builder::from_default_env(); +builder.target(Target::Stdout); + +builder.init(); +``` + +## Stability of the default format + +The default format won't optimise for long-term stability, and explicitly makes no guarantees about the stability of its output across major, minor or patch version bumps during `0.x`. + +If you want to capture or interpret the output of `env_logger` programmatically then you should use a custom format. diff --git a/third_party/rust/env_logger-0.6.2/examples/custom_default_format.rs b/third_party/rust/env_logger-0.6.2/examples/custom_default_format.rs new file mode 100644 index 000000000000..d1a45b6089f2 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/examples/custom_default_format.rs @@ -0,0 +1,44 @@ +/*! +Disabling parts of the default format. + +Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`: + +```no_run,shell +$ export MY_LOG_LEVEL='info' +``` + +Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors +or `auto` to enable them: + +```no_run,shell +$ export MY_LOG_STYLE=never +``` + +If you want to control the logging output completely, see the `custom_logger` example. +*/ + +#[macro_use] +extern crate log; +extern crate env_logger; + +use env_logger::{Env, Builder}; + +fn init_logger() { + let env = Env::default() + .filter("MY_LOG_LEVEL") + .write_style("MY_LOG_STYLE"); + + let mut builder = Builder::from_env(env); + + builder + .default_format_level(false) + .default_format_timestamp_nanos(true); + + builder.init(); +} + +fn main() { + init_logger(); + + info!("a log from `MyLogger`"); +} diff --git a/third_party/rust/env_logger-0.6.2/examples/custom_format.rs b/third_party/rust/env_logger-0.6.2/examples/custom_format.rs new file mode 100644 index 000000000000..c4563f50d20b --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/examples/custom_format.rs @@ -0,0 +1,54 @@ +/*! +Changing the default logging format. + +Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`: + +```no_run,shell +$ export MY_LOG_LEVEL='info' +``` + +Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors +or `auto` to enable them: + +```no_run,shell +$ export MY_LOG_STYLE=never +``` + +If you want to control the logging output completely, see the `custom_logger` example. +*/ + +#[macro_use] +extern crate log; +extern crate env_logger; + +use std::io::Write; + +use env_logger::{Env, Builder, fmt}; + +fn init_logger() { + let env = Env::default() + .filter("MY_LOG_LEVEL") + .write_style("MY_LOG_STYLE"); + + let mut builder = Builder::from_env(env); + + // Use a different format for writing log records + // The colors are only available when the `termcolor` dependency is (which it is by default) + #[cfg(feature = "termcolor")] + builder.format(|buf, record| { + let mut style = buf.style(); + style.set_bg(fmt::Color::Yellow).set_bold(true); + + let timestamp = buf.timestamp(); + + writeln!(buf, "My formatted log ({}): {}", timestamp, style.value(record.args())) + }); + + builder.init(); +} + +fn main() { + init_logger(); + + info!("a log from `MyLogger`"); +} diff --git a/third_party/rust/env_logger-0.6.2/examples/custom_logger.rs b/third_party/rust/env_logger-0.6.2/examples/custom_logger.rs new file mode 100644 index 000000000000..792c9c8e5ee3 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/examples/custom_logger.rs @@ -0,0 +1,60 @@ +/*! +Using `env_logger` to drive a custom logger. + +Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`: + +```no_run,shell +$ export MY_LOG_LEVEL='info' +``` + +If you only want to change the way logs are formatted, look at the `custom_format` example. +*/ + +#[macro_use] +extern crate log; +extern crate env_logger; +use env_logger::filter::Filter; +use log::{Log, Metadata, Record, SetLoggerError}; + +struct MyLogger { + inner: Filter +} + +impl MyLogger { + fn new() -> MyLogger { + use env_logger::filter::Builder; + let mut builder = Builder::from_env("MY_LOG_LEVEL"); + + MyLogger { + inner: builder.build() + } + } + + fn init() -> Result<(), SetLoggerError> { + let logger = Self::new(); + + log::set_max_level(logger.inner.filter()); + log::set_boxed_logger(Box::new(logger)) + } +} + +impl Log for MyLogger { + fn enabled(&self, metadata: &Metadata) -> bool { + self.inner.enabled(metadata) + } + + fn log(&self, record: &Record) { + // Check if the record is matched by the logger before logging + if self.inner.matches(record) { + println!("{} - {}", record.level(), record.args()); + } + } + + fn flush(&self) { } +} + +fn main() { + MyLogger::init().unwrap(); + + info!("a log from `MyLogger`"); +} diff --git a/third_party/rust/env_logger-0.6.2/examples/default.rs b/third_party/rust/env_logger-0.6.2/examples/default.rs new file mode 100644 index 000000000000..302e38a20842 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/examples/default.rs @@ -0,0 +1,39 @@ +/*! +Using `env_logger`. + +Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`: + +```no_run,shell +$ export MY_LOG_LEVEL='info' +``` + +Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors +or `auto` to enable them: + +```no_run,shell +$ export MY_LOG_STYLE=never +``` +*/ + +#[macro_use] +extern crate log; +extern crate env_logger; + +use env_logger::Env; + +fn main() { + // The `Env` lets us tweak what the environment + // variables to read are and what the default + // value is if they're missing + let env = Env::default() + .filter_or("MY_LOG_LEVEL", "trace") + .write_style_or("MY_LOG_STYLE", "always"); + + env_logger::init_from_env(env); + + trace!("some trace log"); + debug!("some debug log"); + info!("some information log"); + warn!("some warning log"); + error!("some error log"); +} diff --git a/third_party/rust/env_logger-0.6.2/examples/direct_logger.rs b/third_party/rust/env_logger-0.6.2/examples/direct_logger.rs new file mode 100644 index 000000000000..410230bcdc24 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/examples/direct_logger.rs @@ -0,0 +1,40 @@ +/*! +Using `env_logger::Logger` and the `log::Log` trait directly. + +This example doesn't rely on environment variables, or having a static logger installed. +*/ + +extern crate log; +extern crate env_logger; + +fn record() -> log::Record<'static> { + let error_metadata = log::MetadataBuilder::new() + .target("myApp") + .level(log::Level::Error) + .build(); + + log::Record::builder() + .metadata(error_metadata) + .args(format_args!("Error!")) + .line(Some(433)) + .file(Some("app.rs")) + .module_path(Some("server")) + .build() +} + +fn main() { + use log::Log; + + let stylish_logger = env_logger::Builder::new() + .filter(None, log::LevelFilter::Error) + .write_style(env_logger::WriteStyle::Always) + .build(); + + let unstylish_logger = env_logger::Builder::new() + .filter(None, log::LevelFilter::Error) + .write_style(env_logger::WriteStyle::Never) + .build(); + + stylish_logger.log(&record()); + unstylish_logger.log(&record()); +} \ No newline at end of file diff --git a/third_party/rust/env_logger-0.6.2/examples/filters_from_code.rs b/third_party/rust/env_logger-0.6.2/examples/filters_from_code.rs new file mode 100644 index 000000000000..ef5b96910dbd --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/examples/filters_from_code.rs @@ -0,0 +1,19 @@ +/*! +Specify logging filters in code instead of using an environment variable. +*/ + +#[macro_use] +extern crate log; +extern crate env_logger; + +fn main() { + env_logger::builder() + .filter_level(log::LevelFilter::Trace) + .init(); + + trace!("some trace log"); + debug!("some debug log"); + info!("some information log"); + warn!("some warning log"); + error!("some error log"); +} diff --git a/third_party/rust/env_logger-0.6.2/src/filter/mod.rs b/third_party/rust/env_logger-0.6.2/src/filter/mod.rs new file mode 100644 index 000000000000..a0fe6a2506b7 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/filter/mod.rs @@ -0,0 +1,579 @@ +//! Filtering for log records. +//! +//! This module contains the log filtering used by `env_logger` to match records. +//! You can use the `Filter` type in your own logger implementation to use the same +//! filter parsing and matching as `env_logger`. For more details about the format +//! for directive strings see [Enabling Logging]. +//! +//! ## Using `env_logger` in your own logger +//! +//! You can use `env_logger`'s filtering functionality with your own logger. +//! Call [`Builder::parse`] to parse directives from a string when constructing +//! your logger. Call [`Filter::matches`] to check whether a record should be +//! logged based on the parsed filters when log records are received. +//! +//! ``` +//! extern crate log; +//! extern crate env_logger; +//! use env_logger::filter::Filter; +//! use log::{Log, Metadata, Record}; +//! +//! struct MyLogger { +//! filter: Filter +//! } +//! +//! impl MyLogger { +//! fn new() -> MyLogger { +//! use env_logger::filter::Builder; +//! let mut builder = Builder::new(); +//! +//! // Parse a directives string from an environment variable +//! if let Ok(ref filter) = std::env::var("MY_LOG_LEVEL") { +//! builder.parse(filter); +//! } +//! +//! MyLogger { +//! filter: builder.build() +//! } +//! } +//! } +//! +//! impl Log for MyLogger { +//! fn enabled(&self, metadata: &Metadata) -> bool { +//! self.filter.enabled(metadata) +//! } +//! +//! fn log(&self, record: &Record) { +//! // Check if the record is matched by the filter +//! if self.filter.matches(record) { +//! println!("{:?}", record); +//! } +//! } +//! +//! fn flush(&self) {} +//! } +//! # fn main() {} +//! ``` +//! +//! [Enabling Logging]: ../index.html#enabling-logging +//! [`Builder::parse`]: struct.Builder.html#method.parse +//! [`Filter::matches`]: struct.Filter.html#method.matches + +use std::env; +use std::mem; +use std::fmt; +use log::{Level, LevelFilter, Record, Metadata}; + +#[cfg(feature = "regex")] +#[path = "regex.rs"] +mod inner; + +#[cfg(not(feature = "regex"))] +#[path = "string.rs"] +mod inner; + +/// A log filter. +/// +/// This struct can be used to determine whether or not a log record +/// should be written to the output. +/// Use the [`Builder`] type to parse and construct a `Filter`. +/// +/// [`Builder`]: struct.Builder.html +pub struct Filter { + directives: Vec, + filter: Option, +} + +/// A builder for a log filter. +/// +/// It can be used to parse a set of directives from a string before building +/// a [`Filter`] instance. +/// +/// ## Example +/// +/// ``` +/// #[macro_use] +/// extern crate log; +/// extern crate env_logger; +/// +/// use std::env; +/// use std::io; +/// use env_logger::filter::Builder; +/// +/// fn main() { +/// let mut builder = Builder::new(); +/// +/// // Parse a logging filter from an environment variable. +/// if let Ok(rust_log) = env::var("RUST_LOG") { +/// builder.parse(&rust_log); +/// } +/// +/// let filter = builder.build(); +/// } +/// ``` +/// +/// [`Filter`]: struct.Filter.html +pub struct Builder { + directives: Vec, + filter: Option, + built: bool, +} + +#[derive(Debug)] +struct Directive { + name: Option, + level: LevelFilter, +} + +impl Filter { + /// Returns the maximum `LevelFilter` that this filter instance is + /// configured to output. + /// + /// # Example + /// + /// ```rust + /// extern crate log; + /// extern crate env_logger; + /// + /// use log::LevelFilter; + /// use env_logger::filter::Builder; + /// + /// fn main() { + /// let mut builder = Builder::new(); + /// builder.filter(Some("module1"), LevelFilter::Info); + /// builder.filter(Some("module2"), LevelFilter::Error); + /// + /// let filter = builder.build(); + /// assert_eq!(filter.filter(), LevelFilter::Info); + /// } + /// ``` + pub fn filter(&self) -> LevelFilter { + self.directives.iter() + .map(|d| d.level) + .max() + .unwrap_or(LevelFilter::Off) + } + + /// Checks if this record matches the configured filter. + pub fn matches(&self, record: &Record) -> bool { + if !self.enabled(record.metadata()) { + return false; + } + + if let Some(filter) = self.filter.as_ref() { + if !filter.is_match(&*record.args().to_string()) { + return false; + } + } + + true + } + + /// Determines if a log message with the specified metadata would be logged. + pub fn enabled(&self, metadata: &Metadata) -> bool { + let level = metadata.level(); + let target = metadata.target(); + + enabled(&self.directives, level, target) + } +} + +impl Builder { + /// Initializes the filter builder with defaults. + pub fn new() -> Builder { + Builder { + directives: Vec::new(), + filter: None, + built: false, + } + } + + /// Initializes the filter builder from an environment. + pub fn from_env(env: &str) -> Builder { + let mut builder = Builder::new(); + + if let Ok(s) = env::var(env) { + builder.parse(&s); + } + + builder + } + + /// Adds a directive to the filter for a specific module. + pub fn filter_module(&mut self, module: &str, level: LevelFilter) -> &mut Self { + self.filter(Some(module), level) + } + + /// Adds a directive to the filter for all modules. + pub fn filter_level(&mut self, level: LevelFilter) -> &mut Self { + self.filter(None, level) + } + + /// Adds a directive to the filter. + /// + /// The given module (if any) will log at most the specified level provided. + /// If no module is provided then the filter will apply to all log messages. + pub fn filter(&mut self, + module: Option<&str>, + level: LevelFilter) -> &mut Self { + self.directives.push(Directive { + name: module.map(|s| s.to_string()), + level, + }); + self + } + + /// Parses the directives string. + /// + /// See the [Enabling Logging] section for more details. + /// + /// [Enabling Logging]: ../index.html#enabling-logging + pub fn parse(&mut self, filters: &str) -> &mut Self { + let (directives, filter) = parse_spec(filters); + + self.filter = filter; + + for directive in directives { + self.directives.push(directive); + } + self + } + + /// Build a log filter. + pub fn build(&mut self) -> Filter { + assert!(!self.built, "attempt to re-use consumed builder"); + self.built = true; + + if self.directives.is_empty() { + // Adds the default filter if none exist + self.directives.push(Directive { + name: None, + level: LevelFilter::Error, + }); + } else { + // Sort the directives by length of their name, this allows a + // little more efficient lookup at runtime. + self.directives.sort_by(|a, b| { + let alen = a.name.as_ref().map(|a| a.len()).unwrap_or(0); + let blen = b.name.as_ref().map(|b| b.len()).unwrap_or(0); + alen.cmp(&blen) + }); + } + + Filter { + directives: mem::replace(&mut self.directives, Vec::new()), + filter: mem::replace(&mut self.filter, None), + } + } +} + +impl Default for Builder { + fn default() -> Self { + Builder::new() + } +} + +impl fmt::Debug for Filter { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + f.debug_struct("Filter") + .field("filter", &self.filter) + .field("directives", &self.directives) + .finish() + } +} + +impl fmt::Debug for Builder { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + if self.built { + f.debug_struct("Filter") + .field("built", &true) + .finish() + } else { + f.debug_struct("Filter") + .field("filter", &self.filter) + .field("directives", &self.directives) + .finish() + } + } +} + +/// Parse a logging specification string (e.g: "crate1,crate2::mod3,crate3::x=error/foo") +/// and return a vector with log directives. +fn parse_spec(spec: &str) -> (Vec, Option) { + let mut dirs = Vec::new(); + + let mut parts = spec.split('/'); + let mods = parts.next(); + let filter = parts.next(); + if parts.next().is_some() { + eprintln!("warning: invalid logging spec '{}', \ + ignoring it (too many '/'s)", spec); + return (dirs, None); + } + mods.map(|m| { for s in m.split(',') { + if s.len() == 0 { continue } + let mut parts = s.split('='); + let (log_level, name) = match (parts.next(), parts.next().map(|s| s.trim()), parts.next()) { + (Some(part0), None, None) => { + // if the single argument is a log-level string or number, + // treat that as a global fallback + match part0.parse() { + Ok(num) => (num, None), + Err(_) => (LevelFilter::max(), Some(part0)), + } + } + (Some(part0), Some(""), None) => (LevelFilter::max(), Some(part0)), + (Some(part0), Some(part1), None) => { + match part1.parse() { + Ok(num) => (num, Some(part0)), + _ => { + eprintln!("warning: invalid logging spec '{}', \ + ignoring it", part1); + continue + } + } + }, + _ => { + eprintln!("warning: invalid logging spec '{}', \ + ignoring it", s); + continue + } + }; + dirs.push(Directive { + name: name.map(|s| s.to_string()), + level: log_level, + }); + }}); + + let filter = filter.map_or(None, |filter| { + match inner::Filter::new(filter) { + Ok(re) => Some(re), + Err(e) => { + eprintln!("warning: invalid regex filter - {}", e); + None + } + } + }); + + return (dirs, filter); +} + + +// Check whether a level and target are enabled by the set of directives. +fn enabled(directives: &[Directive], level: Level, target: &str) -> bool { + // Search for the longest match, the vector is assumed to be pre-sorted. + for directive in directives.iter().rev() { + match directive.name { + Some(ref name) if !target.starts_with(&**name) => {}, + Some(..) | None => { + return level <= directive.level + } + } + } + false +} + +#[cfg(test)] +mod tests { + use log::{Level, LevelFilter}; + + use super::{Builder, Filter, Directive, parse_spec, enabled}; + + fn make_logger_filter(dirs: Vec) -> Filter { + let mut logger = Builder::new().build(); + logger.directives = dirs; + logger + } + + #[test] + fn filter_info() { + let logger = Builder::new().filter(None, LevelFilter::Info).build(); + assert!(enabled(&logger.directives, Level::Info, "crate1")); + assert!(!enabled(&logger.directives, Level::Debug, "crate1")); + } + + #[test] + fn filter_beginning_longest_match() { + let logger = Builder::new() + .filter(Some("crate2"), LevelFilter::Info) + .filter(Some("crate2::mod"), LevelFilter::Debug) + .filter(Some("crate1::mod1"), LevelFilter::Warn) + .build(); + assert!(enabled(&logger.directives, Level::Debug, "crate2::mod1")); + assert!(!enabled(&logger.directives, Level::Debug, "crate2")); + } + + #[test] + fn parse_default() { + let logger = Builder::new().parse("info,crate1::mod1=warn").build(); + assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1")); + assert!(enabled(&logger.directives, Level::Info, "crate2::mod2")); + } + + #[test] + fn match_full_path() { + let logger = make_logger_filter(vec![ + Directive { + name: Some("crate2".to_string()), + level: LevelFilter::Info + }, + Directive { + name: Some("crate1::mod1".to_string()), + level: LevelFilter::Warn + } + ]); + assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1")); + assert!(!enabled(&logger.directives, Level::Info, "crate1::mod1")); + assert!(enabled(&logger.directives, Level::Info, "crate2")); + assert!(!enabled(&logger.directives, Level::Debug, "crate2")); + } + + #[test] + fn no_match() { + let logger = make_logger_filter(vec![ + Directive { name: Some("crate2".to_string()), level: LevelFilter::Info }, + Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn } + ]); + assert!(!enabled(&logger.directives, Level::Warn, "crate3")); + } + + #[test] + fn match_beginning() { + let logger = make_logger_filter(vec![ + Directive { name: Some("crate2".to_string()), level: LevelFilter::Info }, + Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn } + ]); + assert!(enabled(&logger.directives, Level::Info, "crate2::mod1")); + } + + #[test] + fn match_beginning_longest_match() { + let logger = make_logger_filter(vec![ + Directive { name: Some("crate2".to_string()), level: LevelFilter::Info }, + Directive { name: Some("crate2::mod".to_string()), level: LevelFilter::Debug }, + Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn } + ]); + assert!(enabled(&logger.directives, Level::Debug, "crate2::mod1")); + assert!(!enabled(&logger.directives, Level::Debug, "crate2")); + } + + #[test] + fn match_default() { + let logger = make_logger_filter(vec![ + Directive { name: None, level: LevelFilter::Info }, + Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn } + ]); + assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1")); + assert!(enabled(&logger.directives, Level::Info, "crate2::mod2")); + } + + #[test] + fn zero_level() { + let logger = make_logger_filter(vec![ + Directive { name: None, level: LevelFilter::Info }, + Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Off } + ]); + assert!(!enabled(&logger.directives, Level::Error, "crate1::mod1")); + assert!(enabled(&logger.directives, Level::Info, "crate2::mod2")); + } + + #[test] + fn parse_spec_valid() { + let (dirs, filter) = parse_spec("crate1::mod1=error,crate1::mod2,crate2=debug"); + assert_eq!(dirs.len(), 3); + assert_eq!(dirs[0].name, Some("crate1::mod1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::Error); + + assert_eq!(dirs[1].name, Some("crate1::mod2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::max()); + + assert_eq!(dirs[2].name, Some("crate2".to_string())); + assert_eq!(dirs[2].level, LevelFilter::Debug); + assert!(filter.is_none()); + } + + #[test] + fn parse_spec_invalid_crate() { + // test parse_spec with multiple = in specification + let (dirs, filter) = parse_spec("crate1::mod1=warn=info,crate2=debug"); + assert_eq!(dirs.len(), 1); + assert_eq!(dirs[0].name, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::Debug); + assert!(filter.is_none()); + } + + #[test] + fn parse_spec_invalid_level() { + // test parse_spec with 'noNumber' as log level + let (dirs, filter) = parse_spec("crate1::mod1=noNumber,crate2=debug"); + assert_eq!(dirs.len(), 1); + assert_eq!(dirs[0].name, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::Debug); + assert!(filter.is_none()); + } + + #[test] + fn parse_spec_string_level() { + // test parse_spec with 'warn' as log level + let (dirs, filter) = parse_spec("crate1::mod1=wrong,crate2=warn"); + assert_eq!(dirs.len(), 1); + assert_eq!(dirs[0].name, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::Warn); + assert!(filter.is_none()); + } + + #[test] + fn parse_spec_empty_level() { + // test parse_spec with '' as log level + let (dirs, filter) = parse_spec("crate1::mod1=wrong,crate2="); + assert_eq!(dirs.len(), 1); + assert_eq!(dirs[0].name, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::max()); + assert!(filter.is_none()); + } + + #[test] + fn parse_spec_global() { + // test parse_spec with no crate + let (dirs, filter) = parse_spec("warn,crate2=debug"); + assert_eq!(dirs.len(), 2); + assert_eq!(dirs[0].name, None); + assert_eq!(dirs[0].level, LevelFilter::Warn); + assert_eq!(dirs[1].name, Some("crate2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::Debug); + assert!(filter.is_none()); + } + + #[test] + fn parse_spec_valid_filter() { + let (dirs, filter) = parse_spec("crate1::mod1=error,crate1::mod2,crate2=debug/abc"); + assert_eq!(dirs.len(), 3); + assert_eq!(dirs[0].name, Some("crate1::mod1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::Error); + + assert_eq!(dirs[1].name, Some("crate1::mod2".to_string())); + assert_eq!(dirs[1].level, LevelFilter::max()); + + assert_eq!(dirs[2].name, Some("crate2".to_string())); + assert_eq!(dirs[2].level, LevelFilter::Debug); + assert!(filter.is_some() && filter.unwrap().to_string() == "abc"); + } + + #[test] + fn parse_spec_invalid_crate_filter() { + let (dirs, filter) = parse_spec("crate1::mod1=error=warn,crate2=debug/a.c"); + assert_eq!(dirs.len(), 1); + assert_eq!(dirs[0].name, Some("crate2".to_string())); + assert_eq!(dirs[0].level, LevelFilter::Debug); + assert!(filter.is_some() && filter.unwrap().to_string() == "a.c"); + } + + #[test] + fn parse_spec_empty_with_filter() { + let (dirs, filter) = parse_spec("crate1/a*c"); + assert_eq!(dirs.len(), 1); + assert_eq!(dirs[0].name, Some("crate1".to_string())); + assert_eq!(dirs[0].level, LevelFilter::max()); + assert!(filter.is_some() && filter.unwrap().to_string() == "a*c"); + } +} diff --git a/third_party/rust/env_logger-0.6.2/src/filter/regex.rs b/third_party/rust/env_logger-0.6.2/src/filter/regex.rs new file mode 100644 index 000000000000..a042654135e3 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/filter/regex.rs @@ -0,0 +1,29 @@ +extern crate regex; + +use std::fmt; + +use self::regex::Regex; + +#[derive(Debug)] +pub struct Filter { + inner: Regex, +} + +impl Filter { + pub fn new(spec: &str) -> Result { + match Regex::new(spec){ + Ok(r) => Ok(Filter { inner: r }), + Err(e) => Err(e.to_string()), + } + } + + pub fn is_match(&self, s: &str) -> bool { + self.inner.is_match(s) + } +} + +impl fmt::Display for Filter { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } +} diff --git a/third_party/rust/env_logger-0.6.2/src/filter/string.rs b/third_party/rust/env_logger-0.6.2/src/filter/string.rs new file mode 100644 index 000000000000..96d7ecca1243 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/filter/string.rs @@ -0,0 +1,22 @@ +use std::fmt; + +#[derive(Debug)] +pub struct Filter { + inner: String, +} + +impl Filter { + pub fn new(spec: &str) -> Result { + Ok(Filter { inner: spec.to_string() }) + } + + pub fn is_match(&self, s: &str) -> bool { + s.contains(&self.inner) + } +} + +impl fmt::Display for Filter { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } +} diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/humantime/extern_impl.rs b/third_party/rust/env_logger-0.6.2/src/fmt/humantime/extern_impl.rs new file mode 100644 index 000000000000..596a281959a5 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/humantime/extern_impl.rs @@ -0,0 +1,84 @@ +use std::fmt; +use std::time::SystemTime; + +use humantime::{format_rfc3339_nanos, format_rfc3339_seconds}; + +use ::fmt::Formatter; + +pub(in ::fmt) mod glob { + pub use super::*; +} + +impl Formatter { + /// Get a [`Timestamp`] for the current date and time in UTC. + /// + /// # Examples + /// + /// Include the current timestamp with the log record: + /// + /// ``` + /// use std::io::Write; + /// + /// let mut builder = env_logger::Builder::new(); + /// + /// builder.format(|buf, record| { + /// let ts = buf.timestamp(); + /// + /// writeln!(buf, "{}: {}: {}", ts, record.level(), record.args()) + /// }); + /// ``` + /// + /// [`Timestamp`]: struct.Timestamp.html + pub fn timestamp(&self) -> Timestamp { + Timestamp(SystemTime::now()) + } + + /// Get a [`PreciseTimestamp`] for the current date and time in UTC with nanos. + pub fn precise_timestamp(&self) -> PreciseTimestamp { + PreciseTimestamp(SystemTime::now()) + } +} + +/// An [RFC3339] formatted timestamp. +/// +/// The timestamp implements [`Display`] and can be written to a [`Formatter`]. +/// +/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt +/// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html +/// [`Formatter`]: struct.Formatter.html +pub struct Timestamp(SystemTime); + +/// An [RFC3339] formatted timestamp with nanos. +/// +/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt +#[derive(Debug)] +pub struct PreciseTimestamp(SystemTime); + +impl fmt::Debug for Timestamp { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + /// A `Debug` wrapper for `Timestamp` that uses the `Display` implementation. + struct TimestampValue<'a>(&'a Timestamp); + + impl<'a> fmt::Debug for TimestampValue<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } + } + + f.debug_tuple("Timestamp") + .field(&TimestampValue(&self)) + .finish() + } +} + +impl fmt::Display for Timestamp { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + format_rfc3339_seconds(self.0).fmt(f) + } +} + +impl fmt::Display for PreciseTimestamp { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + format_rfc3339_nanos(self.0).fmt(f) + } +} \ No newline at end of file diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/humantime/mod.rs b/third_party/rust/env_logger-0.6.2/src/fmt/humantime/mod.rs new file mode 100644 index 000000000000..c4f7599c9add --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/humantime/mod.rs @@ -0,0 +1,11 @@ +/* +This internal module contains the timestamp implementation. + +Its public API is available when the `humantime` crate is available. +*/ + +#[cfg_attr(feature = "humantime", path = "extern_impl.rs")] +#[cfg_attr(not(feature = "humantime"), path = "shim_impl.rs")] +mod imp; + +pub(in ::fmt) use self::imp::*; diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/humantime/shim_impl.rs b/third_party/rust/env_logger-0.6.2/src/fmt/humantime/shim_impl.rs new file mode 100644 index 000000000000..0f7534000224 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/humantime/shim_impl.rs @@ -0,0 +1,7 @@ +/* +Timestamps aren't available when we don't have a `humantime` dependency. +*/ + +pub(in ::fmt) mod glob { + +} diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/mod.rs b/third_party/rust/env_logger-0.6.2/src/fmt/mod.rs new file mode 100644 index 000000000000..3f3d4975943f --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/mod.rs @@ -0,0 +1,358 @@ +//! Formatting for log records. +//! +//! This module contains a [`Formatter`] that can be used to format log records +//! into without needing temporary allocations. Usually you won't need to worry +//! about the contents of this module and can use the `Formatter` like an ordinary +//! [`Write`]. +//! +//! # Formatting log records +//! +//! The format used to print log records can be customised using the [`Builder::format`] +//! method. +//! Custom formats can apply different color and weight to printed values using +//! [`Style`] builders. +//! +//! ``` +//! use std::io::Write; +//! +//! let mut builder = env_logger::Builder::new(); +//! +//! builder.format(|buf, record| { +//! writeln!(buf, "{}: {}", +//! record.level(), +//! record.args()) +//! }); +//! ``` +//! +//! [`Formatter`]: struct.Formatter.html +//! [`Style`]: struct.Style.html +//! [`Builder::format`]: ../struct.Builder.html#method.format +//! [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html + +use std::io::prelude::*; +use std::{io, fmt, mem}; +use std::rc::Rc; +use std::cell::RefCell; +use std::fmt::Display; + +use log::Record; + +pub(crate) mod writer; +mod humantime; + +pub use self::humantime::glob::*; +pub use self::writer::glob::*; + +use self::writer::{Writer, Buffer}; + +pub(crate) mod glob { + pub use super::{Target, WriteStyle}; +} + +/// A formatter to write logs into. +/// +/// `Formatter` implements the standard [`Write`] trait for writing log records. +/// It also supports terminal colors, through the [`style`] method. +/// +/// # Examples +/// +/// Use the [`writeln`] macro to format a log record. +/// An instance of a `Formatter` is passed to an `env_logger` format as `buf`: +/// +/// ``` +/// use std::io::Write; +/// +/// let mut builder = env_logger::Builder::new(); +/// +/// builder.format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args())); +/// ``` +/// +/// [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html +/// [`writeln`]: https://doc.rust-lang.org/stable/std/macro.writeln.html +/// [`style`]: #method.style +pub struct Formatter { + buf: Rc>, + write_style: WriteStyle, +} + +impl Formatter { + pub(crate) fn new(writer: &Writer) -> Self { + Formatter { + buf: Rc::new(RefCell::new(writer.buffer())), + write_style: writer.write_style(), + } + } + + pub(crate) fn write_style(&self) -> WriteStyle { + self.write_style + } + + pub(crate) fn print(&self, writer: &Writer) -> io::Result<()> { + writer.print(&self.buf.borrow()) + } + + pub(crate) fn clear(&mut self) { + self.buf.borrow_mut().clear() + } +} + +impl Write for Formatter { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.buf.borrow_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.buf.borrow_mut().flush() + } +} + +impl fmt::Debug for Formatter { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + f.debug_struct("Formatter").finish() + } +} + +pub(crate) struct Builder { + pub default_format_timestamp: bool, + pub default_format_timestamp_nanos: bool, + pub default_format_module_path: bool, + pub default_format_level: bool, + #[allow(unknown_lints, bare_trait_objects)] + pub custom_format: Option io::Result<()> + Sync + Send>>, + built: bool, +} + +impl Default for Builder { + fn default() -> Self { + Builder { + default_format_timestamp: true, + default_format_timestamp_nanos: false, + default_format_module_path: true, + default_format_level: true, + custom_format: None, + built: false, + } + } +} + +impl Builder { + /// Convert the format into a callable function. + /// + /// If the `custom_format` is `Some`, then any `default_format` switches are ignored. + /// If the `custom_format` is `None`, then a default format is returned. + /// Any `default_format` switches set to `false` won't be written by the format. + #[allow(unknown_lints, bare_trait_objects)] + pub fn build(&mut self) -> Box io::Result<()> + Sync + Send> { + assert!(!self.built, "attempt to re-use consumed builder"); + + let built = mem::replace(self, Builder { + built: true, + ..Default::default() + }); + + if let Some(fmt) = built.custom_format { + fmt + } + else { + Box::new(move |buf, record| { + let fmt = DefaultFormat { + timestamp: built.default_format_timestamp, + timestamp_nanos: built.default_format_timestamp_nanos, + module_path: built.default_format_module_path, + level: built.default_format_level, + written_header_value: false, + buf, + }; + + fmt.write(record) + }) + } + } +} + +#[cfg(feature = "termcolor")] +type SubtleStyle = StyledValue<'static, &'static str>; +#[cfg(not(feature = "termcolor"))] +type SubtleStyle = &'static str; + +/// The default format. +/// +/// This format needs to work with any combination of crate features. +struct DefaultFormat<'a> { + timestamp: bool, + module_path: bool, + level: bool, + timestamp_nanos: bool, + written_header_value: bool, + buf: &'a mut Formatter, +} + +impl<'a> DefaultFormat<'a> { + fn write(mut self, record: &Record) -> io::Result<()> { + self.write_timestamp()?; + self.write_level(record)?; + self.write_module_path(record)?; + self.finish_header()?; + + self.write_args(record) + } + + fn subtle_style(&self, text: &'static str) -> SubtleStyle { + #[cfg(feature = "termcolor")] + { + self.buf.style() + .set_color(Color::Black) + .set_intense(true) + .into_value(text) + } + #[cfg(not(feature = "termcolor"))] + { + text + } + } + + fn write_header_value(&mut self, value: T) -> io::Result<()> + where + T: Display, + { + if !self.written_header_value { + self.written_header_value = true; + + let open_brace = self.subtle_style("["); + write!(self.buf, "{}{}", open_brace, value) + } else { + write!(self.buf, " {}", value) + } + } + + fn write_level(&mut self, record: &Record) -> io::Result<()> { + if !self.level { + return Ok(()) + } + + let level = { + #[cfg(feature = "termcolor")] + { + self.buf.default_styled_level(record.level()) + } + #[cfg(not(feature = "termcolor"))] + { + record.level() + } + }; + + self.write_header_value(format_args!("{:<5}", level)) + } + + fn write_timestamp(&mut self) -> io::Result<()> { + #[cfg(feature = "humantime")] + { + if !self.timestamp { + return Ok(()) + } + + if self.timestamp_nanos { + let ts_nanos = self.buf.precise_timestamp(); + self.write_header_value(ts_nanos) + } else { + let ts = self.buf.timestamp(); + self.write_header_value(ts) + } + } + #[cfg(not(feature = "humantime"))] + { + let _ = self.timestamp; + let _ = self.timestamp_nanos; + Ok(()) + } + } + + fn write_module_path(&mut self, record: &Record) -> io::Result<()> { + if !self.module_path { + return Ok(()) + } + + if let Some(module_path) = record.module_path() { + self.write_header_value(module_path) + } else { + Ok(()) + } + } + + fn finish_header(&mut self) -> io::Result<()> { + if self.written_header_value { + let close_brace = self.subtle_style("]"); + write!(self.buf, "{} ", close_brace) + } else { + Ok(()) + } + } + + fn write_args(&mut self, record: &Record) -> io::Result<()> { + writeln!(self.buf, "{}", record.args()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use log::{Level, Record}; + + fn write(fmt: DefaultFormat) -> String { + let buf = fmt.buf.buf.clone(); + + let record = Record::builder() + .args(format_args!("log message")) + .level(Level::Info) + .file(Some("test.rs")) + .line(Some(144)) + .module_path(Some("test::path")) + .build(); + + fmt.write(&record).expect("failed to write record"); + + let buf = buf.borrow(); + String::from_utf8(buf.bytes().to_vec()).expect("failed to read record") + } + + #[test] + fn default_format_with_header() { + let writer = writer::Builder::new() + .write_style(WriteStyle::Never) + .build(); + + let mut f = Formatter::new(&writer); + + let written = write(DefaultFormat { + timestamp: false, + timestamp_nanos: false, + module_path: true, + level: true, + written_header_value: false, + buf: &mut f, + }); + + assert_eq!("[INFO test::path] log message\n", written); + } + + #[test] + fn default_format_no_header() { + let writer = writer::Builder::new() + .write_style(WriteStyle::Never) + .build(); + + let mut f = Formatter::new(&writer); + + let written = write(DefaultFormat { + timestamp: false, + timestamp_nanos: false, + module_path: false, + level: false, + written_header_value: false, + buf: &mut f, + }); + + assert_eq!("log message\n", written); + } +} diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/writer/atty.rs b/third_party/rust/env_logger-0.6.2/src/fmt/writer/atty.rs new file mode 100644 index 000000000000..c441cf0895ad --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/writer/atty.rs @@ -0,0 +1,34 @@ +/* +This internal module contains the terminal detection implementation. + +If the `atty` crate is available then we use it to detect whether we're +attached to a particular TTY. If the `atty` crate is not available we +assume we're not attached to anything. This effectively prevents styles +from being printed. +*/ + +#[cfg(feature = "atty")] +mod imp { + use atty; + + pub(in ::fmt) fn is_stdout() -> bool { + atty::is(atty::Stream::Stdout) + } + + pub(in ::fmt) fn is_stderr() -> bool { + atty::is(atty::Stream::Stderr) + } +} + +#[cfg(not(feature = "atty"))] +mod imp { + pub(in ::fmt) fn is_stdout() -> bool { + false + } + + pub(in ::fmt) fn is_stderr() -> bool { + false + } +} + +pub(in ::fmt) use self::imp::*; diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/writer/mod.rs b/third_party/rust/env_logger-0.6.2/src/fmt/writer/mod.rs new file mode 100644 index 000000000000..d84e4146776a --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/writer/mod.rs @@ -0,0 +1,206 @@ +mod termcolor; +mod atty; + +use std::{fmt, io}; +use self::termcolor::BufferWriter; +use self::atty::{is_stdout, is_stderr}; + +pub(in ::fmt) mod glob { + pub use super::termcolor::glob::*; + pub use super::*; +} + +pub(in ::fmt) use self::termcolor::Buffer; + +/// Log target, either `stdout` or `stderr`. +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +pub enum Target { + /// Logs will be sent to standard output. + Stdout, + /// Logs will be sent to standard error. + Stderr, +} + +impl Default for Target { + fn default() -> Self { + Target::Stderr + } +} + +/// Whether or not to print styles to the target. +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +pub enum WriteStyle { + /// Try to print styles, but don't force the issue. + Auto, + /// Try very hard to print styles. + Always, + /// Never print styles. + Never, +} + +impl Default for WriteStyle { + fn default() -> Self { + WriteStyle::Auto + } +} + +/// A terminal target with color awareness. +pub(crate) struct Writer { + inner: BufferWriter, + write_style: WriteStyle, +} + +impl Writer { + pub fn write_style(&self) -> WriteStyle { + self.write_style + } + + pub(in ::fmt) fn buffer(&self) -> Buffer { + self.inner.buffer() + } + + pub(in ::fmt) fn print(&self, buf: &Buffer) -> io::Result<()> { + self.inner.print(buf) + } +} + +/// A builder for a terminal writer. +/// +/// The target and style choice can be configured before building. +pub(crate) struct Builder { + target: Target, + write_style: WriteStyle, + is_test: bool, + built: bool, +} + +impl Builder { + /// Initialize the writer builder with defaults. + pub(crate) fn new() -> Self { + Builder { + target: Default::default(), + write_style: Default::default(), + is_test: false, + built: false, + } + } + + /// Set the target to write to. + pub(crate) fn target(&mut self, target: Target) -> &mut Self { + self.target = target; + self + } + + /// Parses a style choice string. + /// + /// See the [Disabling colors] section for more details. + /// + /// [Disabling colors]: ../index.html#disabling-colors + pub(crate) fn parse_write_style(&mut self, write_style: &str) -> &mut Self { + self.write_style(parse_write_style(write_style)) + } + + /// Whether or not to print style characters when writing. + pub(crate) fn write_style(&mut self, write_style: WriteStyle) -> &mut Self { + self.write_style = write_style; + self + } + + /// Whether or not to capture logs for `cargo test`. + pub(crate) fn is_test(&mut self, is_test: bool) -> &mut Self { + self.is_test = is_test; + self + } + + /// Build a terminal writer. + pub(crate) fn build(&mut self) -> Writer { + assert!(!self.built, "attempt to re-use consumed builder"); + self.built = true; + + let color_choice = match self.write_style { + WriteStyle::Auto => { + if match self.target { + Target::Stderr => is_stderr(), + Target::Stdout => is_stdout(), + } { + WriteStyle::Auto + } else { + WriteStyle::Never + } + }, + color_choice => color_choice, + }; + + let writer = match self.target { + Target::Stderr => BufferWriter::stderr(self.is_test, color_choice), + Target::Stdout => BufferWriter::stdout(self.is_test, color_choice), + }; + + Writer { + inner: writer, + write_style: self.write_style, + } + } +} + +impl Default for Builder { + fn default() -> Self { + Builder::new() + } +} + +impl fmt::Debug for Builder { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + f.debug_struct("Logger") + .field("target", &self.target) + .field("write_style", &self.write_style) + .finish() + } +} + +impl fmt::Debug for Writer { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + f.debug_struct("Writer").finish() + } +} + +fn parse_write_style(spec: &str) -> WriteStyle { + match spec { + "auto" => WriteStyle::Auto, + "always" => WriteStyle::Always, + "never" => WriteStyle::Never, + _ => Default::default(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_write_style_valid() { + let inputs = vec![ + ("auto", WriteStyle::Auto), + ("always", WriteStyle::Always), + ("never", WriteStyle::Never), + ]; + + for (input, expected) in inputs { + assert_eq!(expected, parse_write_style(input)); + } + } + + #[test] + fn parse_write_style_invalid() { + let inputs = vec![ + "", + "true", + "false", + "NEVER!!" + ]; + + for input in inputs { + assert_eq!(WriteStyle::Auto, parse_write_style(input)); + } + } +} diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/extern_impl.rs b/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/extern_impl.rs new file mode 100644 index 000000000000..0c2d138792c1 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/extern_impl.rs @@ -0,0 +1,490 @@ +use std::borrow::Cow; +use std::fmt; +use std::io::{self, Write}; +use std::cell::RefCell; +use std::rc::Rc; + +use log::Level; +use termcolor::{self, ColorChoice, ColorSpec, WriteColor}; + +use ::WriteStyle; +use ::fmt::{Formatter, Target}; + +pub(in ::fmt::writer) mod glob { + pub use super::*; +} + +impl Formatter { + /// Begin a new [`Style`]. + /// + /// # Examples + /// + /// Create a bold, red colored style and use it to print the log level: + /// + /// ``` + /// use std::io::Write; + /// use env_logger::fmt::Color; + /// + /// let mut builder = env_logger::Builder::new(); + /// + /// builder.format(|buf, record| { + /// let mut level_style = buf.style(); + /// + /// level_style.set_color(Color::Red).set_bold(true); + /// + /// writeln!(buf, "{}: {}", + /// level_style.value(record.level()), + /// record.args()) + /// }); + /// ``` + /// + /// [`Style`]: struct.Style.html + pub fn style(&self) -> Style { + Style { + buf: self.buf.clone(), + spec: ColorSpec::new(), + } + } + + /// Get the default [`Style`] for the given level. + /// + /// The style can be used to print other values besides the level. + pub fn default_level_style(&self, level: Level) -> Style { + let mut level_style = self.style(); + match level { + Level::Trace => level_style.set_color(Color::Black).set_intense(true), + Level::Debug => level_style.set_color(Color::White), + Level::Info => level_style.set_color(Color::Green), + Level::Warn => level_style.set_color(Color::Yellow), + Level::Error => level_style.set_color(Color::Red).set_bold(true), + }; + level_style + } + + /// Get a printable [`Style`] for the given level. + /// + /// The style can only be used to print the level. + pub fn default_styled_level(&self, level: Level) -> StyledValue<'static, Level> { + self.default_level_style(level).into_value(level) + } +} + +pub(in ::fmt::writer) struct BufferWriter { + inner: termcolor::BufferWriter, + test_target: Option, +} + +pub(in ::fmt) struct Buffer { + inner: termcolor::Buffer, + test_target: Option, +} + +impl BufferWriter { + pub(in ::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self { + BufferWriter { + inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()), + test_target: if is_test { + Some(Target::Stderr) + } else { + None + }, + } + } + + pub(in ::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self { + BufferWriter { + inner: termcolor::BufferWriter::stdout(write_style.into_color_choice()), + test_target: if is_test { + Some(Target::Stdout) + } else { + None + }, + } + } + + pub(in ::fmt::writer) fn buffer(&self) -> Buffer { + Buffer { + inner: self.inner.buffer(), + test_target: self.test_target, + } + } + + pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { + if let Some(target) = self.test_target { + // This impl uses the `eprint` and `print` macros + // instead of `termcolor`'s buffer. + // This is so their output can be captured by `cargo test` + let log = String::from_utf8_lossy(buf.bytes()); + + match target { + Target::Stderr => eprint!("{}", log), + Target::Stdout => print!("{}", log), + } + + Ok(()) + } else { + self.inner.print(&buf.inner) + } + } +} + +impl Buffer { + pub(in ::fmt) fn clear(&mut self) { + self.inner.clear() + } + + pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result { + self.inner.write(buf) + } + + pub(in ::fmt) fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } + + pub(in ::fmt) fn bytes(&self) -> &[u8] { + self.inner.as_slice() + } + + fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> { + // Ignore styles for test captured logs because they can't be printed + if self.test_target.is_none() { + self.inner.set_color(spec) + } else { + Ok(()) + } + } + + fn reset(&mut self) -> io::Result<()> { + // Ignore styles for test captured logs because they can't be printed + if self.test_target.is_none() { + self.inner.reset() + } else { + Ok(()) + } + } +} + +impl WriteStyle { + fn into_color_choice(self) -> ColorChoice { + match self { + WriteStyle::Always => ColorChoice::Always, + WriteStyle::Auto => ColorChoice::Auto, + WriteStyle::Never => ColorChoice::Never, + } + } +} + +/// A set of styles to apply to the terminal output. +/// +/// Call [`Formatter::style`] to get a `Style` and use the builder methods to +/// set styling properties, like [color] and [weight]. +/// To print a value using the style, wrap it in a call to [`value`] when the log +/// record is formatted. +/// +/// # Examples +/// +/// Create a bold, red colored style and use it to print the log level: +/// +/// ``` +/// use std::io::Write; +/// use env_logger::fmt::Color; +/// +/// let mut builder = env_logger::Builder::new(); +/// +/// builder.format(|buf, record| { +/// let mut level_style = buf.style(); +/// +/// level_style.set_color(Color::Red).set_bold(true); +/// +/// writeln!(buf, "{}: {}", +/// level_style.value(record.level()), +/// record.args()) +/// }); +/// ``` +/// +/// Styles can be re-used to output multiple values: +/// +/// ``` +/// use std::io::Write; +/// use env_logger::fmt::Color; +/// +/// let mut builder = env_logger::Builder::new(); +/// +/// builder.format(|buf, record| { +/// let mut bold = buf.style(); +/// +/// bold.set_bold(true); +/// +/// writeln!(buf, "{}: {} {}", +/// bold.value(record.level()), +/// bold.value("some bold text"), +/// record.args()) +/// }); +/// ``` +/// +/// [`Formatter::style`]: struct.Formatter.html#method.style +/// [color]: #method.set_color +/// [weight]: #method.set_bold +/// [`value`]: #method.value +#[derive(Clone)] +pub struct Style { + buf: Rc>, + spec: ColorSpec, +} + +/// A value that can be printed using the given styles. +/// +/// It is the result of calling [`Style::value`]. +/// +/// [`Style::value`]: struct.Style.html#method.value +pub struct StyledValue<'a, T> { + style: Cow<'a, Style>, + value: T, +} + +impl Style { + /// Set the text color. + /// + /// # Examples + /// + /// Create a style with red text: + /// + /// ``` + /// use std::io::Write; + /// use env_logger::fmt::Color; + /// + /// let mut builder = env_logger::Builder::new(); + /// + /// builder.format(|buf, record| { + /// let mut style = buf.style(); + /// + /// style.set_color(Color::Red); + /// + /// writeln!(buf, "{}", style.value(record.args())) + /// }); + /// ``` + pub fn set_color(&mut self, color: Color) -> &mut Style { + self.spec.set_fg(color.into_termcolor()); + self + } + + /// Set the text weight. + /// + /// If `yes` is true then text will be written in bold. + /// If `yes` is false then text will be written in the default weight. + /// + /// # Examples + /// + /// Create a style with bold text: + /// + /// ``` + /// use std::io::Write; + /// + /// let mut builder = env_logger::Builder::new(); + /// + /// builder.format(|buf, record| { + /// let mut style = buf.style(); + /// + /// style.set_bold(true); + /// + /// writeln!(buf, "{}", style.value(record.args())) + /// }); + /// ``` + pub fn set_bold(&mut self, yes: bool) -> &mut Style { + self.spec.set_bold(yes); + self + } + + /// Set the text intensity. + /// + /// If `yes` is true then text will be written in a brighter color. + /// If `yes` is false then text will be written in the default color. + /// + /// # Examples + /// + /// Create a style with intense text: + /// + /// ``` + /// use std::io::Write; + /// + /// let mut builder = env_logger::Builder::new(); + /// + /// builder.format(|buf, record| { + /// let mut style = buf.style(); + /// + /// style.set_intense(true); + /// + /// writeln!(buf, "{}", style.value(record.args())) + /// }); + /// ``` + pub fn set_intense(&mut self, yes: bool) -> &mut Style { + self.spec.set_intense(yes); + self + } + + /// Set the background color. + /// + /// # Examples + /// + /// Create a style with a yellow background: + /// + /// ``` + /// use std::io::Write; + /// use env_logger::fmt::Color; + /// + /// let mut builder = env_logger::Builder::new(); + /// + /// builder.format(|buf, record| { + /// let mut style = buf.style(); + /// + /// style.set_bg(Color::Yellow); + /// + /// writeln!(buf, "{}", style.value(record.args())) + /// }); + /// ``` + pub fn set_bg(&mut self, color: Color) -> &mut Style { + self.spec.set_bg(color.into_termcolor()); + self + } + + /// Wrap a value in the style. + /// + /// The same `Style` can be used to print multiple different values. + /// + /// # Examples + /// + /// Create a bold, red colored style and use it to print the log level: + /// + /// ``` + /// use std::io::Write; + /// use env_logger::fmt::Color; + /// + /// let mut builder = env_logger::Builder::new(); + /// + /// builder.format(|buf, record| { + /// let mut style = buf.style(); + /// + /// style.set_color(Color::Red).set_bold(true); + /// + /// writeln!(buf, "{}: {}", + /// style.value(record.level()), + /// record.args()) + /// }); + /// ``` + pub fn value(&self, value: T) -> StyledValue { + StyledValue { + style: Cow::Borrowed(self), + value + } + } + + /// Wrap a value in the style by taking ownership of it. + pub(crate) fn into_value(&mut self, value: T) -> StyledValue<'static, T> { + StyledValue { + style: Cow::Owned(self.clone()), + value + } + } +} + +impl<'a, T> StyledValue<'a, T> { + fn write_fmt(&self, f: F) -> fmt::Result + where + F: FnOnce() -> fmt::Result, + { + self.style.buf.borrow_mut().set_color(&self.style.spec).map_err(|_| fmt::Error)?; + + // Always try to reset the terminal style, even if writing failed + let write = f(); + let reset = self.style.buf.borrow_mut().reset().map_err(|_| fmt::Error); + + write.and(reset) + } +} + +impl fmt::Debug for Style { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + f.debug_struct("Style").field("spec", &self.spec).finish() + } +} + +macro_rules! impl_styled_value_fmt { + ($($fmt_trait:path),*) => { + $( + impl<'a, T: $fmt_trait> $fmt_trait for StyledValue<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + self.write_fmt(|| T::fmt(&self.value, f)) + } + } + )* + }; +} + +impl_styled_value_fmt!( + fmt::Debug, + fmt::Display, + fmt::Pointer, + fmt::Octal, + fmt::Binary, + fmt::UpperHex, + fmt::LowerHex, + fmt::UpperExp, + fmt::LowerExp); + +// The `Color` type is copied from https://github.com/BurntSushi/ripgrep/tree/master/termcolor + +/// The set of available colors for the terminal foreground/background. +/// +/// The `Ansi256` and `Rgb` colors will only output the correct codes when +/// paired with the `Ansi` `WriteColor` implementation. +/// +/// The `Ansi256` and `Rgb` color types are not supported when writing colors +/// on Windows using the console. If they are used on Windows, then they are +/// silently ignored and no colors will be emitted. +/// +/// This set may expand over time. +/// +/// This type has a `FromStr` impl that can parse colors from their human +/// readable form. The format is as follows: +/// +/// 1. Any of the explicitly listed colors in English. They are matched +/// case insensitively. +/// 2. A single 8-bit integer, in either decimal or hexadecimal format. +/// 3. A triple of 8-bit integers separated by a comma, where each integer is +/// in decimal or hexadecimal format. +/// +/// Hexadecimal numbers are written with a `0x` prefix. +#[allow(missing_docs)] +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum Color { + Black, + Blue, + Green, + Red, + Cyan, + Magenta, + Yellow, + White, + Ansi256(u8), + Rgb(u8, u8, u8), + #[doc(hidden)] + __Nonexhaustive, +} + +impl Color { + fn into_termcolor(self) -> Option { + match self { + Color::Black => Some(termcolor::Color::Black), + Color::Blue => Some(termcolor::Color::Blue), + Color::Green => Some(termcolor::Color::Green), + Color::Red => Some(termcolor::Color::Red), + Color::Cyan => Some(termcolor::Color::Cyan), + Color::Magenta => Some(termcolor::Color::Magenta), + Color::Yellow => Some(termcolor::Color::Yellow), + Color::White => Some(termcolor::Color::White), + Color::Ansi256(value) => Some(termcolor::Color::Ansi256(value)), + Color::Rgb(r, g, b) => Some(termcolor::Color::Rgb(r, g, b)), + _ => None, + } + } +} diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/mod.rs b/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/mod.rs new file mode 100644 index 000000000000..df0f78591b8a --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/mod.rs @@ -0,0 +1,12 @@ +/* +This internal module contains the style and terminal writing implementation. + +Its public API is available when the `termcolor` crate is available. +The terminal printing is shimmed when the `termcolor` crate is not available. +*/ + +#[cfg_attr(feature = "termcolor", path = "extern_impl.rs")] +#[cfg_attr(not(feature = "termcolor"), path = "shim_impl.rs")] +mod imp; + +pub(in ::fmt) use self::imp::*; diff --git a/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/shim_impl.rs b/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/shim_impl.rs new file mode 100644 index 000000000000..fb4735902d01 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/fmt/writer/termcolor/shim_impl.rs @@ -0,0 +1,65 @@ +use std::io; + +use fmt::{WriteStyle, Target}; + +pub(in ::fmt::writer) mod glob { + +} + +pub(in ::fmt::writer) struct BufferWriter { + target: Target, +} + +pub(in ::fmt) struct Buffer(Vec); + +impl BufferWriter { + pub(in ::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self { + BufferWriter { + target: Target::Stderr, + } + } + + pub(in ::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self { + BufferWriter { + target: Target::Stdout, + } + } + + pub(in ::fmt::writer) fn buffer(&self) -> Buffer { + Buffer(Vec::new()) + } + + pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { + // This impl uses the `eprint` and `print` macros + // instead of using the streams directly. + // This is so their output can be captured by `cargo test` + let log = String::from_utf8_lossy(&buf.0); + + match self.target { + Target::Stderr => eprint!("{}", log), + Target::Stdout => print!("{}", log), + } + + Ok(()) + } +} + +impl Buffer { + pub(in ::fmt) fn clear(&mut self) { + self.0.clear(); + } + + pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result { + self.0.extend(buf); + Ok(buf.len()) + } + + pub(in ::fmt) fn flush(&mut self) -> io::Result<()> { + Ok(()) + } + + #[cfg(test)] + pub(in ::fmt) fn bytes(&self) -> &[u8] { + &self.0 + } +} \ No newline at end of file diff --git a/third_party/rust/env_logger-0.6.2/src/lib.rs b/third_party/rust/env_logger-0.6.2/src/lib.rs new file mode 100644 index 000000000000..37c43229144d --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/src/lib.rs @@ -0,0 +1,1173 @@ +// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! A simple logger configured via environment variables which writes +//! to stdout or stderr, for use with the logging facade exposed by the +//! [`log` crate][log-crate-url]. +//! +//! ## Example +//! +//! ``` +//! #[macro_use] extern crate log; +//! extern crate env_logger; +//! +//! use log::Level; +//! +//! fn main() { +//! env_logger::init(); +//! +//! debug!("this is a debug {}", "message"); +//! error!("this is printed by default"); +//! +//! if log_enabled!(Level::Info) { +//! let x = 3 * 4; // expensive computation +//! info!("the answer was: {}", x); +//! } +//! } +//! ``` +//! +//! Assumes the binary is `main`: +//! +//! ```{.bash} +//! $ RUST_LOG=error ./main +//! [2017-11-09T02:12:24Z ERROR main] this is printed by default +//! ``` +//! +//! ```{.bash} +//! $ RUST_LOG=info ./main +//! [2017-11-09T02:12:24Z ERROR main] this is printed by default +//! [2017-11-09T02:12:24Z INFO main] the answer was: 12 +//! ``` +//! +//! ```{.bash} +//! $ RUST_LOG=debug ./main +//! [2017-11-09T02:12:24Z DEBUG main] this is a debug message +//! [2017-11-09T02:12:24Z ERROR main] this is printed by default +//! [2017-11-09T02:12:24Z INFO main] the answer was: 12 +//! ``` +//! +//! You can also set the log level on a per module basis: +//! +//! ```{.bash} +//! $ RUST_LOG=main=info ./main +//! [2017-11-09T02:12:24Z ERROR main] this is printed by default +//! [2017-11-09T02:12:24Z INFO main] the answer was: 12 +//! ``` +//! +//! And enable all logging: +//! +//! ```{.bash} +//! $ RUST_LOG=main ./main +//! [2017-11-09T02:12:24Z DEBUG main] this is a debug message +//! [2017-11-09T02:12:24Z ERROR main] this is printed by default +//! [2017-11-09T02:12:24Z INFO main] the answer was: 12 +//! ``` +//! +//! If the binary name contains hyphens, you will need to replace +//! them with underscores: +//! +//! ```{.bash} +//! $ RUST_LOG=my_app ./my-app +//! [2017-11-09T02:12:24Z DEBUG my_app] this is a debug message +//! [2017-11-09T02:12:24Z ERROR my_app] this is printed by default +//! [2017-11-09T02:12:24Z INFO my_app] the answer was: 12 +//! ``` +//! +//! This is because Rust modules and crates cannot contain hyphens +//! in their name, although `cargo` continues to accept them. +//! +//! See the documentation for the [`log` crate][log-crate-url] for more +//! information about its API. +//! +//! ## Enabling logging +//! +//! Log levels are controlled on a per-module basis, and by default all logging +//! is disabled except for `error!`. Logging is controlled via the `RUST_LOG` +//! environment variable. The value of this environment variable is a +//! comma-separated list of logging directives. A logging directive is of the +//! form: +//! +//! ```text +//! path::to::module=level +//! ``` +//! +//! The path to the module is rooted in the name of the crate it was compiled +//! for, so if your program is contained in a file `hello.rs`, for example, to +//! turn on logging for this file you would use a value of `RUST_LOG=hello`. +//! Furthermore, this path is a prefix-search, so all modules nested in the +//! specified module will also have logging enabled. +//! +//! The actual `level` is optional to specify. If omitted, all logging will +//! be enabled. If specified, it must be one of the strings `debug`, `error`, +//! `info`, `warn`, or `trace`. +//! +//! As the log level for a module is optional, the module to enable logging for +//! is also optional. If only a `level` is provided, then the global log +//! level for all modules is set to this value. +//! +//! Some examples of valid values of `RUST_LOG` are: +//! +//! * `hello` turns on all logging for the 'hello' module +//! * `info` turns on all info logging +//! * `hello=debug` turns on debug logging for 'hello' +//! * `hello,std::option` turns on hello, and std's option logging +//! * `error,hello=warn` turn on global error logging and also warn for hello +//! +//! ## Filtering results +//! +//! A `RUST_LOG` directive may include a regex filter. The syntax is to append `/` +//! followed by a regex. Each message is checked against the regex, and is only +//! logged if it matches. Note that the matching is done after formatting the +//! log string but before adding any logging meta-data. There is a single filter +//! for all modules. +//! +//! Some examples: +//! +//! * `hello/foo` turns on all logging for the 'hello' module where the log +//! message includes 'foo'. +//! * `info/f.o` turns on all info logging where the log message includes 'foo', +//! 'f1o', 'fao', etc. +//! * `hello=debug/foo*foo` turns on debug logging for 'hello' where the log +//! message includes 'foofoo' or 'fofoo' or 'fooooooofoo', etc. +//! * `error,hello=warn/[0-9]scopes` turn on global error logging and also +//! warn for hello. In both cases the log message must include a single digit +//! number followed by 'scopes'. +//! +//! ## Capturing logs in tests +//! +//! Records logged during `cargo test` will not be captured by the test harness by default. +//! The [`Builder::is_test`] method can be used in unit tests to ensure logs will be captured: +//! +//! ``` +//! # #[macro_use] extern crate log; +//! # extern crate env_logger; +//! # fn main() {} +//! #[cfg(test)] +//! mod tests { +//! fn init() { +//! let _ = env_logger::builder().is_test(true).try_init(); +//! } +//! +//! #[test] +//! fn it_works() { +//! init(); +//! +//! info!("This record will be captured by `cargo test`"); +//! +//! assert_eq!(2, 1 + 1); +//! } +//! } +//! ``` +//! +//! Enabling test capturing comes at the expense of color and other style support +//! and may have performance implications. +//! +//! ## Disabling colors +//! +//! Colors and other styles can be configured with the `RUST_LOG_STYLE` +//! environment variable. It accepts the following values: +//! +//! * `auto` (default) will attempt to print style characters, but don't force the issue. +//! If the console isn't available on Windows, or if TERM=dumb, for example, then don't print colors. +//! * `always` will always print style characters even if they aren't supported by the terminal. +//! This includes emitting ANSI colors on Windows if the console API is unavailable. +//! * `never` will never print style characters. +//! +//! ## Tweaking the default format +//! +//! Parts of the default format can be excluded from the log output using the [`Builder`]. +//! The following example excludes the timestamp from the log output: +//! +//! ``` +//! env_logger::builder() +//! .default_format_timestamp(false) +//! .init(); +//! ``` +//! +//! ### Stability of the default format +//! +//! The default format won't optimise for long-term stability, and explicitly makes no +//! guarantees about the stability of its output across major, minor or patch version +//! bumps during `0.x`. +//! +//! If you want to capture or interpret the output of `env_logger` programmatically +//! then you should use a custom format. +//! +//! ### Using a custom format +//! +//! Custom formats can be provided as closures to the [`Builder`]. +//! These closures take a [`Formatter`] and `log::Record` as arguments: +//! +//! ``` +//! use std::io::Write; +//! +//! env_logger::builder() +//! .format(|buf, record| { +//! writeln!(buf, "{}: {}", record.level(), record.args()) +//! }) +//! .init(); +//! ``` +//! +//! See the [`fmt`] module for more details about custom formats. +//! +//! ## Specifying defaults for environment variables +//! +//! `env_logger` can read configuration from environment variables. +//! If these variables aren't present, the default value to use can be tweaked with the [`Env`] type. +//! The following example defaults to log `warn` and above if the `RUST_LOG` environment variable +//! isn't set: +//! +//! ``` +//! use env_logger::Env; +//! +//! env_logger::from_env(Env::default().default_filter_or("warn")).init(); +//! ``` +//! +//! [log-crate-url]: https://docs.rs/log/ +//! [`Builder`]: struct.Builder.html +//! [`Builder::is_test`]: struct.Builder.html#method.is_test +//! [`Env`]: struct.Env.html +//! [`fmt`]: fmt/index.html + +#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", + html_favicon_url = "https://www.rust-lang.org/static/images/favicon.ico", + html_root_url = "https://docs.rs/env_logger/0.6.2")] +#![cfg_attr(test, deny(warnings))] + +// When compiled for the rustc compiler itself we want to make sure that this is +// an unstable crate +#![cfg_attr(rustbuild, feature(staged_api, rustc_private))] +#![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))] + +#![deny(missing_debug_implementations, missing_docs, warnings)] + +extern crate log; + +#[cfg(feature = "termcolor")] +extern crate termcolor; +#[cfg(feature = "humantime")] +extern crate humantime; +#[cfg(feature = "atty")] +extern crate atty; + +use std::{env, io}; +use std::borrow::Cow; +use std::cell::RefCell; + +use log::{Log, LevelFilter, Record, SetLoggerError, Metadata}; + +pub mod filter; +pub mod fmt; + +pub use self::fmt::glob::*; + +use self::filter::Filter; +use self::fmt::Formatter; +use self::fmt::writer::{self, Writer}; + +/// The default name for the environment variable to read filters from. +pub const DEFAULT_FILTER_ENV: &'static str = "RUST_LOG"; + +/// The default name for the environment variable to read style preferences from. +pub const DEFAULT_WRITE_STYLE_ENV: &'static str = "RUST_LOG_STYLE"; + +/// Set of environment variables to configure from. +/// +/// # Default environment variables +/// +/// By default, the `Env` will read the following environment variables: +/// +/// - `RUST_LOG`: the level filter +/// - `RUST_LOG_STYLE`: whether or not to print styles with records. +/// +/// These sources can be configured using the builder methods on `Env`. +#[derive(Debug)] +pub struct Env<'a> { + filter: Var<'a>, + write_style: Var<'a>, +} + +#[derive(Debug)] +struct Var<'a> { + name: Cow<'a, str>, + default: Option>, +} + +/// The env logger. +/// +/// This struct implements the `Log` trait from the [`log` crate][log-crate-url], +/// which allows it to act as a logger. +/// +/// The [`init()`], [`try_init()`], [`Builder::init()`] and [`Builder::try_init()`] +/// methods will each construct a `Logger` and immediately initialize it as the +/// default global logger. +/// +/// If you'd instead need access to the constructed `Logger`, you can use +/// the associated [`Builder`] and install it with the +/// [`log` crate][log-crate-url] directly. +/// +/// [log-crate-url]: https://docs.rs/log/ +/// [`init()`]: fn.init.html +/// [`try_init()`]: fn.try_init.html +/// [`Builder::init()`]: struct.Builder.html#method.init +/// [`Builder::try_init()`]: struct.Builder.html#method.try_init +/// [`Builder`]: struct.Builder.html +pub struct Logger { + writer: Writer, + filter: Filter, + #[allow(unknown_lints, bare_trait_objects)] + format: Box io::Result<()> + Sync + Send>, +} + +/// `Builder` acts as builder for initializing a `Logger`. +/// +/// It can be used to customize the log format, change the environment variable used +/// to provide the logging directives and also set the default log level filter. +/// +/// # Examples +/// +/// ``` +/// #[macro_use] +/// extern crate log; +/// extern crate env_logger; +/// +/// use std::env; +/// use std::io::Write; +/// use log::LevelFilter; +/// use env_logger::Builder; +/// +/// fn main() { +/// let mut builder = Builder::from_default_env(); +/// +/// builder.format(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args())) +/// .filter(None, LevelFilter::Info) +/// .init(); +/// +/// error!("error message"); +/// info!("info message"); +/// } +/// ``` +#[derive(Default)] +pub struct Builder { + filter: filter::Builder, + writer: writer::Builder, + format: fmt::Builder, + built: bool, +} + +impl Builder { + /// Initializes the log builder with defaults. + /// + /// **NOTE:** This method won't read from any environment variables. + /// Use the [`filter`] and [`write_style`] methods to configure the builder + /// or use [`from_env`] or [`from_default_env`] instead. + /// + /// # Examples + /// + /// Create a new builder and configure filters and style: + /// + /// ``` + /// # extern crate log; + /// # extern crate env_logger; + /// # fn main() { + /// use log::LevelFilter; + /// use env_logger::{Builder, WriteStyle}; + /// + /// let mut builder = Builder::new(); + /// + /// builder.filter(None, LevelFilter::Info) + /// .write_style(WriteStyle::Always) + /// .init(); + /// # } + /// ``` + /// + /// [`filter`]: #method.filter + /// [`write_style`]: #method.write_style + /// [`from_env`]: #method.from_env + /// [`from_default_env`]: #method.from_default_env + pub fn new() -> Builder { + Default::default() + } + + /// Initializes the log builder from the environment. + /// + /// The variables used to read configuration from can be tweaked before + /// passing in. + /// + /// # Examples + /// + /// Initialise a logger reading the log filter from an environment variable + /// called `MY_LOG`: + /// + /// ``` + /// use env_logger::Builder; + /// + /// let mut builder = Builder::from_env("MY_LOG"); + /// builder.init(); + /// ``` + /// + /// Initialise a logger using the `MY_LOG` variable for filtering and + /// `MY_LOG_STYLE` for whether or not to write styles: + /// + /// ``` + /// use env_logger::{Builder, Env}; + /// + /// let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE"); + /// + /// let mut builder = Builder::from_env(env); + /// builder.init(); + /// ``` + pub fn from_env<'a, E>(env: E) -> Self + where + E: Into> + { + let mut builder = Builder::new(); + let env = env.into(); + + if let Some(s) = env.get_filter() { + builder.parse_filters(&s); + } + + if let Some(s) = env.get_write_style() { + builder.parse_write_style(&s); + } + + builder + } + + /// Initializes the log builder from the environment using default variable names. + /// + /// This method is a convenient way to call `from_env(Env::default())` without + /// having to use the `Env` type explicitly. The builder will use the + /// [default environment variables]. + /// + /// # Examples + /// + /// Initialise a logger using the default environment variables: + /// + /// ``` + /// use env_logger::Builder; + /// + /// let mut builder = Builder::from_default_env(); + /// builder.init(); + /// ``` + /// + /// [default environment variables]: struct.Env.html#default-environment-variables + pub fn from_default_env() -> Self { + Self::from_env(Env::default()) + } + + /// Sets the format function for formatting the log output. + /// + /// This function is called on each record logged and should format the + /// log record and output it to the given [`Formatter`]. + /// + /// The format function is expected to output the string directly to the + /// `Formatter` so that implementations can use the [`std::fmt`] macros + /// to format and output without intermediate heap allocations. The default + /// `env_logger` formatter takes advantage of this. + /// + /// # Examples + /// + /// Use a custom format to write only the log message: + /// + /// ``` + /// use std::io::Write; + /// use env_logger::Builder; + /// + /// let mut builder = Builder::new(); + /// + /// builder.format(|buf, record| writeln!(buf, "{}", record.args())); + /// ``` + /// + /// [`Formatter`]: fmt/struct.Formatter.html + /// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html + /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html + pub fn format(&mut self, format: F) -> &mut Self + where F: Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send + { + self.format.custom_format = Some(Box::new(format)); + self + } + + /// Use the default format. + /// + /// This method will clear any custom format set on the builder. + pub fn default_format(&mut self) -> &mut Self { + self.format.custom_format = None; + self + } + + /// Whether or not to write the level in the default format. + pub fn default_format_level(&mut self, write: bool) -> &mut Self { + self.format.default_format_level = write; + self + } + + /// Whether or not to write the module path in the default format. + pub fn default_format_module_path(&mut self, write: bool) -> &mut Self { + self.format.default_format_module_path = write; + self + } + + /// Whether or not to write the timestamp in the default format. + pub fn default_format_timestamp(&mut self, write: bool) -> &mut Self { + self.format.default_format_timestamp = write; + self + } + + /// Whether or not to write the timestamp with nanos. + pub fn default_format_timestamp_nanos(&mut self, write: bool) -> &mut Self { + self.format.default_format_timestamp_nanos = write; + self + } + + /// Adds a directive to the filter for a specific module. + /// + /// # Examples + /// + /// Only include messages for warning and above for logs in `path::to::module`: + /// + /// ``` + /// # extern crate log; + /// # extern crate env_logger; + /// # fn main() { + /// use log::LevelFilter; + /// use env_logger::Builder; + /// + /// let mut builder = Builder::new(); + /// + /// builder.filter_module("path::to::module", LevelFilter::Info); + /// # } + /// ``` + pub fn filter_module(&mut self, module: &str, level: LevelFilter) -> &mut Self { + self.filter.filter_module(module, level); + self + } + + /// Adds a directive to the filter for all modules. + /// + /// # Examples + /// + /// Only include messages for warning and above for logs in `path::to::module`: + /// + /// ``` + /// # extern crate log; + /// # extern crate env_logger; + /// # fn main() { + /// use log::LevelFilter; + /// use env_logger::Builder; + /// + /// let mut builder = Builder::new(); + /// + /// builder.filter_level(LevelFilter::Info); + /// # } + /// ``` + pub fn filter_level(&mut self, level: LevelFilter) -> &mut Self { + self.filter.filter_level(level); + self + } + + /// Adds filters to the logger. + /// + /// The given module (if any) will log at most the specified level provided. + /// If no module is provided then the filter will apply to all log messages. + /// + /// # Examples + /// + /// Only include messages for warning and above for logs in `path::to::module`: + /// + /// ``` + /// # extern crate log; + /// # extern crate env_logger; + /// # fn main() { + /// use log::LevelFilter; + /// use env_logger::Builder; + /// + /// let mut builder = Builder::new(); + /// + /// builder.filter(Some("path::to::module"), LevelFilter::Info); + /// # } + /// ``` + pub fn filter(&mut self, + module: Option<&str>, + level: LevelFilter) -> &mut Self { + self.filter.filter(module, level); + self + } + + /// Parses the directives string in the same form as the `RUST_LOG` + /// environment variable. + /// + /// See the module documentation for more details. + #[deprecated(since = "0.6.1", note = "use `parse_filters` instead.")] + pub fn parse(&mut self, filters: &str) -> &mut Self { + self.parse_filters(filters) + } + + /// Parses the directives string in the same form as the `RUST_LOG` + /// environment variable. + /// + /// See the module documentation for more details. + pub fn parse_filters(&mut self, filters: &str) -> &mut Self { + self.filter.parse(filters); + self + } + + /// Sets the target for the log output. + /// + /// Env logger can log to either stdout or stderr. The default is stderr. + /// + /// # Examples + /// + /// Write log message to `stdout`: + /// + /// ``` + /// use env_logger::{Builder, Target}; + /// + /// let mut builder = Builder::new(); + /// + /// builder.target(Target::Stdout); + /// ``` + pub fn target(&mut self, target: fmt::Target) -> &mut Self { + self.writer.target(target); + self + } + + /// Sets whether or not styles will be written. + /// + /// This can be useful in environments that don't support control characters + /// for setting colors. + /// + /// # Examples + /// + /// Never attempt to write styles: + /// + /// ``` + /// use env_logger::{Builder, WriteStyle}; + /// + /// let mut builder = Builder::new(); + /// + /// builder.write_style(WriteStyle::Never); + /// ``` + pub fn write_style(&mut self, write_style: fmt::WriteStyle) -> &mut Self { + self.writer.write_style(write_style); + self + } + + /// Parses whether or not to write styles in the same form as the `RUST_LOG_STYLE` + /// environment variable. + /// + /// See the module documentation for more details. + pub fn parse_write_style(&mut self, write_style: &str) -> &mut Self { + self.writer.parse_write_style(write_style); + self + } + + /// Sets whether or not the logger will be used in unit tests. + /// + /// If `is_test` is `true` then the logger will allow the testing framework to + /// capture log records rather than printing them to the terminal directly. + pub fn is_test(&mut self, is_test: bool) -> &mut Self { + self.writer.is_test(is_test); + self + } + + /// Initializes the global logger with the built env logger. + /// + /// This should be called early in the execution of a Rust program. Any log + /// events that occur before initialization will be ignored. + /// + /// # Errors + /// + /// This function will fail if it is called more than once, or if another + /// library has already initialized a global logger. + pub fn try_init(&mut self) -> Result<(), SetLoggerError> { + let logger = self.build(); + + let max_level = logger.filter(); + let r = log::set_boxed_logger(Box::new(logger)); + + if r.is_ok() { + log::set_max_level(max_level); + } + + r + } + + /// Initializes the global logger with the built env logger. + /// + /// This should be called early in the execution of a Rust program. Any log + /// events that occur before initialization will be ignored. + /// + /// # Panics + /// + /// This function will panic if it is called more than once, or if another + /// library has already initialized a global logger. + pub fn init(&mut self) { + self.try_init().expect("Builder::init should not be called after logger initialized"); + } + + /// Build an env logger. + /// + /// The returned logger implements the `Log` trait and can be installed manually + /// or nested within another logger. + pub fn build(&mut self) -> Logger { + assert!(!self.built, "attempt to re-use consumed builder"); + self.built = true; + + Logger { + writer: self.writer.build(), + filter: self.filter.build(), + format: self.format.build(), + } + } +} + +impl Logger { + /// Creates the logger from the environment. + /// + /// The variables used to read configuration from can be tweaked before + /// passing in. + /// + /// # Examples + /// + /// Create a logger reading the log filter from an environment variable + /// called `MY_LOG`: + /// + /// ``` + /// use env_logger::Logger; + /// + /// let logger = Logger::from_env("MY_LOG"); + /// ``` + /// + /// Create a logger using the `MY_LOG` variable for filtering and + /// `MY_LOG_STYLE` for whether or not to write styles: + /// + /// ``` + /// use env_logger::{Logger, Env}; + /// + /// let env = Env::new().filter_or("MY_LOG", "info").write_style_or("MY_LOG_STYLE", "always"); + /// + /// let logger = Logger::from_env(env); + /// ``` + pub fn from_env<'a, E>(env: E) -> Self + where + E: Into> + { + Builder::from_env(env).build() + } + + /// Creates the logger from the environment using default variable names. + /// + /// This method is a convenient way to call `from_env(Env::default())` without + /// having to use the `Env` type explicitly. The logger will use the + /// [default environment variables]. + /// + /// # Examples + /// + /// Creates a logger using the default environment variables: + /// + /// ``` + /// use env_logger::Logger; + /// + /// let logger = Logger::from_default_env(); + /// ``` + /// + /// [default environment variables]: struct.Env.html#default-environment-variables + pub fn from_default_env() -> Self { + Builder::from_default_env().build() + } + + /// Returns the maximum `LevelFilter` that this env logger instance is + /// configured to output. + pub fn filter(&self) -> LevelFilter { + self.filter.filter() + } + + /// Checks if this record matches the configured filter. + pub fn matches(&self, record: &Record) -> bool { + self.filter.matches(record) + } +} + +impl Log for Logger { + fn enabled(&self, metadata: &Metadata) -> bool { + self.filter.enabled(metadata) + } + + fn log(&self, record: &Record) { + if self.matches(record) { + // Log records are written to a thread-local buffer before being printed + // to the terminal. We clear these buffers afterwards, but they aren't shrinked + // so will always at least have capacity for the largest log record formatted + // on that thread. + // + // If multiple `Logger`s are used by the same threads then the thread-local + // formatter might have different color support. If this is the case the + // formatter and its buffer are discarded and recreated. + + thread_local! { + static FORMATTER: RefCell> = RefCell::new(None); + } + + FORMATTER.with(|tl_buf| { + // It's possible for implementations to sometimes + // log-while-logging (e.g. a `std::fmt` implementation logs + // internally) but it's super rare. If this happens make sure we + // at least don't panic and ship some output to the screen. + let mut a; + let mut b = None; + let tl_buf = match tl_buf.try_borrow_mut() { + Ok(f) => { + a = f; + &mut *a + } + Err(_) => &mut b, + }; + + // Check the buffer style. If it's different from the logger's + // style then drop the buffer and recreate it. + match *tl_buf { + Some(ref mut formatter) => { + if formatter.write_style() != self.writer.write_style() { + *formatter = Formatter::new(&self.writer) + } + }, + ref mut tl_buf => *tl_buf = Some(Formatter::new(&self.writer)) + } + + // The format is guaranteed to be `Some` by this point + let mut formatter = tl_buf.as_mut().unwrap(); + + let _ = (self.format)(&mut formatter, record).and_then(|_| formatter.print(&self.writer)); + + // Always clear the buffer afterwards + formatter.clear(); + }); + } + } + + fn flush(&self) {} +} + +impl<'a> Env<'a> { + /// Get a default set of environment variables. + pub fn new() -> Self { + Self::default() + } + + /// Specify an environment variable to read the filter from. + pub fn filter(mut self, filter_env: E) -> Self + where + E: Into> + { + self.filter = Var::new(filter_env); + + self + } + + /// Specify an environment variable to read the filter from. + /// + /// If the variable is not set, the default value will be used. + pub fn filter_or(mut self, filter_env: E, default: V) -> Self + where + E: Into>, + V: Into>, + { + self.filter = Var::new_with_default(filter_env, default); + + self + } + + /// Use the default environment variable to read the filter from. + /// + /// If the variable is not set, the default value will be used. + pub fn default_filter_or(mut self, default: V) -> Self + where + V: Into>, + { + self.filter = Var::new_with_default(DEFAULT_FILTER_ENV, default); + + self + } + + fn get_filter(&self) -> Option { + self.filter.get() + } + + /// Specify an environment variable to read the style from. + pub fn write_style(mut self, write_style_env: E) -> Self + where + E: Into> + { + self.write_style = Var::new(write_style_env); + + self + } + + /// Specify an environment variable to read the style from. + /// + /// If the variable is not set, the default value will be used. + pub fn write_style_or(mut self, write_style_env: E, default: V) -> Self + where + E: Into>, + V: Into>, + { + self.write_style = Var::new_with_default(write_style_env, default); + + self + } + + /// Use the default environment variable to read the style from. + /// + /// If the variable is not set, the default value will be used. + pub fn default_write_style_or(mut self, default: V) -> Self + where + V: Into>, + { + self.write_style = Var::new_with_default(DEFAULT_WRITE_STYLE_ENV, default); + + self + } + + fn get_write_style(&self) -> Option { + self.write_style.get() + } +} + +impl<'a> Var<'a> { + fn new(name: E) -> Self + where + E: Into>, + { + Var { + name: name.into(), + default: None, + } + } + + fn new_with_default(name: E, default: V) -> Self + where + E: Into>, + V: Into>, + { + Var { + name: name.into(), + default: Some(default.into()), + } + } + + fn get(&self) -> Option { + env::var(&*self.name) + .ok() + .or_else(|| self.default + .to_owned() + .map(|v| v.into_owned())) + } +} + +impl<'a, T> From for Env<'a> +where + T: Into> +{ + fn from(filter_env: T) -> Self { + Env::default().filter(filter_env.into()) + } +} + +impl<'a> Default for Env<'a> { + fn default() -> Self { + Env { + filter: Var::new(DEFAULT_FILTER_ENV), + write_style: Var::new(DEFAULT_WRITE_STYLE_ENV), + } + } +} + +mod std_fmt_impls { + use std::fmt; + use super::*; + + impl fmt::Debug for Logger{ + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + f.debug_struct("Logger") + .field("filter", &self.filter) + .finish() + } + } + + impl fmt::Debug for Builder{ + fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + if self.built { + f.debug_struct("Logger") + .field("built", &true) + .finish() + } else { + f.debug_struct("Logger") + .field("filter", &self.filter) + .field("writer", &self.writer) + .finish() + } + } + } +} + +/// Attempts to initialize the global logger with an env logger. +/// +/// This should be called early in the execution of a Rust program. Any log +/// events that occur before initialization will be ignored. +/// +/// # Errors +/// +/// This function will fail if it is called more than once, or if another +/// library has already initialized a global logger. +pub fn try_init() -> Result<(), SetLoggerError> { + try_init_from_env(Env::default()) +} + +/// Initializes the global logger with an env logger. +/// +/// This should be called early in the execution of a Rust program. Any log +/// events that occur before initialization will be ignored. +/// +/// # Panics +/// +/// This function will panic if it is called more than once, or if another +/// library has already initialized a global logger. +pub fn init() { + try_init().expect("env_logger::init should not be called after logger initialized"); +} + +/// Attempts to initialize the global logger with an env logger from the given +/// environment variables. +/// +/// This should be called early in the execution of a Rust program. Any log +/// events that occur before initialization will be ignored. +/// +/// # Examples +/// +/// Initialise a logger using the `MY_LOG` environment variable for filters +/// and `MY_LOG_STYLE` for writing colors: +/// +/// ``` +/// # extern crate env_logger; +/// use env_logger::{Builder, Env}; +/// +/// # fn run() -> Result<(), Box<::std::error::Error>> { +/// let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE"); +/// +/// env_logger::try_init_from_env(env)?; +/// +/// Ok(()) +/// # } +/// # fn main() { run().unwrap(); } +/// ``` +/// +/// # Errors +/// +/// This function will fail if it is called more than once, or if another +/// library has already initialized a global logger. +pub fn try_init_from_env<'a, E>(env: E) -> Result<(), SetLoggerError> +where + E: Into> +{ + let mut builder = Builder::from_env(env); + + builder.try_init() +} + +/// Initializes the global logger with an env logger from the given environment +/// variables. +/// +/// This should be called early in the execution of a Rust program. Any log +/// events that occur before initialization will be ignored. +/// +/// # Examples +/// +/// Initialise a logger using the `MY_LOG` environment variable for filters +/// and `MY_LOG_STYLE` for writing colors: +/// +/// ``` +/// use env_logger::{Builder, Env}; +/// +/// let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE"); +/// +/// env_logger::init_from_env(env); +/// ``` +/// +/// # Panics +/// +/// This function will panic if it is called more than once, or if another +/// library has already initialized a global logger. +pub fn init_from_env<'a, E>(env: E) +where + E: Into> +{ + try_init_from_env(env).expect("env_logger::init_from_env should not be called after logger initialized"); +} + +/// Create a new builder with the default environment variables. +/// +/// The builder can be configured before being initialized. +pub fn builder() -> Builder { + Builder::from_default_env() +} + +/// Create a builder from the given environment variables. +/// +/// The builder can be configured before being initialized. +pub fn from_env<'a, E>(env: E) -> Builder +where + E: Into> +{ + Builder::from_env(env) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn env_get_filter_reads_from_var_if_set() { + env::set_var("env_get_filter_reads_from_var_if_set", "from var"); + + let env = Env::new().filter_or("env_get_filter_reads_from_var_if_set", "from default"); + + assert_eq!(Some("from var".to_owned()), env.get_filter()); + } + + #[test] + fn env_get_filter_reads_from_default_if_var_not_set() { + env::remove_var("env_get_filter_reads_from_default_if_var_not_set"); + + let env = Env::new().filter_or("env_get_filter_reads_from_default_if_var_not_set", "from default"); + + assert_eq!(Some("from default".to_owned()), env.get_filter()); + } + + #[test] + fn env_get_write_style_reads_from_var_if_set() { + env::set_var("env_get_write_style_reads_from_var_if_set", "from var"); + + let env = Env::new().write_style_or("env_get_write_style_reads_from_var_if_set", "from default"); + + assert_eq!(Some("from var".to_owned()), env.get_write_style()); + } + + #[test] + fn env_get_write_style_reads_from_default_if_var_not_set() { + env::remove_var("env_get_write_style_reads_from_default_if_var_not_set"); + + let env = Env::new().write_style_or("env_get_write_style_reads_from_default_if_var_not_set", "from default"); + + assert_eq!(Some("from default".to_owned()), env.get_write_style()); + } +} diff --git a/third_party/rust/env_logger-0.6.2/tests/init-twice-retains-filter.rs b/third_party/rust/env_logger-0.6.2/tests/init-twice-retains-filter.rs new file mode 100644 index 000000000000..c1256ef6d14a --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/tests/init-twice-retains-filter.rs @@ -0,0 +1,40 @@ +extern crate log; +extern crate env_logger; + +use std::process; +use std::env; +use std::str; + +fn main() { + if env::var("YOU_ARE_TESTING_NOW").is_ok() { + // Init from the env (which should set the max level to `Debug`) + env_logger::init(); + + assert_eq!(log::LevelFilter::Debug, log::max_level()); + + // Init again using a different max level + // This shouldn't clobber the level that was previously set + env_logger::Builder::new() + .parse_filters("info") + .try_init() + .unwrap_err(); + + assert_eq!(log::LevelFilter::Debug, log::max_level()); + return + } + + let exe = env::current_exe().unwrap(); + let out = process::Command::new(exe) + .env("YOU_ARE_TESTING_NOW", "1") + .env("RUST_LOG", "debug") + .output() + .unwrap_or_else(|e| panic!("Unable to start child process: {}", e)); + if out.status.success() { + return + } + + println!("test failed: {}", out.status); + println!("--- stdout\n{}", str::from_utf8(&out.stdout).unwrap()); + println!("--- stderr\n{}", str::from_utf8(&out.stderr).unwrap()); + process::exit(1); +} diff --git a/third_party/rust/env_logger-0.6.2/tests/log-in-log.rs b/third_party/rust/env_logger-0.6.2/tests/log-in-log.rs new file mode 100644 index 000000000000..6b2c47e7a6f0 --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/tests/log-in-log.rs @@ -0,0 +1,38 @@ +#[macro_use] extern crate log; +extern crate env_logger; + +use std::process; +use std::fmt; +use std::env; +use std::str; + +struct Foo; + +impl fmt::Display for Foo { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + info!("test"); + f.write_str("bar") + } +} + +fn main() { + env_logger::init(); + if env::var("YOU_ARE_TESTING_NOW").is_ok() { + return info!("{}", Foo); + } + + let exe = env::current_exe().unwrap(); + let out = process::Command::new(exe) + .env("YOU_ARE_TESTING_NOW", "1") + .env("RUST_LOG", "debug") + .output() + .unwrap_or_else(|e| panic!("Unable to start child process: {}", e)); + if out.status.success() { + return + } + + println!("test failed: {}", out.status); + println!("--- stdout\n{}", str::from_utf8(&out.stdout).unwrap()); + println!("--- stderr\n{}", str::from_utf8(&out.stderr).unwrap()); + process::exit(1); +} diff --git a/third_party/rust/env_logger-0.6.2/tests/regexp_filter.rs b/third_party/rust/env_logger-0.6.2/tests/regexp_filter.rs new file mode 100644 index 000000000000..d23e9223eabd --- /dev/null +++ b/third_party/rust/env_logger-0.6.2/tests/regexp_filter.rs @@ -0,0 +1,51 @@ +#[macro_use] extern crate log; +extern crate env_logger; + +use std::process; +use std::env; +use std::str; + +fn main() { + if env::var("LOG_REGEXP_TEST").ok() == Some(String::from("1")) { + child_main(); + } else { + parent_main() + } +} + +fn child_main() { + env_logger::init(); + info!("XYZ Message"); +} + +fn run_child(rust_log: String) -> bool { + let exe = env::current_exe().unwrap(); + let out = process::Command::new(exe) + .env("LOG_REGEXP_TEST", "1") + .env("RUST_LOG", rust_log) + .output() + .unwrap_or_else(|e| panic!("Unable to start child process: {}", e)); + str::from_utf8(out.stderr.as_ref()).unwrap().contains("XYZ Message") +} + +fn assert_message_printed(rust_log: &str) { + if !run_child(rust_log.to_string()) { + panic!("RUST_LOG={} should allow the test log message", rust_log) + } +} + +fn assert_message_not_printed(rust_log: &str) { + if run_child(rust_log.to_string()) { + panic!("RUST_LOG={} should not allow the test log message", rust_log) + } +} + +fn parent_main() { + // test normal log severity levels + assert_message_printed("info"); + assert_message_not_printed("warn"); + + // test of regular expression filters + assert_message_printed("info/XYZ"); + assert_message_not_printed("info/XXX"); +} diff --git a/third_party/rust/env_logger/.cargo-checksum.json b/third_party/rust/env_logger/.cargo-checksum.json index e5514ae7e219..f7fcd9e6bc23 100644 --- a/third_party/rust/env_logger/.cargo-checksum.json +++ b/third_party/rust/env_logger/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"CHANGELOG.md":"7c044d74477515ab39287a4caff27eb96daebaed8b9f9b6a1d1c081a7b42d4a7","Cargo.lock":"132c1f881b80a79314567a6993141c6204495fec144cdcec1729f2a3e0fec18b","Cargo.toml":"b60137f1fd54001ca4d8be1d0bbec154225a44c8f4fa3576078bdad55216d357","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"0e231c1c4ad51ff0239062297bdaa69aeb34a8692e3f814188ce1e0ade8583d5","examples/custom_default_format.rs":"799c439f61cb711078f8aa584db537a5758c25b90d44767849dae2ad3822885c","examples/custom_format.rs":"ac8323e2febf8b8ff7238bd254fbbbfb3183da5af84f7f3a261fd9ad892c9ab6","examples/custom_logger.rs":"99fb3c9761ad4c5fe73f4ec2a2bd44b4acf6d1f7b7cfaa16bf0373665d3e2a4b","examples/default.rs":"ac96427611784d310704f738c7a29ebddd7930c8a70ad3c464c4d3eae4cf74a3","examples/direct_logger.rs":"549f6a10e0903d06aca2cc7ba82415b07a23392676101c9bc7aa72b4a9b0b9e2","examples/filters_from_code.rs":"84bd82803683d19ae96f85edcf4ee38cda028c2dbde923dddecc8563453b18e2","src/filter/mod.rs":"de471579c5db400c5ed11b9d7c9fc62686068b42798c58f7165806319ab7ec09","src/filter/regex.rs":"5fff47d1d4d0aa3f2bab90636127d3e72aebf800c3b78faba99637220ffdf865","src/filter/string.rs":"52bbd047c31a1afdb3cd1c11629b956f21b3f47bf22e06421baf3d693a045e59","src/fmt/humantime/extern_impl.rs":"cd2538e7a03fd3ad6c843af3c3d4016ca96cadaefee32cf9b37329c4787e6552","src/fmt/humantime/mod.rs":"408496eb21344c654b9e06da2a2df86de56e427147bb7f7b47851e0da976c003","src/fmt/humantime/shim_impl.rs":"7c2fdf4031f5568b716df14842b0d32bc03ff398763f4849960df7f9632a5bb2","src/fmt/mod.rs":"5104dad2fd14bc18ab6ab46e7c2bc5752b509d9fc934fb99f0ebc126728f8f04","src/fmt/writer/atty.rs":"3e9fd61d291d0919f7aa7119a26dd15d920df8783b4ae57bcf2c3cb6f3ff06b5","src/fmt/writer/mod.rs":"583f6616e0cf21955a530baa332fb7a99bf4fcd418a2367bbd1e733a06a22318","src/fmt/writer/termcolor/extern_impl.rs":"15e048be128568abcdd0ce99dafffe296df26131d4aa05921585761d31c11db5","src/fmt/writer/termcolor/mod.rs":"a3cf956aec030e0f940e4eaefe58d7703857eb900022286e328e05e5f61de183","src/fmt/writer/termcolor/shim_impl.rs":"bdd479c4e933b14ba02a3c1a9fe30eb51bcdf600e23cebd044d68683fdaad037","src/lib.rs":"2c5ab92ee141022f3e657b0f81e84e5ee4e7fad9fb648204e00ed4fb03d4166f","tests/init-twice-retains-filter.rs":"00524ce0f6779981b695bad1fdd244f87b76c126aeccd8b4ff77ef9e6325478b","tests/log-in-log.rs":"41126910998adfbac771c2a1237fecbc5437344f8e4dfc2f93235bab764a087e","tests/regexp_filter.rs":"44aa6c39de894be090e37083601e501cfffb15e3c0cd36209c48abdf3e2cb120"},"package":"aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3"} \ No newline at end of file +{"files":{"CHANGELOG.md":"7c044d74477515ab39287a4caff27eb96daebaed8b9f9b6a1d1c081a7b42d4a7","Cargo.lock":"b1394b6c58241027832cc714a0754902d82aa1f6923ab478c318739462e565ca","Cargo.toml":"2961879155d753ba90ecd98c17875c82007a6973c95867e86bc1ec5bd4f5db41","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"0bf17650e07b88f1486f033643c1e82517caa69410e6faeaa352782d9231d63e","examples/custom_default_format.rs":"ae18cd0e765cf1f16568f9879925861d6f004481f955b58af5ed8fd04b0fca99","examples/custom_format.rs":"b0f41b7a3e6fe7582871281f4244c62c66b0d724bfc678907f67185a784e82b4","examples/custom_logger.rs":"6eeef506681a46925117e8f89395cdf4fea60a0d1f6a420e51768e790272dcde","examples/default.rs":"7ed1c6a8a8fe457a86676bd3a75c07d4ec7fb54147cf2825c9d299a5878a24cd","examples/direct_logger.rs":"ee20c25379c396e5e74e963290a4d8773a86f3fe10193f61fb1efd1c7271faf4","examples/filters_from_code.rs":"7f007b0dfa5a3964f839134824dc3684bf2f3c3d7b4c36c580cd029df5f9308b","src/filter/mod.rs":"5da7e51e7b77efdd4d2b5445e5d0264be2c897909d0f86eb553e16611307aed2","src/filter/regex.rs":"bdf875bac25e089e1e462f5dd01a88678067c24118ecd6268561c6a6af39747d","src/filter/string.rs":"fac54d51189fc0b5d2bff334b7a7e465177b431e3428299e345e1f90062d832e","src/fmt/humantime/extern_impl.rs":"f3087b29eedb8b4d5573621ad206e48a2eac72a77277be3b0e631d7dc9fb7a2e","src/fmt/humantime/mod.rs":"f4111c26cf2ffb85c1d639bd7674d55af7e1736e7e98c52f7be3070046a3253f","src/fmt/humantime/shim_impl.rs":"cce9a252abd5952fa109a72b1dfb85a593d237e22606b2b608a32c69184560e9","src/fmt/mod.rs":"4ab11971a73eb5fe9b40f0bca6dfc404321dd9e2ffcf87d911408e7183dc8362","src/fmt/writer/atty.rs":"69d9dd26c430000cd2d40f9c68b2e77cd492fec22921dd2c16864301252583e0","src/fmt/writer/mod.rs":"1e0feb4dee3ee86c4c24f49566673e99ec85765869105a07a2fc7436d7640cfe","src/fmt/writer/termcolor/extern_impl.rs":"89e9f2e66b914ddc960ad9a4355265a5db5d7be410b139cf2b54ca99207374a7","src/fmt/writer/termcolor/mod.rs":"a790f9391a50cd52be6823e3e55942de13a8d12e23d63765342ae9e8dd6d091c","src/fmt/writer/termcolor/shim_impl.rs":"d93786671d6a89fc2912f77f04b8cb0b82d67277d255d15ac31bfc1bc4464e30","src/lib.rs":"3cbc4f4d3fe51c43fc45a2f435c141f0de5b40b65ba0d2c7d16bb58c04d10898","tests/init-twice-retains-filter.rs":"be5cd2132342d89ede1f5c4266173bb3c4d51cc22a1847f133d299a1c5430ccb","tests/log-in-log.rs":"29fecc65c1e0d1c22d79c97e7ca843ad44a91f27934148d7a05c48899a3f39d8","tests/log_tls_dtors.rs":"7320667d774a9b05037f7bf273fb2574dec0705707692a9cd2f46f4cd5bc68dd","tests/regexp_filter.rs":"a84263c995b534b6479a1d0abadf63f4f0264958ff86d9173d6b2139b82c4dc5"},"package":"44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"} \ No newline at end of file diff --git a/third_party/rust/env_logger/Cargo.lock b/third_party/rust/env_logger/Cargo.lock index 565fc3a87645..bf04607aae65 100644 --- a/third_party/rust/env_logger/Cargo.lock +++ b/third_party/rust/env_logger/Cargo.lock @@ -25,18 +25,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "env_logger" -version = "0.6.2" +version = "0.7.1" dependencies = [ "atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "humantime" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -54,7 +54,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "log" -version = "0.4.6" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -189,10 +189,10 @@ dependencies = [ "checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" "checksum atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "af80143d6f7608d746df1520709e5d141c96f240b0e62b0aa41bdfb53374d9d4" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" -"checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" +"checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" "checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b" -"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" +"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" diff --git a/third_party/rust/env_logger/Cargo.toml b/third_party/rust/env_logger/Cargo.toml index 1d028baaaa11..c34a1ee2391d 100644 --- a/third_party/rust/env_logger/Cargo.toml +++ b/third_party/rust/env_logger/Cargo.toml @@ -11,8 +11,9 @@ # will likely look very different (and much more reasonable) [package] +edition = "2018" name = "env_logger" -version = "0.6.2" +version = "0.7.1" authors = ["The Rust Project Developers"] description = "A logging implementation for `log` which is configured via an environment\nvariable.\n" documentation = "https://docs.rs/env_logger" @@ -30,6 +31,10 @@ harness = false name = "log-in-log" harness = false +[[test]] +name = "log_tls_dtors" +harness = false + [[test]] name = "init-twice-retains-filter" harness = false @@ -38,11 +43,11 @@ version = "0.2.5" optional = true [dependencies.humantime] -version = "1.1" +version = "1.3" optional = true [dependencies.log] -version = "0.4" +version = "0.4.8" features = ["std"] [dependencies.regex] diff --git a/third_party/rust/env_logger/README.md b/third_party/rust/env_logger/README.md index 8dc92f12a811..34e4194a88fb 100644 --- a/third_party/rust/env_logger/README.md +++ b/third_party/rust/env_logger/README.md @@ -16,7 +16,7 @@ It must be added along with `log` to the project dependencies: ```toml [dependencies] log = "0.4.0" -env_logger = "0.6.2" +env_logger = "0.7.1" ``` `env_logger` must be initialized as early as possible in the project. After it's initialized, you can use the `log` macros to do actual logging. @@ -24,7 +24,6 @@ env_logger = "0.6.2" ```rust #[macro_use] extern crate log; -extern crate env_logger; fn main() { env_logger::init(); @@ -54,7 +53,7 @@ Tests can use the `env_logger` crate to see log messages generated during that t log = "0.4.0" [dev-dependencies] -env_logger = "0.6.2" +env_logger = "0.7.1" ``` ```rust @@ -69,7 +68,6 @@ fn add_one(num: i32) -> i32 { #[cfg(test)] mod tests { use super::*; - extern crate env_logger; fn init() { let _ = env_logger::builder().is_test(true).try_init(); diff --git a/third_party/rust/env_logger/examples/custom_default_format.rs b/third_party/rust/env_logger/examples/custom_default_format.rs index d1a45b6089f2..43979247ca5b 100644 --- a/third_party/rust/env_logger/examples/custom_default_format.rs +++ b/third_party/rust/env_logger/examples/custom_default_format.rs @@ -19,9 +19,8 @@ If you want to control the logging output completely, see the `custom_logger` ex #[macro_use] extern crate log; -extern crate env_logger; -use env_logger::{Env, Builder}; +use env_logger::{Builder, Env}; fn init_logger() { let env = Env::default() @@ -30,9 +29,7 @@ fn init_logger() { let mut builder = Builder::from_env(env); - builder - .default_format_level(false) - .default_format_timestamp_nanos(true); + builder.format_level(false).format_timestamp_nanos(); builder.init(); } diff --git a/third_party/rust/env_logger/examples/custom_format.rs b/third_party/rust/env_logger/examples/custom_format.rs index c4563f50d20b..df5a8e5b9cd9 100644 --- a/third_party/rust/env_logger/examples/custom_format.rs +++ b/third_party/rust/env_logger/examples/custom_format.rs @@ -17,38 +17,37 @@ $ export MY_LOG_STYLE=never If you want to control the logging output completely, see the `custom_logger` example. */ -#[macro_use] -extern crate log; -extern crate env_logger; - -use std::io::Write; - -use env_logger::{Env, Builder, fmt}; - -fn init_logger() { - let env = Env::default() - .filter("MY_LOG_LEVEL") - .write_style("MY_LOG_STYLE"); - - let mut builder = Builder::from_env(env); - - // Use a different format for writing log records - // The colors are only available when the `termcolor` dependency is (which it is by default) - #[cfg(feature = "termcolor")] - builder.format(|buf, record| { - let mut style = buf.style(); - style.set_bg(fmt::Color::Yellow).set_bold(true); - - let timestamp = buf.timestamp(); - - writeln!(buf, "My formatted log ({}): {}", timestamp, style.value(record.args())) - }); - - builder.init(); -} - +#[cfg(all(feature = "termcolor", feature = "humantime"))] fn main() { + use env_logger::{fmt, Builder, Env}; + use std::io::Write; + + fn init_logger() { + let env = Env::default() + .filter("MY_LOG_LEVEL") + .write_style("MY_LOG_STYLE"); + + Builder::from_env(env) + .format(|buf, record| { + let mut style = buf.style(); + style.set_bg(fmt::Color::Yellow).set_bold(true); + + let timestamp = buf.timestamp(); + + writeln!( + buf, + "My formatted log ({}): {}", + timestamp, + style.value(record.args()) + ) + }) + .init(); + } + init_logger(); - info!("a log from `MyLogger`"); + log::info!("a log from `MyLogger`"); } + +#[cfg(not(all(feature = "termcolor", feature = "humantime")))] +fn main() {} diff --git a/third_party/rust/env_logger/examples/custom_logger.rs b/third_party/rust/env_logger/examples/custom_logger.rs index 792c9c8e5ee3..85de45b2d97d 100644 --- a/third_party/rust/env_logger/examples/custom_logger.rs +++ b/third_party/rust/env_logger/examples/custom_logger.rs @@ -12,12 +12,12 @@ If you only want to change the way logs are formatted, look at the `custom_forma #[macro_use] extern crate log; -extern crate env_logger; + use env_logger::filter::Filter; use log::{Log, Metadata, Record, SetLoggerError}; struct MyLogger { - inner: Filter + inner: Filter, } impl MyLogger { @@ -26,7 +26,7 @@ impl MyLogger { let mut builder = Builder::from_env("MY_LOG_LEVEL"); MyLogger { - inner: builder.build() + inner: builder.build(), } } @@ -50,7 +50,7 @@ impl Log for MyLogger { } } - fn flush(&self) { } + fn flush(&self) {} } fn main() { diff --git a/third_party/rust/env_logger/examples/default.rs b/third_party/rust/env_logger/examples/default.rs index 302e38a20842..67bb030745e7 100644 --- a/third_party/rust/env_logger/examples/default.rs +++ b/third_party/rust/env_logger/examples/default.rs @@ -17,7 +17,6 @@ $ export MY_LOG_STYLE=never #[macro_use] extern crate log; -extern crate env_logger; use env_logger::Env; diff --git a/third_party/rust/env_logger/examples/direct_logger.rs b/third_party/rust/env_logger/examples/direct_logger.rs index 410230bcdc24..4ba023fae3bc 100644 --- a/third_party/rust/env_logger/examples/direct_logger.rs +++ b/third_party/rust/env_logger/examples/direct_logger.rs @@ -4,9 +4,6 @@ Using `env_logger::Logger` and the `log::Log` trait directly. This example doesn't rely on environment variables, or having a static logger installed. */ -extern crate log; -extern crate env_logger; - fn record() -> log::Record<'static> { let error_metadata = log::MetadataBuilder::new() .target("myApp") @@ -34,7 +31,7 @@ fn main() { .filter(None, log::LevelFilter::Error) .write_style(env_logger::WriteStyle::Never) .build(); - + stylish_logger.log(&record()); unstylish_logger.log(&record()); -} \ No newline at end of file +} diff --git a/third_party/rust/env_logger/examples/filters_from_code.rs b/third_party/rust/env_logger/examples/filters_from_code.rs index ef5b96910dbd..4137c91898b0 100644 --- a/third_party/rust/env_logger/examples/filters_from_code.rs +++ b/third_party/rust/env_logger/examples/filters_from_code.rs @@ -4,7 +4,6 @@ Specify logging filters in code instead of using an environment variable. #[macro_use] extern crate log; -extern crate env_logger; fn main() { env_logger::builder() diff --git a/third_party/rust/env_logger/src/filter/mod.rs b/third_party/rust/env_logger/src/filter/mod.rs index a0fe6a2506b7..a994f4dc6cd6 100644 --- a/third_party/rust/env_logger/src/filter/mod.rs +++ b/third_party/rust/env_logger/src/filter/mod.rs @@ -1,15 +1,15 @@ //! Filtering for log records. -//! +//! //! This module contains the log filtering used by `env_logger` to match records. -//! You can use the `Filter` type in your own logger implementation to use the same -//! filter parsing and matching as `env_logger`. For more details about the format +//! You can use the `Filter` type in your own logger implementation to use the same +//! filter parsing and matching as `env_logger`. For more details about the format //! for directive strings see [Enabling Logging]. -//! +//! //! ## Using `env_logger` in your own logger //! //! You can use `env_logger`'s filtering functionality with your own logger. -//! Call [`Builder::parse`] to parse directives from a string when constructing -//! your logger. Call [`Filter::matches`] to check whether a record should be +//! Call [`Builder::parse`] to parse directives from a string when constructing +//! your logger. Call [`Filter::matches`] to check whether a record should be //! logged based on the parsed filters when log records are received. //! //! ``` @@ -54,15 +54,15 @@ //! } //! # fn main() {} //! ``` -//! +//! //! [Enabling Logging]: ../index.html#enabling-logging //! [`Builder::parse`]: struct.Builder.html#method.parse //! [`Filter::matches`]: struct.Filter.html#method.matches +use log::{Level, LevelFilter, Metadata, Record}; use std::env; -use std::mem; use std::fmt; -use log::{Level, LevelFilter, Record, Metadata}; +use std::mem; #[cfg(feature = "regex")] #[path = "regex.rs"] @@ -73,11 +73,11 @@ mod inner; mod inner; /// A log filter. -/// +/// /// This struct can be used to determine whether or not a log record /// should be written to the output. /// Use the [`Builder`] type to parse and construct a `Filter`. -/// +/// /// [`Builder`]: struct.Builder.html pub struct Filter { directives: Vec, @@ -85,10 +85,10 @@ pub struct Filter { } /// A builder for a log filter. -/// +/// /// It can be used to parse a set of directives from a string before building /// a [`Filter`] instance. -/// +/// /// ## Example /// /// ``` @@ -111,7 +111,7 @@ pub struct Filter { /// let filter = builder.build(); /// } /// ``` -/// +/// /// [`Filter`]: struct.Filter.html pub struct Builder { directives: Vec, @@ -148,7 +148,8 @@ impl Filter { /// } /// ``` pub fn filter(&self) -> LevelFilter { - self.directives.iter() + self.directives + .iter() .map(|d| d.level) .max() .unwrap_or(LevelFilter::Off) @@ -213,9 +214,7 @@ impl Builder { /// /// The given module (if any) will log at most the specified level provided. /// If no module is provided then the filter will apply to all log messages. - pub fn filter(&mut self, - module: Option<&str>, - level: LevelFilter) -> &mut Self { + pub fn filter(&mut self, module: Option<&str>, level: LevelFilter) -> &mut Self { self.directives.push(Directive { name: module.map(|s| s.to_string()), level, @@ -226,7 +225,7 @@ impl Builder { /// Parses the directives string. /// /// See the [Enabling Logging] section for more details. - /// + /// /// [Enabling Logging]: ../index.html#enabling-logging pub fn parse(&mut self, filters: &str) -> &mut Self { let (directives, filter) = parse_spec(filters); @@ -274,7 +273,7 @@ impl Default for Builder { } impl fmt::Debug for Filter { - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Filter") .field("filter", &self.filter) .field("directives", &self.directives) @@ -283,16 +282,14 @@ impl fmt::Debug for Filter { } impl fmt::Debug for Builder { - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.built { - f.debug_struct("Filter") - .field("built", &true) - .finish() + f.debug_struct("Filter").field("built", &true).finish() } else { f.debug_struct("Filter") - .field("filter", &self.filter) - .field("directives", &self.directives) - .finish() + .field("filter", &self.filter) + .field("directives", &self.directives) + .finish() } } } @@ -306,68 +303,75 @@ fn parse_spec(spec: &str) -> (Vec, Option) { let mods = parts.next(); let filter = parts.next(); if parts.next().is_some() { - eprintln!("warning: invalid logging spec '{}', \ - ignoring it (too many '/'s)", spec); + eprintln!( + "warning: invalid logging spec '{}', \ + ignoring it (too many '/'s)", + spec + ); return (dirs, None); } - mods.map(|m| { for s in m.split(',') { - if s.len() == 0 { continue } - let mut parts = s.split('='); - let (log_level, name) = match (parts.next(), parts.next().map(|s| s.trim()), parts.next()) { - (Some(part0), None, None) => { - // if the single argument is a log-level string or number, - // treat that as a global fallback - match part0.parse() { - Ok(num) => (num, None), - Err(_) => (LevelFilter::max(), Some(part0)), - } + mods.map(|m| { + for s in m.split(',') { + if s.len() == 0 { + continue; } - (Some(part0), Some(""), None) => (LevelFilter::max(), Some(part0)), - (Some(part0), Some(part1), None) => { - match part1.parse() { - Ok(num) => (num, Some(part0)), - _ => { - eprintln!("warning: invalid logging spec '{}', \ - ignoring it", part1); - continue + let mut parts = s.split('='); + let (log_level, name) = + match (parts.next(), parts.next().map(|s| s.trim()), parts.next()) { + (Some(part0), None, None) => { + // if the single argument is a log-level string or number, + // treat that as a global fallback + match part0.parse() { + Ok(num) => (num, None), + Err(_) => (LevelFilter::max(), Some(part0)), + } } - } - }, - _ => { - eprintln!("warning: invalid logging spec '{}', \ - ignoring it", s); - continue - } - }; - dirs.push(Directive { - name: name.map(|s| s.to_string()), - level: log_level, - }); - }}); + (Some(part0), Some(""), None) => (LevelFilter::max(), Some(part0)), + (Some(part0), Some(part1), None) => match part1.parse() { + Ok(num) => (num, Some(part0)), + _ => { + eprintln!( + "warning: invalid logging spec '{}', \ + ignoring it", + part1 + ); + continue; + } + }, + _ => { + eprintln!( + "warning: invalid logging spec '{}', \ + ignoring it", + s + ); + continue; + } + }; + dirs.push(Directive { + name: name.map(|s| s.to_string()), + level: log_level, + }); + } + }); - let filter = filter.map_or(None, |filter| { - match inner::Filter::new(filter) { - Ok(re) => Some(re), - Err(e) => { - eprintln!("warning: invalid regex filter - {}", e); - None - } + let filter = filter.map_or(None, |filter| match inner::Filter::new(filter) { + Ok(re) => Some(re), + Err(e) => { + eprintln!("warning: invalid regex filter - {}", e); + None } }); return (dirs, filter); } - // Check whether a level and target are enabled by the set of directives. fn enabled(directives: &[Directive], level: Level, target: &str) -> bool { // Search for the longest match, the vector is assumed to be pre-sorted. for directive in directives.iter().rev() { match directive.name { - Some(ref name) if !target.starts_with(&**name) => {}, - Some(..) | None => { - return level <= directive.level - } + Some(ref name) if !target.starts_with(&**name) => {} + Some(..) | None => return level <= directive.level, } } false @@ -377,7 +381,7 @@ fn enabled(directives: &[Directive], level: Level, target: &str) -> bool { mod tests { use log::{Level, LevelFilter}; - use super::{Builder, Filter, Directive, parse_spec, enabled}; + use super::{enabled, parse_spec, Builder, Directive, Filter}; fn make_logger_filter(dirs: Vec) -> Filter { let mut logger = Builder::new().build(); @@ -395,10 +399,10 @@ mod tests { #[test] fn filter_beginning_longest_match() { let logger = Builder::new() - .filter(Some("crate2"), LevelFilter::Info) - .filter(Some("crate2::mod"), LevelFilter::Debug) - .filter(Some("crate1::mod1"), LevelFilter::Warn) - .build(); + .filter(Some("crate2"), LevelFilter::Info) + .filter(Some("crate2::mod"), LevelFilter::Debug) + .filter(Some("crate1::mod1"), LevelFilter::Warn) + .build(); assert!(enabled(&logger.directives, Level::Debug, "crate2::mod1")); assert!(!enabled(&logger.directives, Level::Debug, "crate2")); } @@ -415,12 +419,12 @@ mod tests { let logger = make_logger_filter(vec![ Directive { name: Some("crate2".to_string()), - level: LevelFilter::Info + level: LevelFilter::Info, }, Directive { name: Some("crate1::mod1".to_string()), - level: LevelFilter::Warn - } + level: LevelFilter::Warn, + }, ]); assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1")); assert!(!enabled(&logger.directives, Level::Info, "crate1::mod1")); @@ -431,8 +435,14 @@ mod tests { #[test] fn no_match() { let logger = make_logger_filter(vec![ - Directive { name: Some("crate2".to_string()), level: LevelFilter::Info }, - Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn } + Directive { + name: Some("crate2".to_string()), + level: LevelFilter::Info, + }, + Directive { + name: Some("crate1::mod1".to_string()), + level: LevelFilter::Warn, + }, ]); assert!(!enabled(&logger.directives, Level::Warn, "crate3")); } @@ -440,8 +450,14 @@ mod tests { #[test] fn match_beginning() { let logger = make_logger_filter(vec![ - Directive { name: Some("crate2".to_string()), level: LevelFilter::Info }, - Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn } + Directive { + name: Some("crate2".to_string()), + level: LevelFilter::Info, + }, + Directive { + name: Some("crate1::mod1".to_string()), + level: LevelFilter::Warn, + }, ]); assert!(enabled(&logger.directives, Level::Info, "crate2::mod1")); } @@ -449,9 +465,18 @@ mod tests { #[test] fn match_beginning_longest_match() { let logger = make_logger_filter(vec![ - Directive { name: Some("crate2".to_string()), level: LevelFilter::Info }, - Directive { name: Some("crate2::mod".to_string()), level: LevelFilter::Debug }, - Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn } + Directive { + name: Some("crate2".to_string()), + level: LevelFilter::Info, + }, + Directive { + name: Some("crate2::mod".to_string()), + level: LevelFilter::Debug, + }, + Directive { + name: Some("crate1::mod1".to_string()), + level: LevelFilter::Warn, + }, ]); assert!(enabled(&logger.directives, Level::Debug, "crate2::mod1")); assert!(!enabled(&logger.directives, Level::Debug, "crate2")); @@ -460,8 +485,14 @@ mod tests { #[test] fn match_default() { let logger = make_logger_filter(vec![ - Directive { name: None, level: LevelFilter::Info }, - Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn } + Directive { + name: None, + level: LevelFilter::Info, + }, + Directive { + name: Some("crate1::mod1".to_string()), + level: LevelFilter::Warn, + }, ]); assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1")); assert!(enabled(&logger.directives, Level::Info, "crate2::mod2")); @@ -470,8 +501,14 @@ mod tests { #[test] fn zero_level() { let logger = make_logger_filter(vec![ - Directive { name: None, level: LevelFilter::Info }, - Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Off } + Directive { + name: None, + level: LevelFilter::Info, + }, + Directive { + name: Some("crate1::mod1".to_string()), + level: LevelFilter::Off, + }, ]); assert!(!enabled(&logger.directives, Level::Error, "crate1::mod1")); assert!(enabled(&logger.directives, Level::Info, "crate2::mod2")); diff --git a/third_party/rust/env_logger/src/filter/regex.rs b/third_party/rust/env_logger/src/filter/regex.rs index a042654135e3..fb21528a1216 100644 --- a/third_party/rust/env_logger/src/filter/regex.rs +++ b/third_party/rust/env_logger/src/filter/regex.rs @@ -11,7 +11,7 @@ pub struct Filter { impl Filter { pub fn new(spec: &str) -> Result { - match Regex::new(spec){ + match Regex::new(spec) { Ok(r) => Ok(Filter { inner: r }), Err(e) => Err(e.to_string()), } diff --git a/third_party/rust/env_logger/src/filter/string.rs b/third_party/rust/env_logger/src/filter/string.rs index 96d7ecca1243..ea476e42f934 100644 --- a/third_party/rust/env_logger/src/filter/string.rs +++ b/third_party/rust/env_logger/src/filter/string.rs @@ -7,7 +7,9 @@ pub struct Filter { impl Filter { pub fn new(spec: &str) -> Result { - Ok(Filter { inner: spec.to_string() }) + Ok(Filter { + inner: spec.to_string(), + }) } pub fn is_match(&self, s: &str) -> bool { diff --git a/third_party/rust/env_logger/src/fmt/humantime/extern_impl.rs b/third_party/rust/env_logger/src/fmt/humantime/extern_impl.rs index 596a281959a5..19dec1b65d39 100644 --- a/third_party/rust/env_logger/src/fmt/humantime/extern_impl.rs +++ b/third_party/rust/env_logger/src/fmt/humantime/extern_impl.rs @@ -1,11 +1,13 @@ use std::fmt; use std::time::SystemTime; -use humantime::{format_rfc3339_nanos, format_rfc3339_seconds}; +use humantime::{ + format_rfc3339_micros, format_rfc3339_millis, format_rfc3339_nanos, format_rfc3339_seconds, +}; -use ::fmt::Formatter; +use crate::fmt::{Formatter, TimestampPrecision}; -pub(in ::fmt) mod glob { +pub(in crate::fmt) mod glob { pub use super::*; } @@ -30,12 +32,46 @@ impl Formatter { /// /// [`Timestamp`]: struct.Timestamp.html pub fn timestamp(&self) -> Timestamp { - Timestamp(SystemTime::now()) + Timestamp { + time: SystemTime::now(), + precision: TimestampPrecision::Seconds, + } } - /// Get a [`PreciseTimestamp`] for the current date and time in UTC with nanos. - pub fn precise_timestamp(&self) -> PreciseTimestamp { - PreciseTimestamp(SystemTime::now()) + /// Get a [`Timestamp`] for the current date and time in UTC with full + /// second precision. + pub fn timestamp_seconds(&self) -> Timestamp { + Timestamp { + time: SystemTime::now(), + precision: TimestampPrecision::Seconds, + } + } + + /// Get a [`Timestamp`] for the current date and time in UTC with + /// millisecond precision. + pub fn timestamp_millis(&self) -> Timestamp { + Timestamp { + time: SystemTime::now(), + precision: TimestampPrecision::Millis, + } + } + + /// Get a [`Timestamp`] for the current date and time in UTC with + /// microsecond precision. + pub fn timestamp_micros(&self) -> Timestamp { + Timestamp { + time: SystemTime::now(), + precision: TimestampPrecision::Micros, + } + } + + /// Get a [`Timestamp`] for the current date and time in UTC with + /// nanosecond precision. + pub fn timestamp_nanos(&self) -> Timestamp { + Timestamp { + time: SystemTime::now(), + precision: TimestampPrecision::Nanos, + } } } @@ -46,13 +82,10 @@ impl Formatter { /// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt /// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html /// [`Formatter`]: struct.Formatter.html -pub struct Timestamp(SystemTime); - -/// An [RFC3339] formatted timestamp with nanos. -/// -/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt -#[derive(Debug)] -pub struct PreciseTimestamp(SystemTime); +pub struct Timestamp { + time: SystemTime, + precision: TimestampPrecision, +} impl fmt::Debug for Timestamp { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -66,19 +99,20 @@ impl fmt::Debug for Timestamp { } f.debug_tuple("Timestamp") - .field(&TimestampValue(&self)) - .finish() + .field(&TimestampValue(&self)) + .finish() } } impl fmt::Display for Timestamp { - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { - format_rfc3339_seconds(self.0).fmt(f) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let formatter = match self.precision { + TimestampPrecision::Seconds => format_rfc3339_seconds, + TimestampPrecision::Millis => format_rfc3339_millis, + TimestampPrecision::Micros => format_rfc3339_micros, + TimestampPrecision::Nanos => format_rfc3339_nanos, + }; + + formatter(self.time).fmt(f) } } - -impl fmt::Display for PreciseTimestamp { - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { - format_rfc3339_nanos(self.0).fmt(f) - } -} \ No newline at end of file diff --git a/third_party/rust/env_logger/src/fmt/humantime/mod.rs b/third_party/rust/env_logger/src/fmt/humantime/mod.rs index c4f7599c9add..ac23ae2493f3 100644 --- a/third_party/rust/env_logger/src/fmt/humantime/mod.rs +++ b/third_party/rust/env_logger/src/fmt/humantime/mod.rs @@ -8,4 +8,4 @@ Its public API is available when the `humantime` crate is available. #[cfg_attr(not(feature = "humantime"), path = "shim_impl.rs")] mod imp; -pub(in ::fmt) use self::imp::*; +pub(in crate::fmt) use self::imp::*; diff --git a/third_party/rust/env_logger/src/fmt/humantime/shim_impl.rs b/third_party/rust/env_logger/src/fmt/humantime/shim_impl.rs index 0f7534000224..906bf9e4c1a6 100644 --- a/third_party/rust/env_logger/src/fmt/humantime/shim_impl.rs +++ b/third_party/rust/env_logger/src/fmt/humantime/shim_impl.rs @@ -2,6 +2,4 @@ Timestamps aren't available when we don't have a `humantime` dependency. */ -pub(in ::fmt) mod glob { - -} +pub(in crate::fmt) mod glob {} diff --git a/third_party/rust/env_logger/src/fmt/mod.rs b/third_party/rust/env_logger/src/fmt/mod.rs index 3f3d4975943f..e699e214d64b 100644 --- a/third_party/rust/env_logger/src/fmt/mod.rs +++ b/third_party/rust/env_logger/src/fmt/mod.rs @@ -29,24 +29,48 @@ //! [`Builder::format`]: ../struct.Builder.html#method.format //! [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html -use std::io::prelude::*; -use std::{io, fmt, mem}; -use std::rc::Rc; use std::cell::RefCell; use std::fmt::Display; +use std::io::prelude::*; +use std::rc::Rc; +use std::{fmt, io, mem}; use log::Record; -pub(crate) mod writer; mod humantime; +pub(crate) mod writer; pub use self::humantime::glob::*; pub use self::writer::glob::*; -use self::writer::{Writer, Buffer}; +use self::writer::{Buffer, Writer}; pub(crate) mod glob { - pub use super::{Target, WriteStyle}; + pub use super::{Target, TimestampPrecision, WriteStyle}; +} + +/// Formatting precision of timestamps. +/// +/// Seconds give precision of full seconds, milliseconds give thousands of a +/// second (3 decimal digits), microseconds are millionth of a second (6 decimal +/// digits) and nanoseconds are billionth of a second (9 decimal digits). +#[derive(Copy, Clone, Debug)] +pub enum TimestampPrecision { + /// Full second precision (0 decimal digits) + Seconds, + /// Millisecond precision (3 decimal digits) + Millis, + /// Microsecond precision (6 decimal digits) + Micros, + /// Nanosecond precision (9 decimal digits) + Nanos, +} + +/// The default timestamp precision is seconds. +impl Default for TimestampPrecision { + fn default() -> Self { + TimestampPrecision::Seconds + } } /// A formatter to write logs into. @@ -107,16 +131,16 @@ impl Write for Formatter { } impl fmt::Debug for Formatter { - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Formatter").finish() } } pub(crate) struct Builder { - pub default_format_timestamp: bool, - pub default_format_timestamp_nanos: bool, - pub default_format_module_path: bool, - pub default_format_level: bool, + pub format_timestamp: Option, + pub format_module_path: bool, + pub format_level: bool, + pub format_indent: Option, #[allow(unknown_lints, bare_trait_objects)] pub custom_format: Option io::Result<()> + Sync + Send>>, built: bool, @@ -125,10 +149,10 @@ pub(crate) struct Builder { impl Default for Builder { fn default() -> Self { Builder { - default_format_timestamp: true, - default_format_timestamp_nanos: false, - default_format_module_path: true, - default_format_level: true, + format_timestamp: Some(Default::default()), + format_module_path: true, + format_level: true, + format_indent: Some(4), custom_format: None, built: false, } @@ -137,7 +161,7 @@ impl Default for Builder { impl Builder { /// Convert the format into a callable function. - /// + /// /// If the `custom_format` is `Some`, then any `default_format` switches are ignored. /// If the `custom_format` is `None`, then a default format is returned. /// Any `default_format` switches set to `false` won't be written by the format. @@ -145,22 +169,24 @@ impl Builder { pub fn build(&mut self) -> Box io::Result<()> + Sync + Send> { assert!(!self.built, "attempt to re-use consumed builder"); - let built = mem::replace(self, Builder { - built: true, - ..Default::default() - }); + let built = mem::replace( + self, + Builder { + built: true, + ..Default::default() + }, + ); if let Some(fmt) = built.custom_format { fmt - } - else { + } else { Box::new(move |buf, record| { let fmt = DefaultFormat { - timestamp: built.default_format_timestamp, - timestamp_nanos: built.default_format_timestamp_nanos, - module_path: built.default_format_module_path, - level: built.default_format_level, + timestamp: built.format_timestamp, + module_path: built.format_module_path, + level: built.format_level, written_header_value: false, + indent: built.format_indent, buf, }; @@ -176,14 +202,14 @@ type SubtleStyle = StyledValue<'static, &'static str>; type SubtleStyle = &'static str; /// The default format. -/// +/// /// This format needs to work with any combination of crate features. struct DefaultFormat<'a> { - timestamp: bool, + timestamp: Option, module_path: bool, level: bool, - timestamp_nanos: bool, written_header_value: bool, + indent: Option, buf: &'a mut Formatter, } @@ -200,7 +226,8 @@ impl<'a> DefaultFormat<'a> { fn subtle_style(&self, text: &'static str) -> SubtleStyle { #[cfg(feature = "termcolor")] { - self.buf.style() + self.buf + .style() .set_color(Color::Black) .set_intense(true) .into_value(text) @@ -227,7 +254,7 @@ impl<'a> DefaultFormat<'a> { fn write_level(&mut self, record: &Record) -> io::Result<()> { if !self.level { - return Ok(()) + return Ok(()); } let level = { @@ -247,29 +274,29 @@ impl<'a> DefaultFormat<'a> { fn write_timestamp(&mut self) -> io::Result<()> { #[cfg(feature = "humantime")] { - if !self.timestamp { - return Ok(()) - } + use self::TimestampPrecision::*; + let ts = match self.timestamp { + None => return Ok(()), + Some(Seconds) => self.buf.timestamp_seconds(), + Some(Millis) => self.buf.timestamp_millis(), + Some(Micros) => self.buf.timestamp_micros(), + Some(Nanos) => self.buf.timestamp_nanos(), + }; - if self.timestamp_nanos { - let ts_nanos = self.buf.precise_timestamp(); - self.write_header_value(ts_nanos) - } else { - let ts = self.buf.timestamp(); - self.write_header_value(ts) - } + self.write_header_value(ts) } #[cfg(not(feature = "humantime"))] { + // Trick the compiler to think we have used self.timestamp + // Workaround for "field is never used: `timestamp`" compiler nag. let _ = self.timestamp; - let _ = self.timestamp_nanos; Ok(()) } } fn write_module_path(&mut self, record: &Record) -> io::Result<()> { if !self.module_path { - return Ok(()) + return Ok(()); } if let Some(module_path) = record.module_path() { @@ -289,7 +316,51 @@ impl<'a> DefaultFormat<'a> { } fn write_args(&mut self, record: &Record) -> io::Result<()> { - writeln!(self.buf, "{}", record.args()) + match self.indent { + // Fast path for no indentation + None => writeln!(self.buf, "{}", record.args()), + + Some(indent_count) => { + // Create a wrapper around the buffer only if we have to actually indent the message + + struct IndentWrapper<'a, 'b: 'a> { + fmt: &'a mut DefaultFormat<'b>, + indent_count: usize, + } + + impl<'a, 'b> Write for IndentWrapper<'a, 'b> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let mut first = true; + for chunk in buf.split(|&x| x == b'\n') { + if !first { + write!(self.fmt.buf, "\n{:width$}", "", width = self.indent_count)?; + } + self.fmt.buf.write_all(chunk)?; + first = false; + } + + Ok(buf.len()) + } + + fn flush(&mut self) -> io::Result<()> { + self.fmt.buf.flush() + } + } + + // The explicit scope here is just to make older versions of Rust happy + { + let mut wrapper = IndentWrapper { + fmt: self, + indent_count, + }; + write!(wrapper, "{}", record.args())?; + } + + writeln!(self.buf)?; + + Ok(()) + } + } } } @@ -303,7 +374,7 @@ mod tests { let buf = fmt.buf.buf.clone(); let record = Record::builder() - .args(format_args!("log message")) + .args(format_args!("log\nmessage")) .level(Level::Info) .file(Some("test.rs")) .line(Some(144)) @@ -317,7 +388,7 @@ mod tests { } #[test] - fn default_format_with_header() { + fn format_with_header() { let writer = writer::Builder::new() .write_style(WriteStyle::Never) .build(); @@ -325,19 +396,19 @@ mod tests { let mut f = Formatter::new(&writer); let written = write(DefaultFormat { - timestamp: false, - timestamp_nanos: false, + timestamp: None, module_path: true, level: true, written_header_value: false, + indent: None, buf: &mut f, }); - assert_eq!("[INFO test::path] log message\n", written); + assert_eq!("[INFO test::path] log\nmessage\n", written); } #[test] - fn default_format_no_header() { + fn format_no_header() { let writer = writer::Builder::new() .write_style(WriteStyle::Never) .build(); @@ -345,14 +416,74 @@ mod tests { let mut f = Formatter::new(&writer); let written = write(DefaultFormat { - timestamp: false, - timestamp_nanos: false, + timestamp: None, module_path: false, level: false, written_header_value: false, + indent: None, buf: &mut f, }); - assert_eq!("log message\n", written); + assert_eq!("log\nmessage\n", written); + } + + #[test] + fn format_indent_spaces() { + let writer = writer::Builder::new() + .write_style(WriteStyle::Never) + .build(); + + let mut f = Formatter::new(&writer); + + let written = write(DefaultFormat { + timestamp: None, + module_path: true, + level: true, + written_header_value: false, + indent: Some(4), + buf: &mut f, + }); + + assert_eq!("[INFO test::path] log\n message\n", written); + } + + #[test] + fn format_indent_zero_spaces() { + let writer = writer::Builder::new() + .write_style(WriteStyle::Never) + .build(); + + let mut f = Formatter::new(&writer); + + let written = write(DefaultFormat { + timestamp: None, + module_path: true, + level: true, + written_header_value: false, + indent: Some(0), + buf: &mut f, + }); + + assert_eq!("[INFO test::path] log\nmessage\n", written); + } + + #[test] + fn format_indent_spaces_no_header() { + let writer = writer::Builder::new() + .write_style(WriteStyle::Never) + .build(); + + let mut f = Formatter::new(&writer); + + let written = write(DefaultFormat { + timestamp: None, + module_path: false, + level: false, + written_header_value: false, + indent: Some(4), + buf: &mut f, + }); + + assert_eq!("log\n message\n", written); } } diff --git a/third_party/rust/env_logger/src/fmt/writer/atty.rs b/third_party/rust/env_logger/src/fmt/writer/atty.rs index c441cf0895ad..f6718413f0d6 100644 --- a/third_party/rust/env_logger/src/fmt/writer/atty.rs +++ b/third_party/rust/env_logger/src/fmt/writer/atty.rs @@ -11,24 +11,24 @@ from being printed. mod imp { use atty; - pub(in ::fmt) fn is_stdout() -> bool { + pub(in crate::fmt) fn is_stdout() -> bool { atty::is(atty::Stream::Stdout) } - pub(in ::fmt) fn is_stderr() -> bool { + pub(in crate::fmt) fn is_stderr() -> bool { atty::is(atty::Stream::Stderr) } } #[cfg(not(feature = "atty"))] mod imp { - pub(in ::fmt) fn is_stdout() -> bool { + pub(in crate::fmt) fn is_stdout() -> bool { false } - pub(in ::fmt) fn is_stderr() -> bool { + pub(in crate::fmt) fn is_stderr() -> bool { false } } -pub(in ::fmt) use self::imp::*; +pub(in crate::fmt) use self::imp::*; diff --git a/third_party/rust/env_logger/src/fmt/writer/mod.rs b/third_party/rust/env_logger/src/fmt/writer/mod.rs index d84e4146776a..6ee63a39811c 100644 --- a/third_party/rust/env_logger/src/fmt/writer/mod.rs +++ b/third_party/rust/env_logger/src/fmt/writer/mod.rs @@ -1,16 +1,16 @@ -mod termcolor; mod atty; +mod termcolor; -use std::{fmt, io}; +use self::atty::{is_stderr, is_stdout}; use self::termcolor::BufferWriter; -use self::atty::{is_stdout, is_stderr}; +use std::{fmt, io}; -pub(in ::fmt) mod glob { +pub(in crate::fmt) mod glob { pub use super::termcolor::glob::*; pub use super::*; } -pub(in ::fmt) use self::termcolor::Buffer; +pub(in crate::fmt) use self::termcolor::Buffer; /// Log target, either `stdout` or `stderr`. #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -55,11 +55,11 @@ impl Writer { self.write_style } - pub(in ::fmt) fn buffer(&self) -> Buffer { + pub(in crate::fmt) fn buffer(&self) -> Buffer { self.inner.buffer() } - pub(in ::fmt) fn print(&self, buf: &Buffer) -> io::Result<()> { + pub(in crate::fmt) fn print(&self, buf: &Buffer) -> io::Result<()> { self.inner.print(buf) } } @@ -127,7 +127,7 @@ impl Builder { } else { WriteStyle::Never } - }, + } color_choice => color_choice, }; @@ -150,16 +150,16 @@ impl Default for Builder { } impl fmt::Debug for Builder { - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Logger") - .field("target", &self.target) - .field("write_style", &self.write_style) - .finish() + .field("target", &self.target) + .field("write_style", &self.write_style) + .finish() } } impl fmt::Debug for Writer { - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Writer").finish() } } @@ -192,12 +192,7 @@ mod tests { #[test] fn parse_write_style_invalid() { - let inputs = vec![ - "", - "true", - "false", - "NEVER!!" - ]; + let inputs = vec!["", "true", "false", "NEVER!!"]; for input in inputs { assert_eq!(WriteStyle::Auto, parse_write_style(input)); diff --git a/third_party/rust/env_logger/src/fmt/writer/termcolor/extern_impl.rs b/third_party/rust/env_logger/src/fmt/writer/termcolor/extern_impl.rs index 0c2d138792c1..2d38e375aaf4 100644 --- a/third_party/rust/env_logger/src/fmt/writer/termcolor/extern_impl.rs +++ b/third_party/rust/env_logger/src/fmt/writer/termcolor/extern_impl.rs @@ -1,16 +1,15 @@ use std::borrow::Cow; +use std::cell::RefCell; use std::fmt; use std::io::{self, Write}; -use std::cell::RefCell; use std::rc::Rc; use log::Level; use termcolor::{self, ColorChoice, ColorSpec, WriteColor}; -use ::WriteStyle; -use ::fmt::{Formatter, Target}; +use crate::fmt::{Formatter, Target, WriteStyle}; -pub(in ::fmt::writer) mod glob { +pub(in crate::fmt::writer) mod glob { pub use super::*; } @@ -47,7 +46,7 @@ impl Formatter { } /// Get the default [`Style`] for the given level. - /// + /// /// The style can be used to print other values besides the level. pub fn default_level_style(&self, level: Level) -> Style { let mut level_style = self.style(); @@ -62,54 +61,46 @@ impl Formatter { } /// Get a printable [`Style`] for the given level. - /// + /// /// The style can only be used to print the level. pub fn default_styled_level(&self, level: Level) -> StyledValue<'static, Level> { self.default_level_style(level).into_value(level) } } -pub(in ::fmt::writer) struct BufferWriter { +pub(in crate::fmt::writer) struct BufferWriter { inner: termcolor::BufferWriter, test_target: Option, } -pub(in ::fmt) struct Buffer { +pub(in crate::fmt) struct Buffer { inner: termcolor::Buffer, test_target: Option, } impl BufferWriter { - pub(in ::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self { + pub(in crate::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self { BufferWriter { inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()), - test_target: if is_test { - Some(Target::Stderr) - } else { - None - }, + test_target: if is_test { Some(Target::Stderr) } else { None }, } } - pub(in ::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self { + pub(in crate::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self { BufferWriter { inner: termcolor::BufferWriter::stdout(write_style.into_color_choice()), - test_target: if is_test { - Some(Target::Stdout) - } else { - None - }, + test_target: if is_test { Some(Target::Stdout) } else { None }, } } - pub(in ::fmt::writer) fn buffer(&self) -> Buffer { + pub(in crate::fmt::writer) fn buffer(&self) -> Buffer { Buffer { inner: self.inner.buffer(), test_target: self.test_target, } } - pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { + pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { if let Some(target) = self.test_target { // This impl uses the `eprint` and `print` macros // instead of `termcolor`'s buffer. @@ -129,19 +120,19 @@ impl BufferWriter { } impl Buffer { - pub(in ::fmt) fn clear(&mut self) { + pub(in crate::fmt) fn clear(&mut self) { self.inner.clear() } - pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result { + pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result { self.inner.write(buf) } - pub(in ::fmt) fn flush(&mut self) -> io::Result<()> { + pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> { self.inner.flush() } - pub(in ::fmt) fn bytes(&self) -> &[u8] { + pub(in crate::fmt) fn bytes(&self) -> &[u8] { self.inner.as_slice() } @@ -374,7 +365,7 @@ impl Style { pub fn value(&self, value: T) -> StyledValue { StyledValue { style: Cow::Borrowed(self), - value + value, } } @@ -382,7 +373,7 @@ impl Style { pub(crate) fn into_value(&mut self, value: T) -> StyledValue<'static, T> { StyledValue { style: Cow::Owned(self.clone()), - value + value, } } } @@ -392,7 +383,11 @@ impl<'a, T> StyledValue<'a, T> { where F: FnOnce() -> fmt::Result, { - self.style.buf.borrow_mut().set_color(&self.style.spec).map_err(|_| fmt::Error)?; + self.style + .buf + .borrow_mut() + .set_color(&self.style.spec) + .map_err(|_| fmt::Error)?; // Always try to reset the terminal style, even if writing failed let write = f(); @@ -403,7 +398,7 @@ impl<'a, T> StyledValue<'a, T> { } impl fmt::Debug for Style { - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Style").field("spec", &self.spec).finish() } } @@ -429,7 +424,8 @@ impl_styled_value_fmt!( fmt::UpperHex, fmt::LowerHex, fmt::UpperExp, - fmt::LowerExp); + fmt::LowerExp +); // The `Color` type is copied from https://github.com/BurntSushi/ripgrep/tree/master/termcolor diff --git a/third_party/rust/env_logger/src/fmt/writer/termcolor/mod.rs b/third_party/rust/env_logger/src/fmt/writer/termcolor/mod.rs index df0f78591b8a..f3e6768cd051 100644 --- a/third_party/rust/env_logger/src/fmt/writer/termcolor/mod.rs +++ b/third_party/rust/env_logger/src/fmt/writer/termcolor/mod.rs @@ -9,4 +9,4 @@ The terminal printing is shimmed when the `termcolor` crate is not available. #[cfg_attr(not(feature = "termcolor"), path = "shim_impl.rs")] mod imp; -pub(in ::fmt) use self::imp::*; +pub(in crate::fmt) use self::imp::*; diff --git a/third_party/rust/env_logger/src/fmt/writer/termcolor/shim_impl.rs b/third_party/rust/env_logger/src/fmt/writer/termcolor/shim_impl.rs index fb4735902d01..563f8ad4ffd7 100644 --- a/third_party/rust/env_logger/src/fmt/writer/termcolor/shim_impl.rs +++ b/third_party/rust/env_logger/src/fmt/writer/termcolor/shim_impl.rs @@ -1,35 +1,33 @@ use std::io; -use fmt::{WriteStyle, Target}; +use crate::fmt::{Target, WriteStyle}; -pub(in ::fmt::writer) mod glob { - -} +pub(in crate::fmt::writer) mod glob {} -pub(in ::fmt::writer) struct BufferWriter { +pub(in crate::fmt::writer) struct BufferWriter { target: Target, } -pub(in ::fmt) struct Buffer(Vec); +pub(in crate::fmt) struct Buffer(Vec); impl BufferWriter { - pub(in ::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self { + pub(in crate::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self { BufferWriter { target: Target::Stderr, } } - pub(in ::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self { + pub(in crate::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self { BufferWriter { target: Target::Stdout, } } - pub(in ::fmt::writer) fn buffer(&self) -> Buffer { + pub(in crate::fmt::writer) fn buffer(&self) -> Buffer { Buffer(Vec::new()) } - pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { + pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { // This impl uses the `eprint` and `print` macros // instead of using the streams directly. // This is so their output can be captured by `cargo test` @@ -45,21 +43,21 @@ impl BufferWriter { } impl Buffer { - pub(in ::fmt) fn clear(&mut self) { + pub(in crate::fmt) fn clear(&mut self) { self.0.clear(); } - pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result { + pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result { self.0.extend(buf); Ok(buf.len()) } - pub(in ::fmt) fn flush(&mut self) -> io::Result<()> { + pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> { Ok(()) } #[cfg(test)] - pub(in ::fmt) fn bytes(&self) -> &[u8] { + pub(in crate::fmt) fn bytes(&self) -> &[u8] { &self.0 } -} \ No newline at end of file +} diff --git a/third_party/rust/env_logger/src/lib.rs b/third_party/rust/env_logger/src/lib.rs index 37c43229144d..415183600b26 100644 --- a/third_party/rust/env_logger/src/lib.rs +++ b/third_party/rust/env_logger/src/lib.rs @@ -16,7 +16,6 @@ //! //! ``` //! #[macro_use] extern crate log; -//! extern crate env_logger; //! //! use log::Level; //! @@ -139,33 +138,32 @@ //! * `error,hello=warn/[0-9]scopes` turn on global error logging and also //! warn for hello. In both cases the log message must include a single digit //! number followed by 'scopes'. -//! +//! //! ## Capturing logs in tests -//! +//! //! Records logged during `cargo test` will not be captured by the test harness by default. //! The [`Builder::is_test`] method can be used in unit tests to ensure logs will be captured: -//! +//! //! ``` //! # #[macro_use] extern crate log; -//! # extern crate env_logger; //! # fn main() {} //! #[cfg(test)] //! mod tests { //! fn init() { //! let _ = env_logger::builder().is_test(true).try_init(); //! } -//! +//! //! #[test] //! fn it_works() { //! init(); -//! +//! //! info!("This record will be captured by `cargo test`"); -//! +//! //! assert_eq!(2, 1 + 1); //! } //! } //! ``` -//! +//! //! Enabling test capturing comes at the expense of color and other style support //! and may have performance implications. //! @@ -179,32 +177,32 @@ //! * `always` will always print style characters even if they aren't supported by the terminal. //! This includes emitting ANSI colors on Windows if the console API is unavailable. //! * `never` will never print style characters. -//! +//! //! ## Tweaking the default format -//! +//! //! Parts of the default format can be excluded from the log output using the [`Builder`]. //! The following example excludes the timestamp from the log output: -//! +//! //! ``` //! env_logger::builder() -//! .default_format_timestamp(false) +//! .format_timestamp(None) //! .init(); //! ``` -//! +//! //! ### Stability of the default format -//! -//! The default format won't optimise for long-term stability, and explicitly makes no -//! guarantees about the stability of its output across major, minor or patch version +//! +//! The default format won't optimise for long-term stability, and explicitly makes no +//! guarantees about the stability of its output across major, minor or patch version //! bumps during `0.x`. -//! -//! If you want to capture or interpret the output of `env_logger` programmatically +//! +//! If you want to capture or interpret the output of `env_logger` programmatically //! then you should use a custom format. -//! +//! //! ### Using a custom format -//! +//! //! Custom formats can be provided as closures to the [`Builder`]. //! These closures take a [`Formatter`] and `log::Record` as arguments: -//! +//! //! ``` //! use std::io::Write; //! @@ -214,54 +212,43 @@ //! }) //! .init(); //! ``` -//! +//! //! See the [`fmt`] module for more details about custom formats. -//! +//! //! ## Specifying defaults for environment variables -//! +//! //! `env_logger` can read configuration from environment variables. //! If these variables aren't present, the default value to use can be tweaked with the [`Env`] type. //! The following example defaults to log `warn` and above if the `RUST_LOG` environment variable //! isn't set: -//! +//! //! ``` //! use env_logger::Env; //! //! env_logger::from_env(Env::default().default_filter_or("warn")).init(); //! ``` -//! +//! //! [log-crate-url]: https://docs.rs/log/ //! [`Builder`]: struct.Builder.html //! [`Builder::is_test`]: struct.Builder.html#method.is_test //! [`Env`]: struct.Env.html //! [`fmt`]: fmt/index.html -#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "https://www.rust-lang.org/static/images/favicon.ico", - html_root_url = "https://docs.rs/env_logger/0.6.2")] +#![doc( + html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", + html_favicon_url = "https://www.rust-lang.org/static/images/favicon.ico", + html_root_url = "https://docs.rs/env_logger/0.7.1" +)] #![cfg_attr(test, deny(warnings))] - // When compiled for the rustc compiler itself we want to make sure that this is // an unstable crate #![cfg_attr(rustbuild, feature(staged_api, rustc_private))] #![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))] - #![deny(missing_debug_implementations, missing_docs, warnings)] -extern crate log; +use std::{borrow::Cow, cell::RefCell, env, io}; -#[cfg(feature = "termcolor")] -extern crate termcolor; -#[cfg(feature = "humantime")] -extern crate humantime; -#[cfg(feature = "atty")] -extern crate atty; - -use std::{env, io}; -use std::borrow::Cow; -use std::cell::RefCell; - -use log::{Log, LevelFilter, Record, SetLoggerError, Metadata}; +use log::{LevelFilter, Log, Metadata, Record, SetLoggerError}; pub mod filter; pub mod fmt; @@ -269,8 +256,8 @@ pub mod fmt; pub use self::fmt::glob::*; use self::filter::Filter; -use self::fmt::Formatter; use self::fmt::writer::{self, Writer}; +use self::fmt::Formatter; /// The default name for the environment variable to read filters from. pub const DEFAULT_FILTER_ENV: &'static str = "RUST_LOG"; @@ -334,9 +321,7 @@ pub struct Logger { /// # Examples /// /// ``` -/// #[macro_use] -/// extern crate log; -/// extern crate env_logger; +/// #[macro_use] extern crate log; /// /// use std::env; /// use std::io::Write; @@ -364,30 +349,28 @@ pub struct Builder { impl Builder { /// Initializes the log builder with defaults. - /// + /// /// **NOTE:** This method won't read from any environment variables. /// Use the [`filter`] and [`write_style`] methods to configure the builder /// or use [`from_env`] or [`from_default_env`] instead. - /// + /// /// # Examples - /// + /// /// Create a new builder and configure filters and style: - /// + /// /// ``` - /// # extern crate log; - /// # extern crate env_logger; /// # fn main() { /// use log::LevelFilter; /// use env_logger::{Builder, WriteStyle}; - /// + /// /// let mut builder = Builder::new(); - /// + /// /// builder.filter(None, LevelFilter::Info) /// .write_style(WriteStyle::Always) /// .init(); /// # } /// ``` - /// + /// /// [`filter`]: #method.filter /// [`write_style`]: #method.write_style /// [`from_env`]: #method.from_env @@ -402,13 +385,13 @@ impl Builder { /// passing in. /// /// # Examples - /// + /// /// Initialise a logger reading the log filter from an environment variable /// called `MY_LOG`: - /// + /// /// ``` /// use env_logger::Builder; - /// + /// /// let mut builder = Builder::from_env("MY_LOG"); /// builder.init(); /// ``` @@ -426,7 +409,7 @@ impl Builder { /// ``` pub fn from_env<'a, E>(env: E) -> Self where - E: Into> + E: Into>, { let mut builder = Builder::new(); let env = env.into(); @@ -443,18 +426,18 @@ impl Builder { } /// Initializes the log builder from the environment using default variable names. - /// + /// /// This method is a convenient way to call `from_env(Env::default())` without /// having to use the `Env` type explicitly. The builder will use the /// [default environment variables]. - /// + /// /// # Examples - /// + /// /// Initialise a logger using the default environment variables: - /// + /// /// ``` /// use env_logger::Builder; - /// + /// /// let mut builder = Builder::from_default_env(); /// builder.init(); /// ``` @@ -473,17 +456,17 @@ impl Builder { /// `Formatter` so that implementations can use the [`std::fmt`] macros /// to format and output without intermediate heap allocations. The default /// `env_logger` formatter takes advantage of this. - /// + /// /// # Examples - /// + /// /// Use a custom format to write only the log message: - /// + /// /// ``` /// use std::io::Write; /// use env_logger::Builder; - /// + /// /// let mut builder = Builder::new(); - /// + /// /// builder.format(|buf, record| writeln!(buf, "{}", record.args())); /// ``` /// @@ -491,44 +474,66 @@ impl Builder { /// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html pub fn format(&mut self, format: F) -> &mut Self - where F: Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send + where + F: Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send, { self.format.custom_format = Some(Box::new(format)); self } /// Use the default format. - /// + /// /// This method will clear any custom format set on the builder. pub fn default_format(&mut self) -> &mut Self { - self.format.custom_format = None; + self.format = Default::default(); self } /// Whether or not to write the level in the default format. - pub fn default_format_level(&mut self, write: bool) -> &mut Self { - self.format.default_format_level = write; + pub fn format_level(&mut self, write: bool) -> &mut Self { + self.format.format_level = write; self } /// Whether or not to write the module path in the default format. - pub fn default_format_module_path(&mut self, write: bool) -> &mut Self { - self.format.default_format_module_path = write; + pub fn format_module_path(&mut self, write: bool) -> &mut Self { + self.format.format_module_path = write; self } - /// Whether or not to write the timestamp in the default format. - pub fn default_format_timestamp(&mut self, write: bool) -> &mut Self { - self.format.default_format_timestamp = write; + /// Configures the amount of spaces to use to indent multiline log records. + /// A value of `None` disables any kind of indentation. + pub fn format_indent(&mut self, indent: Option) -> &mut Self { + self.format.format_indent = indent; self } - /// Whether or not to write the timestamp with nanos. - pub fn default_format_timestamp_nanos(&mut self, write: bool) -> &mut Self { - self.format.default_format_timestamp_nanos = write; + /// Configures if timestamp should be included and in what precision. + pub fn format_timestamp(&mut self, timestamp: Option) -> &mut Self { + self.format.format_timestamp = timestamp; self } + /// Configures the timestamp to use second precision. + pub fn format_timestamp_secs(&mut self) -> &mut Self { + self.format_timestamp(Some(fmt::TimestampPrecision::Seconds)) + } + + /// Configures the timestamp to use millisecond precision. + pub fn format_timestamp_millis(&mut self) -> &mut Self { + self.format_timestamp(Some(fmt::TimestampPrecision::Millis)) + } + + /// Configures the timestamp to use microsecond precision. + pub fn format_timestamp_micros(&mut self) -> &mut Self { + self.format_timestamp(Some(fmt::TimestampPrecision::Micros)) + } + + /// Configures the timestamp to use nanosecond precision. + pub fn format_timestamp_nanos(&mut self) -> &mut Self { + self.format_timestamp(Some(fmt::TimestampPrecision::Nanos)) + } + /// Adds a directive to the filter for a specific module. /// /// # Examples @@ -536,8 +541,6 @@ impl Builder { /// Only include messages for warning and above for logs in `path::to::module`: /// /// ``` - /// # extern crate log; - /// # extern crate env_logger; /// # fn main() { /// use log::LevelFilter; /// use env_logger::Builder; @@ -559,8 +562,6 @@ impl Builder { /// Only include messages for warning and above for logs in `path::to::module`: /// /// ``` - /// # extern crate log; - /// # extern crate env_logger; /// # fn main() { /// use log::LevelFilter; /// use env_logger::Builder; @@ -579,39 +580,26 @@ impl Builder { /// /// The given module (if any) will log at most the specified level provided. /// If no module is provided then the filter will apply to all log messages. - /// + /// /// # Examples - /// + /// /// Only include messages for warning and above for logs in `path::to::module`: - /// + /// /// ``` - /// # extern crate log; - /// # extern crate env_logger; /// # fn main() { /// use log::LevelFilter; /// use env_logger::Builder; - /// + /// /// let mut builder = Builder::new(); - /// + /// /// builder.filter(Some("path::to::module"), LevelFilter::Info); /// # } /// ``` - pub fn filter(&mut self, - module: Option<&str>, - level: LevelFilter) -> &mut Self { + pub fn filter(&mut self, module: Option<&str>, level: LevelFilter) -> &mut Self { self.filter.filter(module, level); self } - /// Parses the directives string in the same form as the `RUST_LOG` - /// environment variable. - /// - /// See the module documentation for more details. - #[deprecated(since = "0.6.1", note = "use `parse_filters` instead.")] - pub fn parse(&mut self, filters: &str) -> &mut Self { - self.parse_filters(filters) - } - /// Parses the directives string in the same form as the `RUST_LOG` /// environment variable. /// @@ -624,16 +612,16 @@ impl Builder { /// Sets the target for the log output. /// /// Env logger can log to either stdout or stderr. The default is stderr. - /// + /// /// # Examples - /// + /// /// Write log message to `stdout`: - /// + /// /// ``` /// use env_logger::{Builder, Target}; - /// + /// /// let mut builder = Builder::new(); - /// + /// /// builder.target(Target::Stdout); /// ``` pub fn target(&mut self, target: fmt::Target) -> &mut Self { @@ -645,16 +633,16 @@ impl Builder { /// /// This can be useful in environments that don't support control characters /// for setting colors. - /// + /// /// # Examples - /// + /// /// Never attempt to write styles: - /// + /// /// ``` /// use env_logger::{Builder, WriteStyle}; - /// + /// /// let mut builder = Builder::new(); - /// + /// /// builder.write_style(WriteStyle::Never); /// ``` pub fn write_style(&mut self, write_style: fmt::WriteStyle) -> &mut Self { @@ -672,7 +660,7 @@ impl Builder { } /// Sets whether or not the logger will be used in unit tests. - /// + /// /// If `is_test` is `true` then the logger will allow the testing framework to /// capture log records rather than printing them to the terminal directly. pub fn is_test(&mut self, is_test: bool) -> &mut Self { @@ -712,7 +700,8 @@ impl Builder { /// This function will panic if it is called more than once, or if another /// library has already initialized a global logger. pub fn init(&mut self) { - self.try_init().expect("Builder::init should not be called after logger initialized"); + self.try_init() + .expect("Builder::init should not be called after logger initialized"); } /// Build an env logger. @@ -759,8 +748,8 @@ impl Logger { /// let logger = Logger::from_env(env); /// ``` pub fn from_env<'a, E>(env: E) -> Self - where - E: Into> + where + E: Into>, { Builder::from_env(env).build() } @@ -818,40 +807,51 @@ impl Log for Logger { static FORMATTER: RefCell> = RefCell::new(None); } - FORMATTER.with(|tl_buf| { - // It's possible for implementations to sometimes - // log-while-logging (e.g. a `std::fmt` implementation logs - // internally) but it's super rare. If this happens make sure we - // at least don't panic and ship some output to the screen. - let mut a; - let mut b = None; - let tl_buf = match tl_buf.try_borrow_mut() { - Ok(f) => { - a = f; - &mut *a - } - Err(_) => &mut b, - }; - - // Check the buffer style. If it's different from the logger's - // style then drop the buffer and recreate it. - match *tl_buf { - Some(ref mut formatter) => { - if formatter.write_style() != self.writer.write_style() { - *formatter = Formatter::new(&self.writer) - } - }, - ref mut tl_buf => *tl_buf = Some(Formatter::new(&self.writer)) - } - - // The format is guaranteed to be `Some` by this point - let mut formatter = tl_buf.as_mut().unwrap(); - - let _ = (self.format)(&mut formatter, record).and_then(|_| formatter.print(&self.writer)); + let print = |formatter: &mut Formatter, record: &Record| { + let _ = + (self.format)(formatter, record).and_then(|_| formatter.print(&self.writer)); // Always clear the buffer afterwards formatter.clear(); - }); + }; + + let printed = FORMATTER + .try_with(|tl_buf| { + match tl_buf.try_borrow_mut() { + // There are no active borrows of the buffer + Ok(mut tl_buf) => match *tl_buf { + // We have a previously set formatter + Some(ref mut formatter) => { + // Check the buffer style. If it's different from the logger's + // style then drop the buffer and recreate it. + if formatter.write_style() != self.writer.write_style() { + *formatter = Formatter::new(&self.writer); + } + + print(formatter, record); + } + // We don't have a previously set formatter + None => { + let mut formatter = Formatter::new(&self.writer); + print(&mut formatter, record); + + *tl_buf = Some(formatter); + } + }, + // There's already an active borrow of the buffer (due to re-entrancy) + Err(_) => { + print(&mut Formatter::new(&self.writer), record); + } + } + }) + .is_ok(); + + if !printed { + // The thread-local storage was not available (because its + // destructor has already run). Create a new single-use + // Formatter on the stack for this call. + print(&mut Formatter::new(&self.writer), record); + } } } @@ -867,7 +867,7 @@ impl<'a> Env<'a> { /// Specify an environment variable to read the filter from. pub fn filter(mut self, filter_env: E) -> Self where - E: Into> + E: Into>, { self.filter = Var::new(filter_env); @@ -888,7 +888,7 @@ impl<'a> Env<'a> { } /// Use the default environment variable to read the filter from. - /// + /// /// If the variable is not set, the default value will be used. pub fn default_filter_or(mut self, default: V) -> Self where @@ -906,7 +906,7 @@ impl<'a> Env<'a> { /// Specify an environment variable to read the style from. pub fn write_style(mut self, write_style_env: E) -> Self where - E: Into> + E: Into>, { self.write_style = Var::new(write_style_env); @@ -917,9 +917,9 @@ impl<'a> Env<'a> { /// /// If the variable is not set, the default value will be used. pub fn write_style_or(mut self, write_style_env: E, default: V) -> Self - where - E: Into>, - V: Into>, + where + E: Into>, + V: Into>, { self.write_style = Var::new_with_default(write_style_env, default); @@ -930,8 +930,8 @@ impl<'a> Env<'a> { /// /// If the variable is not set, the default value will be used. pub fn default_write_style_or(mut self, default: V) -> Self - where - V: Into>, + where + V: Into>, { self.write_style = Var::new_with_default(DEFAULT_WRITE_STYLE_ENV, default); @@ -945,8 +945,8 @@ impl<'a> Env<'a> { impl<'a> Var<'a> { fn new(name: E) -> Self - where - E: Into>, + where + E: Into>, { Var { name: name.into(), @@ -968,15 +968,13 @@ impl<'a> Var<'a> { fn get(&self) -> Option { env::var(&*self.name) .ok() - .or_else(|| self.default - .to_owned() - .map(|v| v.into_owned())) + .or_else(|| self.default.to_owned().map(|v| v.into_owned())) } } impl<'a, T> From for Env<'a> where - T: Into> + T: Into>, { fn from(filter_env: T) -> Self { Env::default().filter(filter_env.into()) @@ -993,28 +991,26 @@ impl<'a> Default for Env<'a> { } mod std_fmt_impls { - use std::fmt; use super::*; + use std::fmt; - impl fmt::Debug for Logger{ - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + impl fmt::Debug for Logger { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Logger") .field("filter", &self.filter) .finish() } } - impl fmt::Debug for Builder{ - fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result { + impl fmt::Debug for Builder { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.built { - f.debug_struct("Logger") - .field("built", &true) - .finish() + f.debug_struct("Logger").field("built", &true).finish() } else { f.debug_struct("Logger") - .field("filter", &self.filter) - .field("writer", &self.writer) - .finish() + .field("filter", &self.filter) + .field("writer", &self.writer) + .finish() } } } @@ -1077,7 +1073,7 @@ pub fn init() { /// library has already initialized a global logger. pub fn try_init_from_env<'a, E>(env: E) -> Result<(), SetLoggerError> where - E: Into> + E: Into>, { let mut builder = Builder::from_env(env); @@ -1109,24 +1105,25 @@ where /// library has already initialized a global logger. pub fn init_from_env<'a, E>(env: E) where - E: Into> + E: Into>, { - try_init_from_env(env).expect("env_logger::init_from_env should not be called after logger initialized"); + try_init_from_env(env) + .expect("env_logger::init_from_env should not be called after logger initialized"); } /// Create a new builder with the default environment variables. -/// +/// /// The builder can be configured before being initialized. pub fn builder() -> Builder { Builder::from_default_env() } /// Create a builder from the given environment variables. -/// +/// /// The builder can be configured before being initialized. pub fn from_env<'a, E>(env: E) -> Builder where - E: Into> + E: Into>, { Builder::from_env(env) } @@ -1148,7 +1145,10 @@ mod tests { fn env_get_filter_reads_from_default_if_var_not_set() { env::remove_var("env_get_filter_reads_from_default_if_var_not_set"); - let env = Env::new().filter_or("env_get_filter_reads_from_default_if_var_not_set", "from default"); + let env = Env::new().filter_or( + "env_get_filter_reads_from_default_if_var_not_set", + "from default", + ); assert_eq!(Some("from default".to_owned()), env.get_filter()); } @@ -1157,7 +1157,8 @@ mod tests { fn env_get_write_style_reads_from_var_if_set() { env::set_var("env_get_write_style_reads_from_var_if_set", "from var"); - let env = Env::new().write_style_or("env_get_write_style_reads_from_var_if_set", "from default"); + let env = + Env::new().write_style_or("env_get_write_style_reads_from_var_if_set", "from default"); assert_eq!(Some("from var".to_owned()), env.get_write_style()); } @@ -1166,7 +1167,10 @@ mod tests { fn env_get_write_style_reads_from_default_if_var_not_set() { env::remove_var("env_get_write_style_reads_from_default_if_var_not_set"); - let env = Env::new().write_style_or("env_get_write_style_reads_from_default_if_var_not_set", "from default"); + let env = Env::new().write_style_or( + "env_get_write_style_reads_from_default_if_var_not_set", + "from default", + ); assert_eq!(Some("from default".to_owned()), env.get_write_style()); } diff --git a/third_party/rust/env_logger/tests/init-twice-retains-filter.rs b/third_party/rust/env_logger/tests/init-twice-retains-filter.rs index c1256ef6d14a..673da3fd2879 100644 --- a/third_party/rust/env_logger/tests/init-twice-retains-filter.rs +++ b/third_party/rust/env_logger/tests/init-twice-retains-filter.rs @@ -1,8 +1,8 @@ -extern crate log; extern crate env_logger; +extern crate log; -use std::process; use std::env; +use std::process; use std::str; fn main() { @@ -20,7 +20,7 @@ fn main() { .unwrap_err(); assert_eq!(log::LevelFilter::Debug, log::max_level()); - return + return; } let exe = env::current_exe().unwrap(); @@ -30,7 +30,7 @@ fn main() { .output() .unwrap_or_else(|e| panic!("Unable to start child process: {}", e)); if out.status.success() { - return + return; } println!("test failed: {}", out.status); diff --git a/third_party/rust/env_logger/tests/log-in-log.rs b/third_party/rust/env_logger/tests/log-in-log.rs index 6b2c47e7a6f0..89517ff36236 100644 --- a/third_party/rust/env_logger/tests/log-in-log.rs +++ b/third_party/rust/env_logger/tests/log-in-log.rs @@ -1,9 +1,10 @@ -#[macro_use] extern crate log; +#[macro_use] +extern crate log; extern crate env_logger; -use std::process; -use std::fmt; use std::env; +use std::fmt; +use std::process; use std::str; struct Foo; @@ -28,7 +29,7 @@ fn main() { .output() .unwrap_or_else(|e| panic!("Unable to start child process: {}", e)); if out.status.success() { - return + return; } println!("test failed: {}", out.status); diff --git a/third_party/rust/env_logger/tests/log_tls_dtors.rs b/third_party/rust/env_logger/tests/log_tls_dtors.rs new file mode 100644 index 000000000000..5db87bd6c59a --- /dev/null +++ b/third_party/rust/env_logger/tests/log_tls_dtors.rs @@ -0,0 +1,66 @@ +#[macro_use] +extern crate log; +extern crate env_logger; + +use std::env; +use std::process; +use std::str; +use std::thread; + +struct DropMe; + +impl Drop for DropMe { + fn drop(&mut self) { + debug!("Dropping now"); + } +} + +fn run() { + // Use multiple thread local values to increase the chance that our TLS + // value will get destroyed after the FORMATTER key in the library + thread_local! { + static DROP_ME_0: DropMe = DropMe; + static DROP_ME_1: DropMe = DropMe; + static DROP_ME_2: DropMe = DropMe; + static DROP_ME_3: DropMe = DropMe; + static DROP_ME_4: DropMe = DropMe; + static DROP_ME_5: DropMe = DropMe; + static DROP_ME_6: DropMe = DropMe; + static DROP_ME_7: DropMe = DropMe; + static DROP_ME_8: DropMe = DropMe; + static DROP_ME_9: DropMe = DropMe; + } + DROP_ME_0.with(|_| {}); + DROP_ME_1.with(|_| {}); + DROP_ME_2.with(|_| {}); + DROP_ME_3.with(|_| {}); + DROP_ME_4.with(|_| {}); + DROP_ME_5.with(|_| {}); + DROP_ME_6.with(|_| {}); + DROP_ME_7.with(|_| {}); + DROP_ME_8.with(|_| {}); + DROP_ME_9.with(|_| {}); +} + +fn main() { + env_logger::init(); + if env::var("YOU_ARE_TESTING_NOW").is_ok() { + // Run on a separate thread because TLS values on the main thread + // won't have their destructors run if pthread is used. + // https://doc.rust-lang.org/std/thread/struct.LocalKey.html#platform-specific-behavior + thread::spawn(run).join().unwrap(); + } else { + let exe = env::current_exe().unwrap(); + let out = process::Command::new(exe) + .env("YOU_ARE_TESTING_NOW", "1") + .env("RUST_LOG", "debug") + .output() + .unwrap_or_else(|e| panic!("Unable to start child process: {}", e)); + if !out.status.success() { + println!("test failed: {}", out.status); + println!("--- stdout\n{}", str::from_utf8(&out.stdout).unwrap()); + println!("--- stderr\n{}", str::from_utf8(&out.stderr).unwrap()); + process::exit(1); + } + } +} diff --git a/third_party/rust/env_logger/tests/regexp_filter.rs b/third_party/rust/env_logger/tests/regexp_filter.rs index d23e9223eabd..40178bac7fdc 100644 --- a/third_party/rust/env_logger/tests/regexp_filter.rs +++ b/third_party/rust/env_logger/tests/regexp_filter.rs @@ -1,8 +1,9 @@ -#[macro_use] extern crate log; +#[macro_use] +extern crate log; extern crate env_logger; -use std::process; use std::env; +use std::process; use std::str; fn main() { @@ -25,7 +26,9 @@ fn run_child(rust_log: String) -> bool { .env("RUST_LOG", rust_log) .output() .unwrap_or_else(|e| panic!("Unable to start child process: {}", e)); - str::from_utf8(out.stderr.as_ref()).unwrap().contains("XYZ Message") + str::from_utf8(out.stderr.as_ref()) + .unwrap() + .contains("XYZ Message") } fn assert_message_printed(rust_log: &str) { @@ -36,7 +39,10 @@ fn assert_message_printed(rust_log: &str) { fn assert_message_not_printed(rust_log: &str) { if run_child(rust_log.to_string()) { - panic!("RUST_LOG={} should not allow the test log message", rust_log) + panic!( + "RUST_LOG={} should not allow the test log message", + rust_log + ) } } diff --git a/third_party/rust/neqo-common/.cargo-checksum.json b/third_party/rust/neqo-common/.cargo-checksum.json index 5ac70c08f418..3b18b7ef6c35 100644 --- a/third_party/rust/neqo-common/.cargo-checksum.json +++ b/third_party/rust/neqo-common/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"0fd12ce59d9614688687bd711a7a2899c3c299a3f58078e4e75e6c12a0cb5235","src/codec.rs":"6ec44d7fbc4c2a4db39da4530e3a463e1608256528d5c68fc59ea90752b2d94a","src/datagram.rs":"569f8d9e34d7ee17144bf63d34136ecd9778da0d337e513f338738c50284615e","src/incrdecoder.rs":"f65afa390317ab2a306e8cd7e08f33490dc824242f9be87b2a16b136715f8094","src/lib.rs":"611fc76c0650225ebe15fa2fd56fcaa77dbf0690b50f2c09bee0ea2dc6678572","src/log.rs":"b8da388073f72a21128d52b0d0c963e07a3d3cf3368438ae3a50be34b8add3a4","src/qlog.rs":"87822ebdec0b7ca8a652f45b8d30919f20a814009b0470b498accee0212ec605","src/timer.rs":"706333bf1b07f65df9d18904b1cb269e4b80dee93a9b239dd8cb128b293955ae","tests/log.rs":"480b165b7907ec642c508b303d63005eee1427115d6973a349eaf6b2242ed18d"},"package":null} \ No newline at end of file +{"files":{"Cargo.toml":"0058144aec5e2ce4061a71640d537e4ab6314b7896dcd6f8061c4273e20c9070","src/codec.rs":"6c0f0138967cc927555241ff5b1266deffcf58b9b1d6d09c633a8d5294d148f2","src/datagram.rs":"569f8d9e34d7ee17144bf63d34136ecd9778da0d337e513f338738c50284615e","src/incrdecoder.rs":"f65afa390317ab2a306e8cd7e08f33490dc824242f9be87b2a16b136715f8094","src/lib.rs":"611fc76c0650225ebe15fa2fd56fcaa77dbf0690b50f2c09bee0ea2dc6678572","src/log.rs":"b8da388073f72a21128d52b0d0c963e07a3d3cf3368438ae3a50be34b8add3a4","src/qlog.rs":"17448c854ea0ced8da08fabb4f08aeaf5126b94f44bd8a7aee6be5f1bdb2cf9d","src/timer.rs":"66886b3697e1b4232d9d9892a12d93afd3381812a8ff901bceac4bb48b264607","tests/log.rs":"480b165b7907ec642c508b303d63005eee1427115d6973a349eaf6b2242ed18d"},"package":null} \ No newline at end of file diff --git a/third_party/rust/neqo-common/Cargo.toml b/third_party/rust/neqo-common/Cargo.toml index 4ff1e1c7027e..2912c7df9dc4 100644 --- a/third_party/rust/neqo-common/Cargo.toml +++ b/third_party/rust/neqo-common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "neqo-common" -version = "0.4.8" +version = "0.4.9" authors = ["Bobby Holley "] edition = "2018" license = "MIT/Apache-2.0" diff --git a/third_party/rust/neqo-common/src/codec.rs b/third_party/rust/neqo-common/src/codec.rs index 3b56425eaf37..74de0e1a62e3 100644 --- a/third_party/rust/neqo-common/src/codec.rs +++ b/third_party/rust/neqo-common/src/codec.rs @@ -108,7 +108,7 @@ impl<'a> Decoder<'a> { pub fn decode_varint(&mut self) -> Option { let b1 = match self.decode_byte() { Some(b) => b, - _ => return None, + None => return None, }; match b1 >> 6 { 0 => Some(u64::from(b1 & 0x3f)), @@ -129,7 +129,7 @@ impl<'a> Decoder<'a> { fn decode_checked(&mut self, n: Option) -> Option<&'a [u8]> { let len = match n { Some(l) => l, - _ => return None, + None => return None, }; if let Ok(l) = usize::try_from(len) { self.decode(l) @@ -176,6 +176,16 @@ impl<'a> From<&'a [u8]> for Decoder<'a> { } } +impl<'a, T> From<&'a T> for Decoder<'a> +where + T: AsRef<[u8]>, +{ + #[must_use] + fn from(buf: &'a T) -> Decoder<'a> { + Decoder::new(buf.as_ref()) + } +} + impl<'a, 'b> PartialEq> for Decoder<'a> { #[must_use] fn eq(&self, other: &Decoder<'b>) -> bool { @@ -225,7 +235,8 @@ impl Encoder { /// Don't use this except in testing. #[must_use] - pub fn from_hex(s: &str) -> Self { + pub fn from_hex(s: impl AsRef) -> Self { + let s = s.as_ref(); if s.len() % 2 != 0 { panic!("Needs to be even length"); } @@ -425,7 +436,7 @@ mod tests { assert_eq!(dec.decode_remainder(), &[0x01, 0x23, 0x45]); assert!(dec.decode(2).is_none()); - let mut dec = Decoder::from(&enc[0..0]); + let mut dec = Decoder::from(&[]); assert_eq!(dec.decode_remainder().len(), 0); } diff --git a/third_party/rust/neqo-common/src/qlog.rs b/third_party/rust/neqo-common/src/qlog.rs index 8165c65f023b..e7db2e2db287 100644 --- a/third_party/rust/neqo-common/src/qlog.rs +++ b/third_party/rust/neqo-common/src/qlog.rs @@ -95,6 +95,12 @@ impl NeqoQlog { } } +impl Default for NeqoQlog { + fn default() -> Self { + Self::disabled() + } +} + impl fmt::Debug for NeqoQlogShared { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "NeqoQlog writing to {}", self.qlog_path.display()) diff --git a/third_party/rust/neqo-common/src/timer.rs b/third_party/rust/neqo-common/src/timer.rs index 8994e5379a54..4ddaf94fa66b 100644 --- a/third_party/rust/neqo-common/src/timer.rs +++ b/third_party/rust/neqo-common/src/timer.rs @@ -89,7 +89,10 @@ impl Timer { } /// Slide forward in time by `n * self.granularity`. - #[allow(clippy::cast_possible_truncation)] // guarded by assertion + #[allow(clippy::unknown_clippy_lints)] // Until we require rust 1.45. + #[allow(clippy::cast_possible_truncation, clippy::reversed_empty_ranges)] + // cast_possible_truncation is ok because we have an assertion guard. + // reversed_empty_ranges is to avoid different types on the if/else. fn tick(&mut self, n: usize) { let new = self.bucket(n); let iter = if new < self.cursor { @@ -147,7 +150,7 @@ impl Timer { let bucket = self.time_bucket(time); let start_index = match self.items[bucket].binary_search_by_key(&time, TimerItem::time) { Ok(idx) => idx, - _ => return None, + Err(_) => return None, }; // start_index is just one of potentially many items with the same time. // Search backwards for a match, ... diff --git a/third_party/rust/neqo-crypto/.cargo-checksum.json b/third_party/rust/neqo-crypto/.cargo-checksum.json index 2f58c9f5ad6f..b8ed0643b407 100644 --- a/third_party/rust/neqo-crypto/.cargo-checksum.json +++ b/third_party/rust/neqo-crypto/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"e1e3edec4a8e6e90f1fcda51f6112a469a5a68f15fdc2e07defa07ea36fe7901","TODO":"ac0f1c2ebcca03f5b3c0cc56c5aedbb030a4b511e438bc07a57361c789f91e9f","bindings/bindings.toml":"0ae7922bb20f2b8cf54b307cd642303e65b00cfbc3e681877e2a7a86f7b22530","bindings/mozpkix.hpp":"77072c8bb0f6eb6bfe8cbadc111dcd92e0c79936d13f2e501aae1e5d289a6675","bindings/nspr_err.h":"2d5205d017b536c2d838bcf9bc4ec79f96dd50e7bb9b73892328781f1ee6629d","bindings/nspr_error.h":"e41c03c77b8c22046f8618832c9569fbcc7b26d8b9bbc35eea7168f35e346889","bindings/nspr_io.h":"085b289849ef0e77f88512a27b4d9bdc28252bd4d39c6a17303204e46ef45f72","bindings/nspr_time.h":"2e637fd338a5cf0fd3fb0070a47f474a34c2a7f4447f31b6875f5a9928d0a261","bindings/nss_ciphers.h":"95ec6344a607558b3c5ba8510f463b6295f3a2fb3f538a01410531045a5f62d1","bindings/nss_init.h":"ef49045063782fb612aff459172cc6a89340f15005808608ade5320ca9974310","bindings/nss_p11.h":"0b81e64fe6db49b2ecff94edd850be111ef99ec11220e88ceb1c67be90143a78","bindings/nss_secerr.h":"713e8368bdae5159af7893cfa517dabfe5103cede051dee9c9557c850a2defc6","bindings/nss_ssl.h":"af222fb957b989e392e762fa2125c82608a0053aff4fb97e556691646c88c335","bindings/nss_sslerr.h":"24b97f092183d8486f774cdaef5030d0249221c78343570d83a4ee5b594210ae","bindings/nss_sslopt.h":"b7807eb7abdad14db6ad7bc51048a46b065a0ea65a4508c95a12ce90e59d1eea","build.rs":"0be611e6b25a18d5ed724071a3a471c2c5b584649b5fe36de28a56ed8caaf800","src/aead.rs":"e26ad34f7168f42aa87eb3a6455af3191a0e8555782b1d70032fe1d248922f98","src/agent.rs":"d653735397becd2832b4d15f699cfb6be44c6066ef8a99404fa0139e98bf093c","src/agentio.rs":"cc562d09a09719b90b4e1d147fd579e3e89b683448709e920033bceaea108a61","src/auth.rs":"71ac7e297a5f872d26cf67b6bbd96e4548ea38374bdd84c1094f76a5de4ed1cb","src/cert.rs":"fd3fd2bbb38754bdcee3898549feae412943c9f719032531c1ad6e61783b5394","src/constants.rs":"c39ee506a10d685fda77c1d2ddf691b595b067b4e1044ac7a21e360119d6002b","src/err.rs":"04f38831ca62d29d8aadfe9daf95fd29e68ece184e6d3e00bfb9ee1d12744033","src/exp.rs":"61586662407359c1ecb8ed4987bc3c702f26ba2e203a091a51b6d6363cbd510f","src/ext.rs":"97cba23247e5f9656f27587214f7d7370a69174bae5960a012ce3e6fc99f9116","src/hkdf.rs":"40e44f4280497ef525c2b4c465f14f06d241150851668b264ee958f74321cfbe","src/hp.rs":"7fce64e0cc3a6a7e744bc797886bcfaa39679f0a81853b2e55ea0f54fb6bf700","src/lib.rs":"f66cd7a1c949eb47dfa33189d5e28ebbe653c812a93e9cd96583f82b0495707f","src/once.rs":"b9850384899a1a016e839743d3489c0d4d916e1973746ef8c89872105d7d9736","src/p11.rs":"0b62ee5938aefb82e8faee5aa14e990a00442cc9744e8ba22eda80b32030c42c","src/prio.rs":"bc4e97049563b136cb7b39f5171e7909d56a77ed46690aaacb781eeb4a4743e0","src/replay.rs":"40924865994396441a68e6009ecbdf352d6a02fdf539aa65604124e26bffb4d3","src/result.rs":"cef34dfcb907723e195b56501132e4560e250b327783cb5e41201da5b63e9b5c","src/secrets.rs":"acb5befa74e06281c6f80d7298efc58f568bb4e6d949b4225c335e3f392be741","src/selfencrypt.rs":"429cb889a4e9e2345888cc033115c0aa306d2ff90bdfe22b3067700eb1426c37","src/ssl.rs":"d64c20ed2a0b63c5fa3aee674a622408a89a764ee225098f18d0c61ce6c6df29","src/time.rs":"5b2ab4028b04b6245c666f33f1c1449816d3d1eb8141f723f5773f21f8fe4388","tests/aead.rs":"a1d8eb69f5672e064f84dce3d214b347a396718e3de56d57ccc108ee87f1cbc1","tests/agent.rs":"2312590910dc3cba4c446c0dee844773779d8a3870cf543fcc863821fcee50dd","tests/ext.rs":"5f5de777599cbe1295a4461b32c249de74666edb0a13173f76948f2939963dfd","tests/handshake.rs":"6f12fb9a02d36f64254ffe49385de69fce8bc95b73af80be011f0e065d65a5a3","tests/hkdf.rs":"539235e9dcf2a56b72961a9a04f0080409adf6bf465bfad7c30026421b2d4326","tests/hp.rs":"e52a7d2f4387f2dfe8bfe1da5867e8e0d3eb51e171c6904e18b18c4343536af8","tests/init.rs":"20aad800ac793aaf83059cf860593750509fdedeeff0c08a648e7a5cb398dae0","tests/selfencrypt.rs":"46e9a1a09c2ae577eb106d23a5cdacf762575c0dea1948aedab06ef7389ce713"},"package":null} \ No newline at end of file +{"files":{"Cargo.toml":"ec995e040bbf7cb16fa8220bcec8932ccab13bb4e22e575c13d71c0bee7b06d5","TODO":"ac0f1c2ebcca03f5b3c0cc56c5aedbb030a4b511e438bc07a57361c789f91e9f","bindings/bindings.toml":"0ae7922bb20f2b8cf54b307cd642303e65b00cfbc3e681877e2a7a86f7b22530","bindings/mozpkix.hpp":"77072c8bb0f6eb6bfe8cbadc111dcd92e0c79936d13f2e501aae1e5d289a6675","bindings/nspr_err.h":"2d5205d017b536c2d838bcf9bc4ec79f96dd50e7bb9b73892328781f1ee6629d","bindings/nspr_error.h":"e41c03c77b8c22046f8618832c9569fbcc7b26d8b9bbc35eea7168f35e346889","bindings/nspr_io.h":"085b289849ef0e77f88512a27b4d9bdc28252bd4d39c6a17303204e46ef45f72","bindings/nspr_time.h":"2e637fd338a5cf0fd3fb0070a47f474a34c2a7f4447f31b6875f5a9928d0a261","bindings/nss_ciphers.h":"95ec6344a607558b3c5ba8510f463b6295f3a2fb3f538a01410531045a5f62d1","bindings/nss_init.h":"ef49045063782fb612aff459172cc6a89340f15005808608ade5320ca9974310","bindings/nss_p11.h":"0b81e64fe6db49b2ecff94edd850be111ef99ec11220e88ceb1c67be90143a78","bindings/nss_secerr.h":"713e8368bdae5159af7893cfa517dabfe5103cede051dee9c9557c850a2defc6","bindings/nss_ssl.h":"af222fb957b989e392e762fa2125c82608a0053aff4fb97e556691646c88c335","bindings/nss_sslerr.h":"24b97f092183d8486f774cdaef5030d0249221c78343570d83a4ee5b594210ae","bindings/nss_sslopt.h":"b7807eb7abdad14db6ad7bc51048a46b065a0ea65a4508c95a12ce90e59d1eea","build.rs":"0be611e6b25a18d5ed724071a3a471c2c5b584649b5fe36de28a56ed8caaf800","src/aead.rs":"e26ad34f7168f42aa87eb3a6455af3191a0e8555782b1d70032fe1d248922f98","src/agent.rs":"0cf454a0d272dacb31a0e49e9a97c880d1f18c16b9c158b1772ce34c4f15963a","src/agentio.rs":"cc562d09a09719b90b4e1d147fd579e3e89b683448709e920033bceaea108a61","src/auth.rs":"71ac7e297a5f872d26cf67b6bbd96e4548ea38374bdd84c1094f76a5de4ed1cb","src/cert.rs":"fd3fd2bbb38754bdcee3898549feae412943c9f719032531c1ad6e61783b5394","src/constants.rs":"c39ee506a10d685fda77c1d2ddf691b595b067b4e1044ac7a21e360119d6002b","src/err.rs":"04f38831ca62d29d8aadfe9daf95fd29e68ece184e6d3e00bfb9ee1d12744033","src/exp.rs":"61586662407359c1ecb8ed4987bc3c702f26ba2e203a091a51b6d6363cbd510f","src/ext.rs":"97cba23247e5f9656f27587214f7d7370a69174bae5960a012ce3e6fc99f9116","src/hkdf.rs":"40e44f4280497ef525c2b4c465f14f06d241150851668b264ee958f74321cfbe","src/hp.rs":"7fce64e0cc3a6a7e744bc797886bcfaa39679f0a81853b2e55ea0f54fb6bf700","src/lib.rs":"f66cd7a1c949eb47dfa33189d5e28ebbe653c812a93e9cd96583f82b0495707f","src/once.rs":"b9850384899a1a016e839743d3489c0d4d916e1973746ef8c89872105d7d9736","src/p11.rs":"0b62ee5938aefb82e8faee5aa14e990a00442cc9744e8ba22eda80b32030c42c","src/prio.rs":"bc4e97049563b136cb7b39f5171e7909d56a77ed46690aaacb781eeb4a4743e0","src/replay.rs":"40924865994396441a68e6009ecbdf352d6a02fdf539aa65604124e26bffb4d3","src/result.rs":"cef34dfcb907723e195b56501132e4560e250b327783cb5e41201da5b63e9b5c","src/secrets.rs":"acb5befa74e06281c6f80d7298efc58f568bb4e6d949b4225c335e3f392be741","src/selfencrypt.rs":"429cb889a4e9e2345888cc033115c0aa306d2ff90bdfe22b3067700eb1426c37","src/ssl.rs":"d64c20ed2a0b63c5fa3aee674a622408a89a764ee225098f18d0c61ce6c6df29","src/time.rs":"5b2ab4028b04b6245c666f33f1c1449816d3d1eb8141f723f5773f21f8fe4388","tests/aead.rs":"a1d8eb69f5672e064f84dce3d214b347a396718e3de56d57ccc108ee87f1cbc1","tests/agent.rs":"2312590910dc3cba4c446c0dee844773779d8a3870cf543fcc863821fcee50dd","tests/ext.rs":"eba9f03accdd598e38292ac88263a81b367d60d5a736a43117a3663de105ec48","tests/handshake.rs":"6f12fb9a02d36f64254ffe49385de69fce8bc95b73af80be011f0e065d65a5a3","tests/hkdf.rs":"539235e9dcf2a56b72961a9a04f0080409adf6bf465bfad7c30026421b2d4326","tests/hp.rs":"e52a7d2f4387f2dfe8bfe1da5867e8e0d3eb51e171c6904e18b18c4343536af8","tests/init.rs":"20aad800ac793aaf83059cf860593750509fdedeeff0c08a648e7a5cb398dae0","tests/selfencrypt.rs":"46e9a1a09c2ae577eb106d23a5cdacf762575c0dea1948aedab06ef7389ce713"},"package":null} \ No newline at end of file diff --git a/third_party/rust/neqo-crypto/Cargo.toml b/third_party/rust/neqo-crypto/Cargo.toml index e4d006e9ade5..cc747dae46ee 100644 --- a/third_party/rust/neqo-crypto/Cargo.toml +++ b/third_party/rust/neqo-crypto/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "neqo-crypto" -version = "0.4.8" +version = "0.4.9" authors = ["Martin Thomson "] edition = "2018" build = "build.rs" @@ -11,7 +11,7 @@ neqo-common = { path = "../neqo-common" } log = {version = "0.4.0", default-features = false} [build-dependencies] -bindgen = {version = "0.53.2", default-features = false} +bindgen = {version = "0.53.2", features= ["runtime"]} serde = "1.0" serde_derive = "1.0" toml = "0.4" diff --git a/third_party/rust/neqo-crypto/src/agent.rs b/third_party/rust/neqo-crypto/src/agent.rs index fb433bd1d619..e75e5fdcedf3 100644 --- a/third_party/rust/neqo-crypto/src/agent.rs +++ b/third_party/rust/neqo-crypto/src/agent.rs @@ -76,7 +76,7 @@ fn get_alpn(fd: *mut ssl::PRFileDesc, pre: bool) -> Res> { chosen.truncate(usize::try_from(chosen_len)?); Some(match String::from_utf8(chosen) { Ok(a) => a, - _ => return Err(Error::InternalError), + Err(_) => return Err(Error::InternalError), }) } _ => None, @@ -780,7 +780,7 @@ impl DerefMut for Client { /// `ZeroRttCheckResult` encapsulates the options for handling a `ClientHello`. #[derive(Clone, Debug, PartialEq)] pub enum ZeroRttCheckResult { - /// Accept 0-RTT; the default. + /// Accept 0-RTT. Accept, /// Reject 0-RTT, but continue the handshake normally. Reject, diff --git a/third_party/rust/neqo-crypto/tests/ext.rs b/third_party/rust/neqo-crypto/tests/ext.rs index c4b6ba96314c..b814c190a489 100644 --- a/third_party/rust/neqo-crypto/tests/ext.rs +++ b/third_party/rust/neqo-crypto/tests/ext.rs @@ -80,14 +80,14 @@ fn simple_extension() { let mut server = Server::new(&["key"]).expect("should create server"); let client_handler = Rc::new(RefCell::new(SimpleExtensionHandler::default())); - let ch2 = Rc::clone(&client_handler); + let ch = Rc::clone(&client_handler); client - .extension_handler(0xffff, ch2) + .extension_handler(0xffff, ch) .expect("client handler installed"); let server_handler = Rc::new(RefCell::new(SimpleExtensionHandler::default())); - let sh2 = Rc::clone(&server_handler); + let sh = Rc::clone(&server_handler); server - .extension_handler(0xffff, sh2) + .extension_handler(0xffff, sh) .expect("server handler installed"); connect(&mut client, &mut server); diff --git a/third_party/rust/neqo-http3/.cargo-checksum.json b/third_party/rust/neqo-http3/.cargo-checksum.json index 5f33b97a7778..1a6e10ddffb2 100644 --- a/third_party/rust/neqo-http3/.cargo-checksum.json +++ b/third_party/rust/neqo-http3/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"e1eaeb4dd4d2edac1c2afc74f39596e01257d9cb3566e6abdba3a6252efe0d7d","src/client_events.rs":"e912179817153f8e4be39d3db429a0b36c07c3619ec5a12786a719237860fcf1","src/connection.rs":"3ced80d0f32da5b742bc517fa273a56db70269fbaa3b72d8e136edf866c9dc62","src/connection_client.rs":"221ce0ac40530c64e204be37c0a99e4d1d39d1ce95faa007001d6b155d4fdf2f","src/connection_server.rs":"5d32fe8280e451c9853741d3910b1e0aa71220d35ba9cd672919fe74e621de1f","src/control_stream_local.rs":"03d6259599543da2154388d5e48efbc06271e079429d3d946278c4c58c0521c7","src/control_stream_remote.rs":"1dfac4956a7d6971e2cef2c83963d838e73aa3bf3286b7bde97099978c41d527","src/hframe.rs":"5b7349c8f6d18416d4159f0f8da46b5019e82b1eead7e95e151c2d0e1d4cd959","src/lib.rs":"48c632a5171671e5a13f40d81315096a16677d2a4ba6e0b287b50526f28f044a","src/push_controller.rs":"1bda78b2d649489d6a0256121046a1b2bf41f2a5bf779f5c28441531ab811966","src/push_stream.rs":"dad3691b919d1daba0b39489dd00d975a7c25526cda4b8a46d8d7808c543cca7","src/qlog.rs":"29c0e3c4c9571eb7fe905967edeb1c4bc236b1e35a0e0f11a4a847f1d246681d","src/recv_message.rs":"764f59a150ec12f44327687a45bfddb25453574b1ee8a400621cf2e626e4d84b","src/send_message.rs":"7121543a5774b0a590fbfa69d0fab6f1aea6fb61e36314782a63c568ab3b0b83","src/server.rs":"2fbb040e42a168b839cbba5a28b29595c7d0e06faaf94816f05ed856ba7cbf51","src/server_connection_events.rs":"0c2b8831ce9d254a15a3af24d155a119ca1d4a29dd6d287114bf0607efe93076","src/server_events.rs":"27f23dc49f649fb66113c5a71345d9af30e7de04f791d4e1928d32c66b47d3f1","src/settings.rs":"7f0729b0b342132ac3d6dfffc8006732ef15262c071a1629f8883227a289a457","src/stream_type_reader.rs":"aacb2e865f79b3ac55a887fd670f2286d8ffef94f7d8b3ecfa7e0cccbfa9ec04","tests/httpconn.rs":"57ef3a309ca3e9ac768c51ee4c98d16511b855ce4133760bf088e4ac0b967084"},"package":null} \ No newline at end of file +{"files":{"Cargo.toml":"79d2df171ec6ed54088a9ef58ab6af5f4ad6c7d17549e76991451f170a127dc1","src/client_events.rs":"e912179817153f8e4be39d3db429a0b36c07c3619ec5a12786a719237860fcf1","src/connection.rs":"3e203b343f36bb54299425939838003310b8bf13e7d1aca37d2d18779345214c","src/connection_client.rs":"f48a0051cc5b0027189646a17ebcf27c64f467835f3fb1479badb17ba8e37346","src/connection_server.rs":"5d32fe8280e451c9853741d3910b1e0aa71220d35ba9cd672919fe74e621de1f","src/control_stream_local.rs":"03d6259599543da2154388d5e48efbc06271e079429d3d946278c4c58c0521c7","src/control_stream_remote.rs":"1dfac4956a7d6971e2cef2c83963d838e73aa3bf3286b7bde97099978c41d527","src/hframe.rs":"5b7349c8f6d18416d4159f0f8da46b5019e82b1eead7e95e151c2d0e1d4cd959","src/lib.rs":"b29d3140536fcf26cbf0c00d597b30ece4cffbba0fadc3616fabb0968453297c","src/push_controller.rs":"744372679db12ab179e10cf196397d8f0c2f4085cd627e8fbde07889f918638c","src/push_stream.rs":"5044e2d5a8a7aa0711901342f532a952be407327f844613afdae4f8a5e14d21a","src/qlog.rs":"29c0e3c4c9571eb7fe905967edeb1c4bc236b1e35a0e0f11a4a847f1d246681d","src/recv_message.rs":"ee1f20d6b96079c557f1d675b8f88d274eaa9b73626a9f2da152e0a0c091e3dd","src/send_message.rs":"7e3f5280eca9b007b3b189134ce6832d3996f10405d396790acbced06a72af81","src/server.rs":"29af820be014adc338c67284f4df0584757a7ff14d7202e0b9a7a5384ab7b4ed","src/server_connection_events.rs":"0c2b8831ce9d254a15a3af24d155a119ca1d4a29dd6d287114bf0607efe93076","src/server_events.rs":"27f23dc49f649fb66113c5a71345d9af30e7de04f791d4e1928d32c66b47d3f1","src/settings.rs":"92fb232a11d1f37f22dee99c1fa6e0becf287fc6c6996ce890e30b2a6e37cf56","src/stream_type_reader.rs":"aacb2e865f79b3ac55a887fd670f2286d8ffef94f7d8b3ecfa7e0cccbfa9ec04","tests/httpconn.rs":"57ef3a309ca3e9ac768c51ee4c98d16511b855ce4133760bf088e4ac0b967084"},"package":null} \ No newline at end of file diff --git a/third_party/rust/neqo-http3/Cargo.toml b/third_party/rust/neqo-http3/Cargo.toml index 436d60dff9e5..b89fe1b8b830 100644 --- a/third_party/rust/neqo-http3/Cargo.toml +++ b/third_party/rust/neqo-http3/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "neqo-http3" -version = "0.4.8" +version = "0.4.9" authors = ["Dragana Damjanovic "] edition = "2018" license = "MIT/Apache-2.0" diff --git a/third_party/rust/neqo-http3/src/connection.rs b/third_party/rust/neqo-http3/src/connection.rs index 882ad2add1e3..00fd79002a76 100644 --- a/third_party/rust/neqo-http3/src/connection.rs +++ b/third_party/rust/neqo-http3/src/connection.rs @@ -353,7 +353,7 @@ impl Http3Connection { ); if let Some(s) = self.recv_streams.remove(&stream_id) { - s.stream_reset(app_error); + s.stream_reset_recv(app_error, &mut self.qpack_decoder); Ok(()) } else if self.is_critical_stream(stream_id) { Err(Error::HttpClosedCriticalStream) @@ -527,9 +527,11 @@ impl Http3Connection { ) -> Res<()> { qinfo!([self], "Reset stream {} error={}.", stream_id, error); - // We want to execute both statements, therefore we use | instead of ||. - let found = self.send_streams.remove(&stream_id).is_some() - | self.recv_streams.remove(&stream_id).is_some(); + let mut found = self.send_streams.remove(&stream_id).is_some(); + if let Some(s) = self.recv_streams.remove(&stream_id) { + s.stream_reset(&mut self.qpack_decoder); + found = true; + } // Stream maybe already be closed and we may get an error here, but we do not care. let _ = conn.stream_reset_send(stream_id, error); @@ -588,7 +590,7 @@ impl Http3Connection { HSettingType::BlockedStreams => { self.qpack_encoder.set_max_blocked_streams(s.value)? } - _ => {} + HSettingType::MaxHeaderListSize => (), } } Ok(()) diff --git a/third_party/rust/neqo-http3/src/connection_client.rs b/third_party/rust/neqo-http3/src/connection_client.rs index cf2b952da38a..2af73862db7d 100644 --- a/third_party/rust/neqo-http3/src/connection_client.rs +++ b/third_party/rust/neqo-http3/src/connection_client.rs @@ -20,8 +20,8 @@ use neqo_common::{ use neqo_crypto::{agent::CertificateInfo, AuthenticationStatus, SecretAgentInfo}; use neqo_qpack::{stats::Stats, QpackSettings}; use neqo_transport::{ - AppError, Connection, ConnectionEvent, ConnectionIdManager, Output, QuicVersion, StreamId, - StreamType, ZeroRttState, + AppError, Connection, ConnectionEvent, ConnectionId, ConnectionIdManager, Output, QuicVersion, + StreamId, StreamType, ZeroRttState, }; use std::cell::RefCell; use std::fmt::Display; @@ -144,6 +144,13 @@ impl Http3Client { self.conn.set_qlog(qlog); } + /// Get the connection id, which is useful for disambiguating connections to + /// the same origin. + #[must_use] + pub fn connection_id(&self) -> &ConnectionId { + &self.conn.odcid().expect("Client always has odcid") + } + /// Returns a resumption token if present. /// A resumption token encodes transport and settings parameter as well. #[must_use] @@ -172,7 +179,7 @@ impl Http3Client { let mut dec = Decoder::from(token); let settings_slice = match dec.decode_vvec() { Some(v) => v, - _ => return Err(Error::InvalidResumptionToken), + None => return Err(Error::InvalidResumptionToken), }; qtrace!([self], " settings {}", hex_with_len(&settings_slice)); let mut dec_settings = Decoder::from(settings_slice); @@ -697,6 +704,7 @@ mod tests { CloseError, ConnectionEvent, FixedConnectionIdManager, QuicVersion, State, RECV_BUFFER_SIZE, SEND_BUFFER_SIZE, }; + use std::convert::TryFrom; use test_fixture::{ default_server, fixture_init, loopback, now, DEFAULT_ALPN, DEFAULT_SERVER_NAME, }; @@ -836,6 +844,9 @@ mod tests { .unwrap(), 1 ); + self.encoder + .add_recv_stream(CLIENT_SIDE_DECODER_STREAM_ID) + .unwrap(); } pub fn create_control_stream(&mut self) { @@ -3089,9 +3100,7 @@ mod tests { fn test_read_frames_header_blocked() { let (mut client, mut server, request_stream_id) = connect_and_send_request(true); - server.encoder.set_max_capacity(100).unwrap(); - server.encoder.set_max_blocked_streams(100).unwrap(); - server.encoder.send(&mut server.conn).unwrap(); + setup_server_side_encoder(&mut client, &mut server); let headers = vec![ (String::from(":status"), String::from("200")), @@ -3127,11 +3136,11 @@ mod tests { assert!(!client.events().any(header_ready_event)); // Let client receive the encoder instructions. - let _out = client.process(encoder_inst_pkt.dgram(), now()); + let _ = client.process(encoder_inst_pkt.dgram(), now()); let out = server.conn.process(None, now()); - let _out = client.process(out.dgram(), now()); - let _out = client.process(None, now()); + let _ = client.process(out.dgram(), now()); + let _ = client.process(None, now()); let mut recv_header = false; let mut recv_data = false; @@ -3159,9 +3168,7 @@ mod tests { fn test_read_frames_header_blocked_with_fin_after_headers() { let (mut hconn, mut server, request_stream_id) = connect_and_send_request(true); - server.encoder.set_max_capacity(100).unwrap(); - server.encoder.set_max_blocked_streams(100).unwrap(); - server.encoder.send(&mut server.conn).unwrap(); + setup_server_side_encoder(&mut hconn, &mut server); let sent_headers = vec![ (String::from(":status"), String::from("200")), @@ -3266,7 +3273,7 @@ mod tests { let out = client.process(out.dgram(), now()); assert_eq!(client.state(), Http3State::Connected); - let _out = server.conn.process(out.dgram(), now()); + let _ = server.conn.process(out.dgram(), now()); assert!(server.conn.state().connected()); assert!(client.tls_info().unwrap().resumed()); @@ -4005,9 +4012,7 @@ mod tests { let out = client.process(None, now()); let _ = server.conn.process(out.dgram(), now()); - server.encoder.set_max_capacity(100).unwrap(); - server.encoder.set_max_blocked_streams(100).unwrap(); - server.encoder.send(&mut server.conn).unwrap(); + setup_server_side_encoder(&mut client, &mut server); let headers = vec![ (String::from(":status"), String::from("200")), @@ -4036,13 +4041,13 @@ mod tests { server.conn.stream_close_send(request_stream_id).unwrap(); let out = server.conn.process(None, now()); - let _out = client.process(out.dgram(), now()); + let _ = client.process(out.dgram(), now()); let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. }); assert!(!client.events().any(header_ready_event)); // Let client receive the encoder instructions. - let _out = client.process(qpack_pkt1.dgram(), now()); + let _ = client.process(qpack_pkt1.dgram(), now()); assert!(client.events().any(header_ready_event)); } @@ -5088,7 +5093,7 @@ mod tests { .unwrap(); let out = client.process(None, now()); - let _out = server.conn.process(out.dgram(), now()); + let _ = server.conn.process(out.dgram(), now()); // Check that encoder got stream_canceled instruction. let mut inst = [0_u8; 100]; let (amount, fin) = server @@ -5099,4 +5104,258 @@ mod tests { assert_eq!(amount, STREAM_CANCELED_ID_0.len()); assert_eq!(&inst[..amount], STREAM_CANCELED_ID_0); } + + #[test] + fn data_readable_in_decoder_blocked_state() { + let (mut client, mut server, request_stream_id) = connect_and_send_request(true); + + setup_server_side_encoder(&mut client, &mut server); + + let headers = vec![ + (String::from(":status"), String::from("200")), + (String::from("my-header"), String::from("my-header")), + (String::from("content-length"), String::from("0")), + ]; + let encoded_headers = server + .encoder + .encode_header_block(&mut server.conn, &headers, request_stream_id) + .unwrap(); + let hframe = HFrame::Headers { + header_block: encoded_headers.to_vec(), + }; + + // Delay encoder instruction so that the stream will be blocked. + let encoder_insts = server.conn.process(None, now()); + + // Send response headers. + let mut d = Encoder::default(); + hframe.encode(&mut d); + server_send_response_and_exchange_packet( + &mut client, + &mut server, + request_stream_id, + &d, + false, + ); + + // Headers are blocked waiting fro the encoder instructions. + let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. }); + assert!(!client.events().any(header_ready_event)); + + // Now send data frame. This will trigger DataRead event. + let mut d = Encoder::default(); + hframe.encode(&mut d); + let d_frame = HFrame::Data { len: 0 }; + d_frame.encode(&mut d); + server_send_response_and_exchange_packet( + &mut client, + &mut server, + request_stream_id, + &d, + true, + ); + + // Now read headers. + let _ = client.process(encoder_insts.dgram(), now()); + } + + #[test] + fn qpack_stream_reset() { + let (mut client, mut server, request_stream_id) = connect_and_send_request(true); + setup_server_side_encoder(&mut client, &mut server); + // Cancel request. + let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code()); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 0); + let out = client.process(None, now()); + let _ = server.conn.process(out.dgram(), now()); + let _ = server + .encoder + .recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 1); + } + + fn send_headers_using_encoder( + client: &mut Http3Client, + server: &mut TestServer, + request_stream_id: u64, + headers: &[(String, String)], + data: &[u8], + ) -> Option { + let encoded_headers = server + .encoder + .encode_header_block(&mut server.conn, &headers, request_stream_id) + .unwrap(); + let hframe = HFrame::Headers { + header_block: encoded_headers.to_vec(), + }; + + let out = server.conn.process(None, now()); + + // Send response + let mut d = Encoder::default(); + hframe.encode(&mut d); + let d_frame = HFrame::Data { + len: u64::try_from(data.len()).unwrap(), + }; + d_frame.encode(&mut d); + d.encode(data); + server_send_response_and_exchange_packet(client, server, request_stream_id, &d, true); + + out.dgram() + } + + #[test] + fn qpack_stream_reset_recv() { + let (mut client, mut server, request_stream_id) = connect_and_send_request(true); + setup_server_side_encoder(&mut client, &mut server); + + // Cancel request. + let _ = server + .conn + .stream_reset_send(request_stream_id, Error::HttpRequestCancelled.code()); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 0); + let out = server.conn.process(None, now()); + let out = client.process(out.dgram(), now()); + let _ = server.conn.process(out.dgram(), now()); + let _ = server + .encoder + .recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 1); + } + + #[test] + fn qpack_stream_reset_during_header_qpack_blocked() { + let (mut client, mut server, request_stream_id) = connect_and_send_request(true); + + setup_server_side_encoder(&mut client, &mut server); + + let _ = send_headers_using_encoder( + &mut client, + &mut server, + request_stream_id, + &[ + (String::from(":status"), String::from("200")), + (String::from("my-header"), String::from("my-header")), + (String::from("content-length"), String::from("3")), + ], + &[0x61, 0x62, 0x63], + ); + + let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. }); + assert!(!client.events().any(header_ready_event)); + + // Cancel request. + let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code()); + + assert_eq!(server.encoder.stats().stream_cancelled_recv, 0); + let out = client.process(None, now()); + let _ = server.conn.process(out.dgram(), now()); + let _ = server + .encoder + .recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 1); + } + + #[test] + fn qpack_no_stream_cancelled_after_fin() { + let (mut client, mut server, request_stream_id) = connect_and_send_request(true); + + setup_server_side_encoder(&mut client, &mut server); + + let encoder_instruct = send_headers_using_encoder( + &mut client, + &mut server, + request_stream_id, + &[ + (String::from(":status"), String::from("200")), + (String::from("my-header"), String::from("my-header")), + (String::from("content-length"), String::from("3")), + ], + &[], + ); + + // Exchange encoder instructions + let _ = client.process(encoder_instruct, now()); + + let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. }); + assert!(client.events().any(header_ready_event)); + // After this the recv_stream is in ClosePending state + + // Cancel request. + let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code()); + + assert_eq!(server.encoder.stats().stream_cancelled_recv, 0); + let out = client.process(None, now()); + let _ = server.conn.process(out.dgram(), now()); + let _ = server + .encoder + .recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 0); + } + + #[test] + fn qpack_stream_reset_push_promise_header_decoder_block() { + let (mut client, mut server, request_stream_id) = connect_and_send_request(true); + + setup_server_side_encoder(&mut client, &mut server); + + let headers = vec![ + (String::from(":status"), String::from("200")), + (String::from("content-length"), String::from("3")), + ]; + let encoded_headers = server + .encoder + .encode_header_block(&mut server.conn, &headers, request_stream_id) + .unwrap(); + let hframe = HFrame::Headers { + header_block: encoded_headers.to_vec(), + }; + + // Send the encoder instructions. + let out = server.conn.process(None, now()); + let _ = client.process(out.dgram(), now()); + + // Send PushPromise that will be blocked waiting for decoder instructions. + let _ = send_push_promise_using_encoder(&mut client, &mut server, request_stream_id, 0); + + // Send response + let mut d = Encoder::default(); + hframe.encode(&mut d); + let d_frame = HFrame::Data { len: 0 }; + d_frame.encode(&mut d); + server_send_response_and_exchange_packet( + &mut client, + &mut server, + request_stream_id, + &d, + true, + ); + + let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. }); + assert!(client.events().any(header_ready_event)); + + // Cancel request. + let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code()); + + let out = client.process(None, now()); + let _ = server.conn.process(out.dgram(), now()); + let _ = server + .encoder + .recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 1); + } + + #[test] + fn qpack_stream_reset_dynamic_table_zero() { + let (mut client, mut server, request_stream_id) = connect_and_send_request(true); + // Cancel request. + let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code()); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 0); + let out = client.process(None, now()); + let _ = server.conn.process(out.dgram(), now()); + let _ = server + .encoder + .recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID); + assert_eq!(server.encoder.stats().stream_cancelled_recv, 0); + } } diff --git a/third_party/rust/neqo-http3/src/lib.rs b/third_party/rust/neqo-http3/src/lib.rs index ea4bc1ee98df..2c94902146e0 100644 --- a/third_party/rust/neqo-http3/src/lib.rs +++ b/third_party/rust/neqo-http3/src/lib.rs @@ -244,7 +244,8 @@ impl ::std::fmt::Display for Error { } pub trait RecvStream: Debug { - fn stream_reset(&self, app_error: AppError); + fn stream_reset_recv(&self, app_error: AppError, decoder: &mut QPackDecoder); + fn stream_reset(&self, decoder: &mut QPackDecoder); /// # Errors /// An error may happen while reading a stream, e.g. early close, protocol error, etc. fn receive(&mut self, conn: &mut Connection, decoder: &mut QPackDecoder) -> Res<()>; diff --git a/third_party/rust/neqo-http3/src/push_controller.rs b/third_party/rust/neqo-http3/src/push_controller.rs index cb3ab16a834c..65bb3cb32d42 100644 --- a/third_party/rust/neqo-http3/src/push_controller.rs +++ b/third_party/rust/neqo-http3/src/push_controller.rs @@ -365,7 +365,7 @@ impl PushController { self.push_streams.close(push_id); Ok(()) } - _ => Err(Error::InvalidStreamId), + Some(_) => Err(Error::InvalidStreamId), } } @@ -434,7 +434,7 @@ impl PushController { Some(PushState::Active { .. }) => { self.conn_events.insert(event); } - _ => { + Some(_) => { debug_assert!(false, "No record of a stream!"); } } diff --git a/third_party/rust/neqo-http3/src/push_stream.rs b/third_party/rust/neqo-http3/src/push_stream.rs index 4f89f3a712c4..58d0582f426d 100644 --- a/third_party/rust/neqo-http3/src/push_stream.rs +++ b/third_party/rust/neqo-http3/src/push_stream.rs @@ -139,12 +139,21 @@ impl RecvStream for PushStream { matches!(self.state, PushStreamState::Closed) } - fn stream_reset(&self, _app_error: AppError) { + fn stream_reset_recv(&self, _app_error: AppError, decoder: &mut QPackDecoder) { + if !self.done() { + decoder.cancel_stream(self.stream_id); + } if let Some(push_id) = self.state.push_id() { self.push_handler.borrow_mut().push_stream_reset(push_id); } } + fn stream_reset(&self, decoder: &mut QPackDecoder) { + if !self.done() { + decoder.cancel_stream(self.stream_id); + } + } + fn read_data( &mut self, conn: &mut Connection, diff --git a/third_party/rust/neqo-http3/src/recv_message.rs b/third_party/rust/neqo-http3/src/recv_message.rs index 42556a0709ea..905dd87f13fd 100644 --- a/third_party/rust/neqo-http3/src/recv_message.rs +++ b/third_party/rust/neqo-http3/src/recv_message.rs @@ -294,6 +294,13 @@ impl RecvMessage { } self.state = RecvMessageState::Closed; } + + fn closing(&self) -> bool { + matches!( + self.state, + RecvMessageState::ClosePending | RecvMessageState::Closed + ) + } } impl RecvStream for RecvMessage { @@ -323,10 +330,19 @@ impl RecvStream for RecvMessage { matches!(self.state, RecvMessageState::Closed) } - fn stream_reset(&self, app_error: AppError) { + fn stream_reset_recv(&self, app_error: AppError, decoder: &mut QPackDecoder) { + if !self.closing() || !self.blocked_push_promise.is_empty() { + decoder.cancel_stream(self.stream_id); + } self.conn_events.reset(self.stream_id, app_error); } + fn stream_reset(&self, decoder: &mut QPackDecoder) { + if !self.closing() || !self.blocked_push_promise.is_empty() { + decoder.cancel_stream(self.stream_id); + } + } + fn read_data( &mut self, conn: &mut Connection, diff --git a/third_party/rust/neqo-http3/src/send_message.rs b/third_party/rust/neqo-http3/src/send_message.rs index 616a797a8ecb..c8580edea499 100644 --- a/third_party/rust/neqo-http3/src/send_message.rs +++ b/third_party/rust/neqo-http3/src/send_message.rs @@ -13,7 +13,6 @@ use neqo_common::{qdebug, qinfo, qtrace, Encoder}; use neqo_qpack::encoder::QPackEncoder; use neqo_transport::{AppError, Connection}; use std::cmp::min; -use std::convert::TryFrom; use std::fmt::Debug; const MAX_DATA_HEADER_SIZE_2: usize = (1 << 6) - 1; // Maximal amount of data with DATA frame header size 2 @@ -140,11 +139,9 @@ impl SendMessage { | SendMessageState::Initialized { .. } | SendMessageState::SendingInitialMessage { .. } => Ok(0), SendMessageState::SendingData => { - let available = usize::try_from( - conn.stream_avail_send_space(self.stream_id) - .map_err(|e| Error::map_stream_send_errors(&e))?, - ) - .unwrap_or(usize::max_value()); + let available = conn + .stream_avail_send_space(self.stream_id) + .map_err(|e| Error::map_stream_send_errors(&e))?; if available <= 2 { return Ok(0); } diff --git a/third_party/rust/neqo-http3/src/server.rs b/third_party/rust/neqo-http3/src/server.rs index ea10c455ca70..50f7437f01f3 100644 --- a/third_party/rust/neqo-http3/src/server.rs +++ b/third_party/rust/neqo-http3/src/server.rs @@ -87,7 +87,7 @@ impl Http3Server { } /// Process HTTP3 layer. - pub fn process_http3(&mut self, now: Instant) { + fn process_http3(&mut self, now: Instant) { qtrace!([self], "Process http3 internal."); let mut active_conns = self.server.active_connections(); diff --git a/third_party/rust/neqo-http3/src/settings.rs b/third_party/rust/neqo-http3/src/settings.rs index a4aa09d28dbc..72348fa9c806 100644 --- a/third_party/rust/neqo-http3/src/settings.rs +++ b/third_party/rust/neqo-http3/src/settings.rs @@ -166,7 +166,7 @@ impl ZeroRttChecker for HttpZeroRttChecker { u64::from(self.settings.max_blocked_streams) >= setting.value } HSettingType::MaxTableCapacity => self.settings.max_table_size_decoder >= setting.value, - _ => true, + HSettingType::MaxHeaderListSize => true, }) { ZeroRttCheckResult::Accept } else { diff --git a/third_party/rust/neqo-qpack/.cargo-checksum.json b/third_party/rust/neqo-qpack/.cargo-checksum.json index feb27b8398be..119537c3b473 100644 --- a/third_party/rust/neqo-qpack/.cargo-checksum.json +++ b/third_party/rust/neqo-qpack/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"5f585c883d2c53b2934848b0022f18bb85b7fe6dbe68a719f25478435081fe1b","src/decoder.rs":"1933c031d3de604479dcd753e46b6c6a7f5a9e6957b5e8e425ac6ceb80bb567b","src/decoder_instructions.rs":"a8e04dff5fc4c658322a10daadab947dc2e41932c00c3f8d387671a86d0516af","src/encoder.rs":"f881a54e020a41a3a68bcfa8290e309ea5deab4dbbc3fe3c0103b5d6d9d3f800","src/encoder_instructions.rs":"1d4424bf21c0ac26b7c8fee6450b943346c5493ab86dd7ec2edc5f566454721e","src/header_block.rs":"f935872919154f678947732270d688be4790309f9e390a0c8eb6c9484a41a8dd","src/huffman.rs":"68fa0bada0c35d20f793980596accdcc548970214841f71789290fc334e51fc1","src/huffman_decode_helper.rs":"2970c57f052878b727c2f764490c54184f5c2608e1d6aa961c3b01509e290122","src/huffman_table.rs":"06fea766a6276ac56c7ee0326faed800a742c15fda1f33bf2513e6cc6a5e6d27","src/lib.rs":"8ee2082fd94064e61286e24e5192ee02eee50b1ca82b9105a1e1945e596ccaa8","src/prefix.rs":"72c587c40aef4ed38cf13b2de91091d671611679be2a9da6f0b24abafaf50dc5","src/qlog.rs":"7618085e27bb3fb1f4d1c73ba501b9a293723293c4020b7cc4129676eb278131","src/qpack_send_buf.rs":"5170b93afaf0c1609463e6c5ae4dccb1a2ce4e4407296db1bcaf06c6b5bb97ab","src/reader.rs":"4bcea0de1d7dc09ec0cdff364d8f62da54bbbe1f6db55a495f943f31369b4074","src/static_table.rs":"fda9d5c6f38f94b0bf92d3afdf8432dce6e27e189736596e16727090c77b78ec","src/stats.rs":"39b8bb0af37c7f05e9db40f74dfec2f6be4a3f112c2347dd235c2488403eb03c","src/table.rs":"f7091bdd9ad1f8fe3b2298a7dbfd3d285c212d69569cda54f9bcf251cb758a21"},"package":null} \ No newline at end of file +{"files":{"Cargo.toml":"da9afbbea6416a849713467e4658890ab8a2d873198628ce8055a06e37177227","src/decoder.rs":"9f19a014fd800553904ae26407b744b4adb4608c314fbedae1c1aee4fa5902fd","src/decoder_instructions.rs":"a8e04dff5fc4c658322a10daadab947dc2e41932c00c3f8d387671a86d0516af","src/encoder.rs":"511f53f32fe499f8956372a4c23d058c1f8017f396e9601788efbf26d541140d","src/encoder_instructions.rs":"1d4424bf21c0ac26b7c8fee6450b943346c5493ab86dd7ec2edc5f566454721e","src/header_block.rs":"f935872919154f678947732270d688be4790309f9e390a0c8eb6c9484a41a8dd","src/huffman.rs":"68fa0bada0c35d20f793980596accdcc548970214841f71789290fc334e51fc1","src/huffman_decode_helper.rs":"2970c57f052878b727c2f764490c54184f5c2608e1d6aa961c3b01509e290122","src/huffman_table.rs":"06fea766a6276ac56c7ee0326faed800a742c15fda1f33bf2513e6cc6a5e6d27","src/lib.rs":"8ee2082fd94064e61286e24e5192ee02eee50b1ca82b9105a1e1945e596ccaa8","src/prefix.rs":"72c587c40aef4ed38cf13b2de91091d671611679be2a9da6f0b24abafaf50dc5","src/qlog.rs":"7618085e27bb3fb1f4d1c73ba501b9a293723293c4020b7cc4129676eb278131","src/qpack_send_buf.rs":"5170b93afaf0c1609463e6c5ae4dccb1a2ce4e4407296db1bcaf06c6b5bb97ab","src/reader.rs":"4bcea0de1d7dc09ec0cdff364d8f62da54bbbe1f6db55a495f943f31369b4074","src/static_table.rs":"fda9d5c6f38f94b0bf92d3afdf8432dce6e27e189736596e16727090c77b78ec","src/stats.rs":"bc82c8f86b54981be234948e285af3d778ba9d23ddaaf2fbedf9c03121eaada7","src/table.rs":"f7091bdd9ad1f8fe3b2298a7dbfd3d285c212d69569cda54f9bcf251cb758a21"},"package":null} \ No newline at end of file diff --git a/third_party/rust/neqo-qpack/Cargo.toml b/third_party/rust/neqo-qpack/Cargo.toml index 23dd3e0c75f6..eb5ac65ba7ce 100644 --- a/third_party/rust/neqo-qpack/Cargo.toml +++ b/third_party/rust/neqo-qpack/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "neqo-qpack" -version = "0.4.8" +version = "0.4.9" authors = ["Dragana Damjanovic "] edition = "2018" license = "MIT/Apache-2.0" diff --git a/third_party/rust/neqo-qpack/src/decoder.rs b/third_party/rust/neqo-qpack/src/decoder.rs index aa1f14a27b18..6f0eabf3d184 100644 --- a/third_party/rust/neqo-qpack/src/decoder.rs +++ b/third_party/rust/neqo-qpack/src/decoder.rs @@ -151,7 +151,10 @@ impl QPackDecoder { } pub fn cancel_stream(&mut self, stream_id: u64) { - DecoderInstruction::StreamCancellation { stream_id }.marshal(&mut self.send_buf); + if self.table.capacity() > 0 { + self.blocked_streams.retain(|(id, _)| *id != stream_id); + DecoderInstruction::StreamCancellation { stream_id }.marshal(&mut self.send_buf); + } } /// # Errors @@ -189,10 +192,20 @@ impl QPackDecoder { match decoder.decode_header_block(&self.table, self.max_entries, self.table.base()) { Ok(HeaderDecoderResult::Blocked(req_insert_cnt)) => { - self.blocked_streams.push((stream_id, req_insert_cnt)); if self.blocked_streams.len() > self.max_blocked_streams { Err(Error::DecompressionFailed) } else { + let r = self + .blocked_streams + .iter() + .filter_map(|(id, req)| if *id <= stream_id { Some(*req) } else { None }) + .collect::>(); + if !r.is_empty() { + debug_assert!(r.len() == 1); + debug_assert!(r[0] == req_insert_cnt); + return Ok(None); + } + self.blocked_streams.push((stream_id, req_insert_cnt)); Ok(None) } } diff --git a/third_party/rust/neqo-qpack/src/encoder.rs b/third_party/rust/neqo-qpack/src/encoder.rs index c56559fd6ac0..292f7ebc6c40 100644 --- a/third_party/rust/neqo-qpack/src/encoder.rs +++ b/third_party/rust/neqo-qpack/src/encoder.rs @@ -223,6 +223,7 @@ impl QPackEncoder { } DecoderInstruction::HeaderAck { stream_id } => self.header_ack(stream_id), DecoderInstruction::StreamCancellation { stream_id } => { + self.stats.stream_cancelled_recv += 1; self.stream_cancellation(stream_id) } _ => Ok(()), diff --git a/third_party/rust/neqo-qpack/src/stats.rs b/third_party/rust/neqo-qpack/src/stats.rs index b128a7355ed1..b5712de74b3b 100644 --- a/third_party/rust/neqo-qpack/src/stats.rs +++ b/third_party/rust/neqo-qpack/src/stats.rs @@ -12,4 +12,5 @@ pub struct Stats { pub dynamic_table_inserts: usize, // This is the munber of header blockes that reference the dynamic table. pub dynamic_table_references: usize, + pub stream_cancelled_recv: usize, } diff --git a/third_party/rust/neqo-transport/.cargo-checksum.json b/third_party/rust/neqo-transport/.cargo-checksum.json index 29da97e0e070..07b440dbbd65 100644 --- a/third_party/rust/neqo-transport/.cargo-checksum.json +++ b/third_party/rust/neqo-transport/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"140f9401908daff563cc06e0e647bceace5f316dcbba8bef987f9f2d085d4166","TODO":"d759cb804b32fa9d96ea8d3574a3c4073da9fe6a0b02b708a0e22cce5a5b4a0f","src/cc.rs":"e89194d22ac09f4e7b6ab520079c6d580aa82faa40021dce9802304e2e2a2585","src/cid.rs":"936ed772eddf533cb962332a68034a0e209023991fa44ec72e2805cdff29a3c2","src/connection.rs":"62098192bc2e40ff42d62ee663b3b9f509aac7ce0ac6be982dfecd07bbae6314","src/crypto.rs":"dcd99f95b20fb5f336338f35574af1a2e96245aeea27e0164597cbba31161609","src/dump.rs":"d69ccb0e3b240823b886a791186afbac9f2e26d1f1f67a55dbf86f8cd3e6203e","src/events.rs":"1a17db9b910b5f7260ff363f0f13700767545a149d0e4a7c0454f1ff73a78fa6","src/flow_mgr.rs":"ff820f4f45fb5dbed5e4b1e9433483a36fc624bd5fa1a22dd7abb779ba723caf","src/frame.rs":"d35059f97154d918e46c3d9b9fc5e6bf18b45265f523da57eee56a3c3e153be4","src/lib.rs":"ab39d43753c19cabdd697ab4265aaf079ecf19f1c72bc31aef74dca9c9ab59b9","src/pace.rs":"cee74faccfba9de2a827782b5a34015626b7748be27ffd4942a4caaaf14c6c18","src/packet/mod.rs":"4440a6aa20bd47b5798f8b2728d1e089787743a4eeff954d2aaa63d899d24114","src/packet/retry.rs":"842b1fa864e03e2bbe76dd2daeaa727cf20440fed2b654ca3b50f62bc4df26a8","src/path.rs":"3ef5d00c247cfe0eced6885cdd283e25766ee979d5bdc662abfdf4d4b962dbf1","src/qlog.rs":"0bd5acb9e8be04fc9f65852d7144acf8d2ac5ebf70a351ec6bceb3e3bcb4a14e","src/recovery.rs":"7abec6675d296ab98d176abb17d72025b42431c0a487e67723ba3c5472c11d83","src/recv_stream.rs":"6c164fa54dee1a0ff6aa2d3ab4c5ec48d7ed624907d980d811a8c361995e85b6","src/send_stream.rs":"0d65328d63ccdefde6fff3eb3b3d7dcbb3c7b5605e0dc290d6fd6abd3e29c89c","src/server.rs":"6dd25aa224da98ed970fd96ab5c462bf3a35376ed8f69352b686a70dc95f833f","src/stats.rs":"8593be6985c4545371c3d1563ba9582118d6005b0f51b9c04b98a0dd01a55002","src/stream_id.rs":"74c3523085bfdddfb009a33a747b329b27007b3f0ba728b18a5b6e8ab8ad1d26","src/tparams.rs":"91da52cd894ae9c6097100e728c1b6679cafa7743dedc1902d0dff0f2d17521d","src/tracking.rs":"ad41a166ecef97730ba1dc215f732778e9e043bf05941f2225930d546a4931cf","tests/conn_vectors.rs":"4ff98a0a65fe31e3503d1ec3d5fc9cdb99815a4e0343510a17d0a0ab122650ac","tests/connection.rs":"a93985c199a9ef987106f4a20b35ebf803cdbbb855c07b1362b403eed7101ef8","tests/server.rs":"759557986f606b4b2d3a02e6c99ab07ca29dbe92d4d7fdc229ccdbc52899d125"},"package":null} \ No newline at end of file +{"files":{"Cargo.toml":"12aa405824e4721413b634d7ab23d1a5fec4afd65b3f08ed18dcf04f29f89325","TODO":"d759cb804b32fa9d96ea8d3574a3c4073da9fe6a0b02b708a0e22cce5a5b4a0f","src/cc.rs":"4a3409ddcf653fc67057f337c6a07a58d52979f683b43c0b8b802100f2685aa7","src/cid.rs":"936ed772eddf533cb962332a68034a0e209023991fa44ec72e2805cdff29a3c2","src/connection.rs":"a6760732ba17649636cd1ccbc9c4f2678ccb7ad34f143fea7aa74fa67c753efe","src/crypto.rs":"ee5ae4b73a8d55aa1e65fb83d86c03b4e0f2db27b6c0d410ca944da0268906e8","src/dump.rs":"d69ccb0e3b240823b886a791186afbac9f2e26d1f1f67a55dbf86f8cd3e6203e","src/events.rs":"3d545f4bf0625dee172c580c3881b3a87ece055335dd08780ec66780a5398547","src/flow_mgr.rs":"ff820f4f45fb5dbed5e4b1e9433483a36fc624bd5fa1a22dd7abb779ba723caf","src/frame.rs":"f56d38e67524e19c37cde41fe74c01831d45ebe76b595e8f6e275a34360b5126","src/lib.rs":"7f8b642ef73bbb5a498514a9617e2650ea4f73204311eca971f0c41b8ff06e77","src/pace.rs":"eb9094cfcae54162022f70f230b6a9811add0063b878100f147a9365473f6612","src/packet/mod.rs":"a77866c8f6c07a2ca9e41093644da495ca6370103b6154180a70cddcb6d9528e","src/packet/retry.rs":"842b1fa864e03e2bbe76dd2daeaa727cf20440fed2b654ca3b50f62bc4df26a8","src/path.rs":"3ef5d00c247cfe0eced6885cdd283e25766ee979d5bdc662abfdf4d4b962dbf1","src/qlog.rs":"01c2d07ca4572f4283d0b0120bf661c5031c129d6f2e5917235600c33add18a4","src/recovery.rs":"9fb6349a0a6430bc034b1981039611e731b780fb8c7ae9b53adf1592b7c5ab7b","src/recv_stream.rs":"b1688b9300320e0aa2c6b862030bfda6b50d9a4e6d14a3d89dd8554e92b780e0","src/send_stream.rs":"223547f3140e9b459c303d46393760d7523b5b9d74c72c82d383b0ceb5a9f429","src/server.rs":"6dd25aa224da98ed970fd96ab5c462bf3a35376ed8f69352b686a70dc95f833f","src/stats.rs":"65927fbdd26632730e7029020ad02e9e6c59b2ec9ef26693369e81d4530149fd","src/stream_id.rs":"74c3523085bfdddfb009a33a747b329b27007b3f0ba728b18a5b6e8ab8ad1d26","src/tparams.rs":"91da52cd894ae9c6097100e728c1b6679cafa7743dedc1902d0dff0f2d17521d","src/tracking.rs":"20c1fd08edfaa77aba8adac586d3efe9c204d8e8cc484c1bbf7be85ceac84e71","tests/conn_vectors.rs":"4ff98a0a65fe31e3503d1ec3d5fc9cdb99815a4e0343510a17d0a0ab122650ac","tests/connection.rs":"a93985c199a9ef987106f4a20b35ebf803cdbbb855c07b1362b403eed7101ef8","tests/network.rs":"a986c22da7132ec843a44c4bcb5a7d2726132aa27a47a8ea91634cd88e1b763b","tests/server.rs":"24ead84b0436daadbd22436dccf3625ab7ed83804829519e102ab7f226044a61","tests/sim/connection.rs":"d3c565990e1ca05d872bcdf83e48e66af79e6bac82a6a40d7cdd3ba193fee3c6","tests/sim/delay.rs":"9efa722adb89e37262369e9f3c67405f0acc8c24997271811e48df9e856e5a8d","tests/sim/drop.rs":"bd89e5c71cdd1b27cd755faaedd87d5feadf2f424df721a7df41a51bcebcbb58","tests/sim/mod.rs":"c6230ea030e69b9c6c4fcb30ba3f397c6da588498454f34f5075d16ce4db0542","tests/sim/net.rs":"597f4d37bc26c3d82eeeaa6d14dd03bc2be3930686df2b293748b43c07c497d7","tests/sim/rng.rs":"2c90b0bbaf0c952ebee232deb3594f7a86af387737b15474de3e97ee6b623d90","tests/sim/taildrop.rs":"5c505d150f0071e8cc2d540b3a817a6942fdf13df32f1fbc6822952f2e146176"},"package":null} \ No newline at end of file diff --git a/third_party/rust/neqo-transport/Cargo.toml b/third_party/rust/neqo-transport/Cargo.toml index 7d4c104285c9..b332a1745f17 100644 --- a/third_party/rust/neqo-transport/Cargo.toml +++ b/third_party/rust/neqo-transport/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "neqo-transport" -version = "0.4.8" +version = "0.4.9" authors = ["EKR ", "Andy Grover "] edition = "2018" license = "MIT/Apache-2.0" @@ -12,6 +12,7 @@ lazy_static = "1.3.0" log = {version = "0.4.0", default-features = false} smallvec = "1.0.0" qlog = "0.3.0" +indexmap = "1.0" [dev-dependencies] test-fixture = { path = "../test-fixture" } diff --git a/third_party/rust/neqo-transport/src/cc.rs b/third_party/rust/neqo-transport/src/cc.rs index 7c4ee5dbe182..e5d0d24d41ac 100644 --- a/third_party/rust/neqo-transport/src/cc.rs +++ b/third_party/rust/neqo-transport/src/cc.rs @@ -6,7 +6,7 @@ // Congestion control -use std::cmp::max; +use std::cmp::{max, min}; use std::fmt::{self, Display}; use std::time::{Duration, Instant}; @@ -31,9 +31,11 @@ const PERSISTENT_CONG_THRESH: u32 = 3; pub struct CongestionControl { congestion_window: usize, // = kInitialWindow bytes_in_flight: usize, + acked_bytes: usize, congestion_recovery_start_time: Option, ssthresh: usize, pacer: Option, + in_recovery: bool, qlog: NeqoQlog, qlog_curr_cong_state: CongestionState, @@ -44,9 +46,11 @@ impl Default for CongestionControl { Self { congestion_window: INITIAL_WINDOW, bytes_in_flight: 0, + acked_bytes: 0, congestion_recovery_start_time: None, ssthresh: std::usize::MAX, pacer: None, + in_recovery: false, qlog: NeqoQlog::disabled(), qlog_curr_cong_state: CongestionState::SlowStart, } @@ -67,12 +71,6 @@ impl Display for CongestionControl { } } -#[derive(Debug, Clone, Copy)] -enum PacketState { - Acked, - Lost, -} - impl CongestionControl { pub fn set_qlog(&mut self, qlog: NeqoQlog) { self.qlog = qlog; @@ -90,6 +88,12 @@ impl CongestionControl { self.ssthresh } + #[cfg(test)] + #[must_use] + pub fn bif(&self) -> usize { + self.bytes_in_flight + } + #[must_use] pub fn cwnd_avail(&self) -> usize { // BIF can be higher than cwnd due to PTO packets, which are sent even @@ -103,10 +107,17 @@ impl CongestionControl { assert!(self.bytes_in_flight >= pkt.size); self.bytes_in_flight -= pkt.size; - if self.in_congestion_recovery(pkt.time_sent, PacketState::Acked) { - // Do not increase congestion window in recovery period. + if !self.after_recovery_start(pkt.time_sent) { + // Do not increase congestion window for packets sent before + // recovery start. continue; } + + if self.in_recovery { + self.in_recovery = false; + qlog::metrics_updated(&mut self.qlog, &[QlogMetric::InRecovery(false)]); + } + if self.app_limited() { // Do not increase congestion_window if application limited. qlog::congestion_state_updated( @@ -117,31 +128,40 @@ impl CongestionControl { continue; } - if self.congestion_window < self.ssthresh { - self.congestion_window += pkt.size; - qinfo!([self], "slow start"); - qlog::congestion_state_updated( - &mut self.qlog, - &mut self.qlog_curr_cong_state, - CongestionState::SlowStart, - ); - } else { - self.congestion_window += (MAX_DATAGRAM_SIZE * pkt.size) / self.congestion_window; - qinfo!([self], "congestion avoidance"); - qlog::congestion_state_updated( - &mut self.qlog, - &mut self.qlog_curr_cong_state, - CongestionState::CongestionAvoidance, - ); - } - qlog::metrics_updated( + self.acked_bytes += pkt.size; + } + qtrace!([self], "ACK received, acked_bytes = {}", self.acked_bytes); + + // Slow start, up to the slow start threshold. + if self.congestion_window < self.ssthresh { + let increase = min(self.ssthresh - self.congestion_window, self.acked_bytes); + self.congestion_window += increase; + self.acked_bytes -= increase; + qinfo!([self], "slow start += {}", increase); + qlog::congestion_state_updated( &mut self.qlog, - &[ - QlogMetric::CongestionWindow(self.congestion_window), - QlogMetric::BytesInFlight(self.bytes_in_flight), - ], + &mut self.qlog_curr_cong_state, + CongestionState::SlowStart, ); } + // Congestion avoidance, above the slow start threshold. + if self.acked_bytes >= self.congestion_window { + self.acked_bytes -= self.congestion_window; + self.congestion_window += MAX_DATAGRAM_SIZE; + qinfo!([self], "congestion avoidance += {}", MAX_DATAGRAM_SIZE); + qlog::congestion_state_updated( + &mut self.qlog, + &mut self.qlog_curr_cong_state, + CongestionState::CongestionAvoidance, + ); + } + qlog::metrics_updated( + &mut self.qlog, + &[ + QlogMetric::CongestionWindow(self.congestion_window), + QlogMetric::BytesInFlight(self.bytes_in_flight), + ], + ); } pub fn on_packets_lost( @@ -155,7 +175,7 @@ impl CongestionControl { return; } - for pkt in lost_packets.iter().filter(|pkt| pkt.cc_in_flight()) { + for pkt in lost_packets.iter().filter(|pkt| pkt.ack_eliciting()) { assert!(self.bytes_in_flight >= pkt.size); self.bytes_in_flight -= pkt.size; } @@ -178,6 +198,11 @@ impl CongestionControl { { if last_lost_pkt.time_sent.duration_since(first.time_sent) > congestion_period { self.congestion_window = MIN_CONG_WINDOW; + self.acked_bytes = 0; + qlog::metrics_updated( + &mut self.qlog, + &[QlogMetric::CongestionWindow(self.congestion_window)], + ); qinfo!([self], "persistent congestion"); } } @@ -201,7 +226,7 @@ impl CongestionControl { .unwrap() .spend(pkt.time_sent, rtt, self.congestion_window, pkt.size); - if !pkt.cc_in_flight() { + if !pkt.ack_eliciting() { return; } @@ -217,34 +242,23 @@ impl CongestionControl { &mut self.qlog, &[QlogMetric::BytesInFlight(self.bytes_in_flight)], ); - - debug_assert!(self.bytes_in_flight <= self.congestion_window); } #[must_use] - fn in_congestion_recovery(&mut self, sent_time: Instant, packet_state: PacketState) -> bool { + fn after_recovery_start(&mut self, sent_time: Instant) -> bool { match self.congestion_recovery_start_time { - Some(crst) => { - if sent_time <= crst { - true - } else { - if let PacketState::Acked = packet_state { - qlog::metrics_updated(&mut self.qlog, &[QlogMetric::InRecovery(false)]); - self.congestion_recovery_start_time = None; - } - false - } - } - None => false, + Some(crst) => sent_time > crst, + None => true, } } fn on_congestion_event(&mut self, now: Instant, sent_time: Instant) { - // Start a new congestion event if packet was sent after the - // start of the previous congestion recovery period. - if !self.in_congestion_recovery(sent_time, PacketState::Lost) { + // Start a new congestion event if lost packet was sent after the start + // of the previous congestion recovery period. + if self.after_recovery_start(sent_time) { self.congestion_recovery_start_time = Some(now); self.congestion_window /= 2; // kLossReductionFactor = 0.5 + self.acked_bytes /= 2; self.congestion_window = max(self.congestion_window, MIN_CONG_WINDOW); self.ssthresh = self.congestion_window; qinfo!( @@ -256,10 +270,12 @@ impl CongestionControl { qlog::metrics_updated( &mut self.qlog, &[ + QlogMetric::CongestionWindow(self.congestion_window), QlogMetric::SsThresh(self.ssthresh), QlogMetric::InRecovery(true), ], ); + self.in_recovery = true; qlog::congestion_state_updated( &mut self.qlog, &mut self.qlog_curr_cong_state, @@ -298,3 +314,93 @@ impl CongestionControl { } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::packet::PacketType; + use std::rc::Rc; + use test_fixture::now; + + #[test] + fn issue_876() { + const PTO: Duration = Duration::from_millis(100); + const RTT: Duration = Duration::from_millis(98); + let mut cc = CongestionControl::default(); + let time_now = now(); + let time_before = time_now - Duration::from_millis(100); + let time_after1 = time_now + Duration::from_millis(100); + let time_after2 = time_now + Duration::from_millis(150); + let time_after3 = time_now + Duration::from_millis(175); + + cc.start_pacer(time_now); + + let sent_packets = vec![ + SentPacket::new( + PacketType::Short, + 1, // pn + time_before, // time sent + true, // ack eliciting + Rc::default(), // tokens + 103, // size + ), + SentPacket::new( + PacketType::Short, + 2, // pn + time_before, // time sent + true, // ack eliciting + Rc::default(), // tokens + 105, // size + ), + SentPacket::new( + PacketType::Short, + 3, // pn + time_after2, // time sent + true, // ack eliciting + Rc::default(), // tokens + 107, // size + ), + ]; + + cc.on_packet_sent(&sent_packets[0], RTT); + assert_eq!(cc.acked_bytes, 0); + assert_eq!(cc.cwnd(), INITIAL_WINDOW); + assert_eq!(cc.ssthresh(), std::usize::MAX); + assert_eq!(cc.bif(), 103); + + cc.on_packet_sent(&sent_packets[1], RTT); + assert_eq!(cc.acked_bytes, 0); + assert_eq!(cc.cwnd(), INITIAL_WINDOW); + assert_eq!(cc.ssthresh(), std::usize::MAX); + assert_eq!(cc.bif(), 208); + + cc.on_packets_lost(time_after1, None, PTO, &sent_packets[0..1]); + + // We are now in recovery + assert_eq!(cc.acked_bytes, 0); + assert_eq!(cc.cwnd(), INITIAL_WINDOW / 2); + assert_eq!(cc.ssthresh(), INITIAL_WINDOW / 2); + assert_eq!(cc.bif(), 105); + + // Send a packet after recovery starts + cc.on_packet_sent(&sent_packets[2], RTT); + assert_eq!(cc.acked_bytes, 0); + assert_eq!(cc.cwnd(), INITIAL_WINDOW / 2); + assert_eq!(cc.ssthresh(), INITIAL_WINDOW / 2); + assert_eq!(cc.bif(), 212); + + // and ack it. cwnd increases slightly + cc.on_packets_acked(&sent_packets[2..3]); + assert_eq!(cc.acked_bytes, sent_packets[2].size); + assert_eq!(cc.cwnd(), INITIAL_WINDOW / 2); + assert_eq!(cc.ssthresh(), INITIAL_WINDOW / 2); + assert_eq!(cc.bif(), 105); + + // Packet from before is lost. Should not hurt cwnd. + cc.on_packets_lost(time_after3, None, PTO, &sent_packets[1..2]); + assert_eq!(cc.acked_bytes, sent_packets[2].size); + assert_eq!(cc.cwnd(), INITIAL_WINDOW / 2); + assert_eq!(cc.ssthresh(), INITIAL_WINDOW / 2); + assert_eq!(cc.bif(), 0); + } +} diff --git a/third_party/rust/neqo-transport/src/connection.rs b/third_party/rust/neqo-transport/src/connection.rs index ab8581e4c0a2..36818ea4849d 100644 --- a/third_party/rust/neqo-transport/src/connection.rs +++ b/third_party/rust/neqo-transport/src/connection.rs @@ -45,7 +45,7 @@ use crate::qlog; use crate::recovery::{LossRecovery, RecoveryToken, SendProfile, GRANULARITY}; use crate::recv_stream::{RecvStream, RecvStreams, RECV_BUFFER_SIZE}; use crate::send_stream::{SendStream, SendStreams}; -use crate::stats::Stats; +use crate::stats::{Stats, StatsCell}; use crate::stream_id::{StreamId, StreamIndex, StreamIndexes}; use crate::tparams::{ self, TransportParameter, TransportParameterId, TransportParameters, TransportParametersHandler, @@ -228,6 +228,7 @@ impl RetryInfo { /// -transport 10.2 ("Idle Timeout"). enum IdleTimeoutState { Init, + New(Instant), PacketReceived(Instant), AckElicitingPacketSent(Instant), } @@ -254,13 +255,17 @@ impl IdleTimeout { self.timeout = min(self.timeout, peer_timeout); } - pub fn expiry(&self, pto: Duration) -> Option { - match self.state { - IdleTimeoutState::Init => None, - IdleTimeoutState::PacketReceived(t) | IdleTimeoutState::AckElicitingPacketSent(t) => { - Some(t + max(self.timeout, pto * 3)) + pub fn expiry(&mut self, now: Instant, pto: Duration) -> Instant { + let start = match self.state { + IdleTimeoutState::Init => { + self.state = IdleTimeoutState::New(now); + now } - } + IdleTimeoutState::New(t) + | IdleTimeoutState::PacketReceived(t) + | IdleTimeoutState::AckElicitingPacketSent(t) => t, + }; + start + max(self.timeout, pto * 3) } fn on_packet_sent(&mut self, now: Instant) { @@ -268,7 +273,9 @@ impl IdleTimeout { // time we reset the timeout here. match self.state { IdleTimeoutState::AckElicitingPacketSent(_) => {} - IdleTimeoutState::Init | IdleTimeoutState::PacketReceived(_) => { + IdleTimeoutState::Init + | IdleTimeoutState::New(_) + | IdleTimeoutState::PacketReceived(_) => { self.state = IdleTimeoutState::AckElicitingPacketSent(now); } } @@ -278,12 +285,8 @@ impl IdleTimeout { self.state = IdleTimeoutState::PacketReceived(now); } - pub fn expired(&self, now: Instant, pto: Duration) -> bool { - if let Some(expiry) = self.expiry(pto) { - now >= expiry - } else { - false - } + pub fn expired(&mut self, now: Instant, pto: Duration) -> bool { + now >= self.expiry(now, pto) } } @@ -456,7 +459,7 @@ pub struct Connection { loss_recovery: LossRecovery, events: ConnectionEvents, token: Option>, - stats: Stats, + stats: StatsCell, qlog: NeqoQlog, quic_version: QuicVersion, @@ -564,7 +567,8 @@ impl Connection { let crypto = Crypto::new(agent, protocols, tphandler.clone())?; - let mut c = Self { + let stats = StatsCell::default(); + let c = Self { role, state: State::Init, cid_manager, @@ -586,14 +590,14 @@ impl Connection { recv_streams: RecvStreams::default(), flow_mgr: Rc::new(RefCell::new(FlowMgr::default())), state_signaling: StateSignaling::Idle, - loss_recovery: LossRecovery::new(), + loss_recovery: LossRecovery::new(stats.clone()), events: ConnectionEvents::default(), token: None, - stats: Stats::default(), + stats, qlog: NeqoQlog::disabled(), quic_version, }; - c.stats.init(format!("{}", c)); + c.stats.borrow_mut().init(format!("{}", c)); Ok(c) } @@ -613,6 +617,13 @@ impl Connection { &mut self.qlog } + /// Get the original destination connection id for this connection. This + /// will always be present for Role::Client but not if Role::Server is in + /// State::Init. + pub fn odcid(&self) -> Option<&ConnectionId> { + self.remote_original_destination_cid.as_ref() + } + /// Set a local transport parameter, possibly overriding a default value. pub fn set_local_tparam(&self, tp: TransportParameterId, value: TransportParameter) -> Res<()> { if *self.state() == State::Init { @@ -813,9 +824,9 @@ impl Connection { &self.zero_rtt_state } - /// Get collected statistics. - pub fn stats(&self) -> &Stats { - &self.stats + /// Get a snapshot of collected statistics. + pub fn stats(&self) -> Stats { + self.stats.borrow().clone() } // This function wraps a call to another function and sets the connection state @@ -878,7 +889,8 @@ impl Connection { return; } - if self.idle_timeout.expired(now, self.loss_recovery.raw_pto()) { + let pto = self.loss_recovery.pto_raw(PNSpace::ApplicationData); + if self.idle_timeout.expired(now, pto) { qinfo!([self], "idle timeout expired"); self.set_state(State::Closed(ConnectionError::Transport( Error::IdleTimeout, @@ -928,10 +940,10 @@ impl Connection { delays.push(ack_time); } - if let Some(idle_time) = self.idle_timeout.expiry(self.loss_recovery.raw_pto()) { - qtrace!([self], "Idle timer {:?}", idle_time); - delays.push(idle_time); - } + let pto = self.loss_recovery.pto_raw(PNSpace::ApplicationData); + let idle_time = self.idle_timeout.expiry(now, pto); + qtrace!([self], "Idle timer {:?}", idle_time); + delays.push(idle_time); if let Some(lr_time) = self.loss_recovery.next_timeout() { qtrace!([self], "Loss recovery timer {:?}", lr_time); @@ -950,10 +962,7 @@ impl Connection { } } - // Should always at least have idle timeout, once connected - assert!(!delays.is_empty()); let earliest = delays.into_iter().min().unwrap(); - // TODO(agrover, mt) - need to analyze and fix #47 // rather than just clamping to zero here. qdebug!( @@ -1009,15 +1018,17 @@ impl Connection { fn handle_retry(&mut self, packet: PublicPacket) -> Res<()> { qinfo!([self], "received Retry"); if self.retry_info.is_some() { - self.stats.pkt_dropped("Extra Retry"); + self.stats.borrow_mut().pkt_dropped("Extra Retry"); return Ok(()); } if packet.token().is_empty() { - self.stats.pkt_dropped("Retry without a token"); + self.stats.borrow_mut().pkt_dropped("Retry without a token"); return Ok(()); } if !packet.is_valid_retry(&self.remote_original_destination_cid.as_ref().unwrap()) { - self.stats.pkt_dropped("Retry with bad integrity tag"); + self.stats + .borrow_mut() + .pkt_dropped("Retry with bad integrity tag"); return Ok(()); } if let Some(p) = &mut self.path { @@ -1049,10 +1060,10 @@ impl Connection { Ok(()) } - fn discard_keys(&mut self, space: PNSpace) { + fn discard_keys(&mut self, space: PNSpace, now: Instant) { if self.crypto.discard(space) { qinfo!([self], "Drop packet number space {}", space); - self.loss_recovery.discard(space); + self.loss_recovery.discard(space, now); self.acks.drop_space(space); } } @@ -1147,21 +1158,21 @@ impl Connection { // Handle each packet in the datagram while !slc.is_empty() { + self.stats.borrow_mut().packets_rx += 1; let (packet, remainder) = match PublicPacket::decode(slc, self.cid_manager.borrow().as_decoder()) { Ok((packet, remainder)) => (packet, remainder), Err(e) => { qinfo!([self], "Garbage packet: {}", e); qtrace!([self], "Garbage packet contents: {}", hex(slc)); - self.stats.pkt_dropped("Garbage packet"); + self.stats.borrow_mut().pkt_dropped("Garbage packet"); break; } }; - self.stats.packets_rx += 1; match (packet.packet_type(), &self.state, &self.role) { (PacketType::Initial, State::Init, Role::Server) => { if !packet.is_valid_initial() { - self.stats.pkt_dropped("Invalid Initial"); + self.stats.borrow_mut().pkt_dropped("Invalid Initial"); break; } qinfo!( @@ -1192,7 +1203,7 @@ impl Connection { if versions.is_empty() || versions.contains(&self.quic_version.as_u32()) { // Ignore VersionNegotiation packets that contain the current version. - self.stats.dropped_rx += 1; + self.stats.borrow_mut().dropped_rx += 1; return Ok(frames); } self.set_state(State::Closed(ConnectionError::Transport( @@ -1201,7 +1212,7 @@ impl Connection { return Err(Error::VersionNegotiation); } Err(_) => { - self.stats.dropped_rx += 1; + self.stats.borrow_mut().dropped_rx += 1; return Ok(frames); } } @@ -1214,6 +1225,7 @@ impl Connection { | (PacketType::Retry, ..) | (PacketType::OtherVersion, ..) => { self.stats + .borrow_mut() .pkt_dropped(format!("{:?}", packet.packet_type())); break; } @@ -1222,19 +1234,22 @@ impl Connection { match self.state { State::Init => { - self.stats.pkt_dropped("Received while in Init state"); + self.stats + .borrow_mut() + .pkt_dropped("Received while in Init state"); break; } State::WaitInitial => {} State::Handshaking | State::Connected | State::Confirmed => { if !self.is_valid_cid(packet.dcid()) { self.stats + .borrow_mut() .pkt_dropped(format!("Ignoring packet with CID {:?}", packet.dcid())); break; } if self.role == Role::Server && packet.packet_type() == PacketType::Handshake { // Server has received a Handshake packet -> discard Initial keys and states - self.discard_keys(PNSpace::Initial); + self.discard_keys(PNSpace::Initial, now); } } State::Closing { .. } => { @@ -1245,14 +1260,16 @@ impl Connection { } State::Draining { .. } | State::Closed(..) => { // Do nothing. - self.stats.pkt_dropped(format!("State {:?}", self.state)); + self.stats + .borrow_mut() + .pkt_dropped(format!("State {:?}", self.state)); break; } } qtrace!([self], "Received unverified packet {:?}", packet); - let pto = self.loss_recovery.pto(); + let pto = self.loss_recovery.pto_raw(PNSpace::ApplicationData); match packet.decrypt(&mut self.crypto.states, now + pto) { Ok(payload) => { // TODO(ekr@rtfm.com): Have the server blow away the initial @@ -1267,7 +1284,7 @@ impl Connection { payload.pn(), &payload[..], ); - qlog::packet_received(&mut self.qlog, &payload); + qlog::packet_received(&mut self.qlog, &packet, &payload); let res = self.process_packet(&payload, now); if res.is_err() && self.path.is_none() { // We need to make a path for sending an error message. @@ -1290,7 +1307,7 @@ impl Connection { // If the state isn't available, or we can't decrypt the packet, drop // the rest of the datagram on the floor, but don't generate an error. self.check_stateless_reset(&d, slc, now)?; - self.stats.pkt_dropped("Decryption failure"); + self.stats.borrow_mut().pkt_dropped("Decryption failure"); qlog::packet_dropped(&mut self.qlog, &packet); } } @@ -1313,7 +1330,7 @@ impl Connection { let space = PNSpace::from(packet.packet_type()); if self.acks.get_mut(space).unwrap().is_duplicate(packet.pn()) { qdebug!([self], "Duplicate packet from {} pn={}", space, packet.pn()); - self.stats.dups_rx += 1; + self.stats.borrow_mut().dups_rx += 1; return Ok(vec![]); } @@ -1528,8 +1545,9 @@ impl Connection { ) -> (Vec, bool) { let mut tokens = Vec::new(); - let mut ack_eliciting = if profile.pto() { - // Add a PING on a PTO. This might get a more expedient ACK. + let mut ack_eliciting = if profile.should_probe(space) { + // Send PING in all spaces that allow a probe. + // This might get a more expedient ACK. builder.encode_varint(Frame::Ping.get_type()); true } else { @@ -1622,16 +1640,19 @@ impl Connection { } dump_packet(self, "TX ->", pt, pn, &builder[payload_start..]); - qlog::packet_sent(&mut self.qlog, pt, pn, &builder[payload_start..]); + qlog::packet_sent( + &mut self.qlog, + pt, + pn, + builder.len(), + &builder[payload_start..], + ); - self.stats.packets_tx += 1; + self.stats.borrow_mut().packets_tx += 1; encoder = builder.build(self.crypto.states.tx(*space).unwrap())?; debug_assert!(encoder.len() <= path.mtu()); - // Normal packets are in flight if they include PADDING frames, - // but we don't send those. - let in_flight = !profile.pto() && ack_eliciting; - if in_flight { + if ack_eliciting { self.idle_timeout.on_packet_sent(now); } let sent = SentPacket::new( @@ -1641,7 +1662,6 @@ impl Connection { ack_eliciting, Rc::new(tokens), encoder.len() - header_start, - in_flight, ); if pt == PacketType::Initial && self.role == Role::Client { // Packets containing Initial packets might need padding, and we want to @@ -1658,10 +1678,10 @@ impl Connection { if *space == PNSpace::Handshake { if self.role == Role::Client { // Client can send Handshake packets -> discard Initial keys and states - self.discard_keys(PNSpace::Initial); + self.discard_keys(PNSpace::Initial, now); } else if self.state == State::Confirmed { // We could discard handshake keys in set_state, but wait until after sending an ACK. - self.discard_keys(PNSpace::Handshake); + self.discard_keys(PNSpace::Handshake, now); } } } @@ -1719,7 +1739,7 @@ impl Connection { fn get_closing_period_time(&self, now: Instant) -> Instant { // Spec says close time should be at least PTO times 3. - now + (self.loss_recovery.pto() * 3) + now + (self.loss_recovery.pto_raw(PNSpace::ApplicationData) * 3) } /// Close the connection. @@ -1903,12 +1923,15 @@ impl Connection { qerror!("frame not allowed: {:?} {:?}", frame, ptype); return Err(Error::ProtocolViolation); } + let space = PNSpace::from(ptype); match frame { Frame::Padding => { // Ignore } Frame::Ping => { - // Ack elicited with no further handling needed + // If we get a PING and there are outstanding CRYPTO frames, + // prepare to resend them. + self.crypto.resend_unacked(space); } Frame::Ack { largest_acknowledged, @@ -1917,7 +1940,7 @@ impl Connection { ack_ranges, } => { self.handle_ack( - PNSpace::from(ptype), + space, largest_acknowledged, ack_delay, first_ack_range, @@ -1946,7 +1969,6 @@ impl Connection { } } Frame::Crypto { offset, data } => { - let space = PNSpace::from(ptype); qtrace!( [self], "Crypto frame on space={} offset={}, data={:0x?}", @@ -1960,6 +1982,9 @@ impl Connection { let read = self.crypto.streams.read_to_end(space, &mut buf); qdebug!("Read {} bytes", read); self.handshake(now, space, Some(&buf))?; + } else { + // If we get a useless CRYPTO frame send outstanding CRYPTO frames again. + self.crypto.resend_unacked(space); } } Frame::NewToken { token } => self.token = Some(token), @@ -2098,7 +2123,7 @@ impl Connection { return Err(Error::ProtocolViolation); } self.set_state(State::Confirmed); - self.discard_keys(PNSpace::Handshake); + self.discard_keys(PNSpace::Handshake, now); } }; @@ -2128,6 +2153,17 @@ impl Connection { } } + fn decode_ack_delay(&self, v: u64) -> Duration { + // If we have remote transport parameters, use them. + // Otherwise, ack delay should be zero (because it's the handshake). + if let Some(r) = self.tps.borrow().remote.as_ref() { + let exponent = u32::try_from(r.get_integer(tparams::ACK_DELAY_EXPONENT)).unwrap(); + Duration::from_micros(v.checked_shl(exponent).unwrap_or(u64::MAX)) + } else { + Duration::new(0, 0) + } + } + fn handle_ack( &mut self, space: PNSpace, @@ -2152,7 +2188,7 @@ impl Connection { space, largest_acknowledged, acked_ranges, - Duration::from_millis(ack_delay), + self.decode_ack_delay(ack_delay), now, ); for acked in acked_packets { @@ -2214,11 +2250,11 @@ impl Connection { } // Setting application keys has to occur after 0-RTT rejection. - let pto = self.loss_recovery.pto(); + let pto = self.loss_recovery.pto_raw(PNSpace::ApplicationData); self.crypto.install_application_keys(now + pto)?; self.process_tps()?; self.set_state(State::Connected); - self.stats.resumed = self.crypto.tls.info().unwrap().resumed(); + self.stats.borrow_mut().resumed = self.crypto.tls.info().unwrap().resumed(); if self.role == Role::Server { self.state_signaling.handshake_done(); self.set_state(State::Confirmed); @@ -2237,6 +2273,7 @@ impl Connection { self.recv_streams.clear(); } self.events.connection_state_change(state); + qlog::connection_state_updated(&mut self.qlog, &self.state) } else if mem::discriminant(&state) != mem::discriminant(&self.state) { // Only tolerate a regression in state if the new state is closing // and the connection is already closed. @@ -2346,6 +2383,8 @@ impl Connection { loop { let next_stream_id = next_stream_idx.to_stream_id(stream_id.stream_type(), stream_id.role()); + self.events.new_stream(next_stream_id); + self.recv_streams.insert( next_stream_id, RecvStream::new( @@ -2356,9 +2395,7 @@ impl Connection { ), ); - if next_stream_id.is_uni() { - self.events.new_stream(next_stream_id); - } else { + if next_stream_id.is_bidi() { // From the local perspective, this is a remote- originated BiDi stream. // From the remote perspective, this is a local-originated BiDi stream. // Therefore, look at the remote's transport parameters for the @@ -2378,7 +2415,6 @@ impl Connection { self.events.clone(), ), ); - self.events.new_stream(next_stream_id); } *next_stream_idx += 1; @@ -2620,7 +2656,7 @@ impl ::std::fmt::Display for Connection { mod tests { use super::*; use crate::cc::PACING_BURST_SIZE; - use crate::cc::{INITIAL_CWND_PKTS, MIN_CONG_WINDOW}; + use crate::cc::{INITIAL_CWND_PKTS, MAX_DATAGRAM_SIZE, MIN_CONG_WINDOW}; use crate::frame::{CloseError, StreamType}; use crate::packet::PACKET_BIT_LONG; use crate::path::PATH_MTU_V6; @@ -2904,7 +2940,7 @@ mod tests { let out = server.process(Some(d), now()); assert_eq!( out.as_dgram_ref().is_some(), - (d_num as u64 + 1) % (MAX_UNACKED_PKTS + 1) == 0 + (d_num + 1) % (MAX_UNACKED_PKTS + 1) == 0 ); qdebug!("Output={:0x?}", out.as_dgram_ref()); } @@ -2955,6 +2991,7 @@ mod tests { let output = a.process(input, now).dgram(); assert!(had_input || output.is_some()); input = output; + qtrace!("t += {:?}", rtt / 2); now += rtt / 2; mem::swap(&mut a, &mut b); } @@ -3994,6 +4031,7 @@ mod tests { #[test] fn pto_initial() { + const INITIAL_PTO: Duration = Duration::from_millis(300); let mut now = now(); qdebug!("---- client: generate CH"); @@ -4003,7 +4041,7 @@ mod tests { assert_eq!(pkt1.clone().unwrap().len(), PATH_MTU_V6); let delay = client.process(None, now).callback(); - assert_eq!(delay, Duration::from_millis(300)); + assert_eq!(delay, INITIAL_PTO); // Resend initial after PTO. now += delay; @@ -4017,7 +4055,7 @@ mod tests { let delay = client.process(None, now).callback(); // PTO has doubled. - assert_eq!(delay, Duration::from_millis(600)); + assert_eq!(delay, INITIAL_PTO * 2); // Server process the first initial pkt. let mut server = default_server(); @@ -4028,18 +4066,20 @@ mod tests { // After the handshake packet the initial keys and the crypto stream for the initial // packet number space will be discarded. // Here only an ack for the Handshake packet will be sent. - now += Duration::from_millis(10); let out = client.process(out, now).dgram(); assert!(out.is_some()); - // We do not have PTO for the resent initial packet any more, because keys are discarded. - // The timeout will be an idle time out of LOCAL_IDLE_TIMEOUT seconds. - let out = client.process(None, now); - assert_eq!(out, Output::Callback(LOCAL_IDLE_TIMEOUT)); + // We do not have PTO for the resent initial packet any more, but + // the Handshake PTO timer should be armed. As the RTT is apparently + // the same as the initial PTO value, and there is only one sample, + // the PTO will be 3x the INITIAL PTO. + let delay = client.process(None, now).callback(); + assert_eq!(delay, INITIAL_PTO * 3); } + /// A complete handshake that involves a PTO in the Handshake space. #[test] - fn pto_handshake() { + fn pto_handshake_complete() { let mut now = now(); // start handshake let mut client = default_client(); @@ -4056,7 +4096,9 @@ mod tests { let pkt = client.process(pkt, now).dgram(); let cb = client.process(None, now).callback(); - assert_eq!(cb, LOCAL_IDLE_TIMEOUT); + // The client now has a single RTT estimate (20ms), so + // the handshake PTO is set based on that. + assert_eq!(cb, Duration::from_millis(60)); now += Duration::from_millis(10); let pkt = server.process(pkt, now).dgram(); @@ -4092,18 +4134,17 @@ mod tests { let pkt = server.process(pkt1, now).dgram(); assert!(pkt.is_some()); - // Check that the PTO packets (pkt2, pkt3) have a Handshake and an app pn space packet. - // The server has discarded the Handshake keys already, therefore the handshake packet - // will be dropped. + // Check that the PTO packets (pkt2, pkt3) are Handshake packets. + // The server discarded the Handshake keys already, therefore they are dropped. let dropped_before = server.stats().dropped_rx; let frames = server.test_process_input(pkt2.unwrap(), now); assert_eq!(1, server.stats().dropped_rx - dropped_before); - assert_eq!(frames[0], (Frame::Ping, PNSpace::ApplicationData)); + assert!(frames.is_empty()); let dropped_before = server.stats().dropped_rx; let frames = server.test_process_input(pkt3.unwrap(), now); assert_eq!(1, server.stats().dropped_rx - dropped_before); - assert_eq!(frames[0], (Frame::Ping, PNSpace::ApplicationData)); + assert!(frames.is_empty()); now += Duration::from_millis(10); // Client receive ack for the first packet @@ -4122,8 +4163,9 @@ mod tests { assert_eq!(cb, LOCAL_IDLE_TIMEOUT - ACK_DELAY); } + /// Test that PTO in the Handshake space contains the right frames. #[test] - fn test_pto_handshake_and_app_data() { + fn pto_handshake_frames() { let mut now = now(); qdebug!("---- client: generate CH"); let mut client = default_client(); @@ -4154,7 +4196,7 @@ mod tests { let out = client.process(None, now); assert_eq!(out, Output::Callback(Duration::from_millis(60))); - // Wait for PTO to expire and resend a handshake and 1rtt packet + // Wait for PTO to expire and resend a handshake packet. now += Duration::from_millis(60); let pkt2 = client.process(None, now).dgram(); assert!(pkt2.is_some()); @@ -4162,96 +4204,65 @@ mod tests { now += Duration::from_millis(10); let frames = server.test_process_input(pkt2.unwrap(), now); - assert!(matches!(frames[0], (Frame::Ping, PNSpace::Handshake))); + assert_eq!(frames.len(), 2); + assert_eq!(frames[0], (Frame::Ping, PNSpace::Handshake)); assert!(matches!( frames[1], (Frame::Crypto { .. }, PNSpace::Handshake) )); - assert!(matches!(frames[2], (Frame::Ping, PNSpace::ApplicationData))); - assert!(matches!( - frames[3], - (Frame::Stream { .. }, PNSpace::ApplicationData) - )); } + /// In the case that the Handshake takes too many packets, the server might + /// be stalled on the anti-amplification limit. If a Handshake ACK from the + /// client is lost, the client has to keep the PTO timer armed or the server + /// might be unable to send anything, causing a deadlock. #[test] - fn pto_count_increase_across_spaces() { + fn handshake_ack_pto() { + const RTT: Duration = Duration::from_millis(10); let mut now = now(); - qdebug!("---- client: generate CH"); let mut client = default_client(); - let pkt = client.process(None, now).dgram(); - - now += Duration::from_millis(10); - qdebug!("---- server: CH -> SH, EE, CERT, CV, FIN"); let mut server = default_server(); - let pkt = server.process(pkt, now).dgram(); + // This is a greasing transport parameter, and large enough that the + // server needs to send two Handshake packets. + let big = TransportParameter::Bytes(vec![0; PATH_MTU_V6]); + server.set_local_tparam(0xce16, big).unwrap(); - now += Duration::from_millis(10); - qdebug!("---- client: cert verification"); - let pkt = client.process(pkt, now).dgram(); + let c1 = client.process(None, now).dgram(); - now += Duration::from_millis(10); - let _pkt = server.process(pkt, now); + now += RTT / 2; + let s1 = server.process(c1, now).dgram(); + assert!(s1.is_some()); + let s2 = server.process(None, now).dgram(); + assert!(s1.is_some()); - now += Duration::from_millis(10); - client.authenticated(AuthenticationStatus::Ok, now); + // Now let the client have the Initial, but drop the first coalesced Handshake packet. + now += RTT / 2; + let (initial, _) = split_datagram(s1.unwrap()); + client.process_input(initial, now); + let c2 = client.process(s2, now).dgram(); + assert!(c2.is_some()); // This is an ACK. Drop it. + let delay = client.process(None, now).callback(); + assert_eq!(delay, RTT * 3); - qdebug!("---- client: SH..FIN -> FIN"); - let pkt1 = client.process(None, now).dgram(); - assert!(pkt1.is_some()); - // Get PTO timer. - let out = client.process(None, now); - assert_eq!(out, Output::Callback(Duration::from_millis(60))); + // Wait for the PTO and ensure that the client generates a packet. + now += delay; + let c3 = client.process(None, now).dgram(); + assert!(c3.is_some()); - now += Duration::from_millis(10); - assert_eq!(client.stream_create(StreamType::UniDi).unwrap(), 2); - assert_eq!(client.stream_send(2, b"zero").unwrap(), 4); - qdebug!("---- client: 1RTT packet"); - let pkt2 = client.process(None, now).dgram(); - assert!(pkt2.is_some()); + now += RTT / 2; + let frames = server.test_process_input(c3.unwrap(), now); + assert_eq!(frames, vec![(Frame::Ping, PNSpace::Handshake)]); - // Get PTO timer. It is the timer for pkt1(handshake pn space). - let out = client.process(None, now); - assert_eq!(out, Output::Callback(Duration::from_millis(50))); - - // Wait for PTO to expire and resend a handshake and 1rtt packet - now += Duration::from_millis(50); - let pkt3 = client.process(None, now).dgram(); - assert!(pkt3.is_some()); - let pkt4 = client.process(None, now).dgram(); - assert!(pkt4.is_some()); - - // Get PTO timer. It is the timer for pkt2(app pn space). PTO has been doubled. - // pkt2 has been sent 50ms ago (50 + 120 = 170 == 2*85) - let out = client.process(None, now); - assert_eq!(out, Output::Callback(Duration::from_millis(120))); - - // Wait for PTO to expire and resend a handshake and 1rtt packet - now += Duration::from_millis(120); - let pkt5 = client.process(None, now).dgram(); - assert!(pkt5.is_some()); - - // Now check what the server receives. - let assert_hs_and_app_pto = |frames: &[(Frame, PNSpace)]| { - assert!(matches!(frames[0], (Frame::Ping, PNSpace::Handshake))); - assert!(matches!( - frames[1], - (Frame::Crypto { .. }, PNSpace::Handshake) - )); - assert!(matches!(frames[2], (Frame::Ping, PNSpace::ApplicationData))); - assert!(matches!( - frames[3], - (Frame::Stream { .. }, PNSpace::ApplicationData) - )); - }; - - now += Duration::from_millis(10); - let frames = server.test_process_input(pkt3.unwrap(), now); - assert_hs_and_app_pto(&frames); - - now += Duration::from_millis(10); - let frames = server.test_process_input(pkt5.unwrap(), now); - assert_hs_and_app_pto(&frames); + // Now complete the handshake as cheaply as possible. + let dgram = server.process(None, now).dgram(); + client.process_input(dgram.unwrap(), now); + maybe_authenticate(&mut client); + let dgram = client.process(None, now).dgram(); + assert_eq!(*client.state(), State::Connected); + let dgram = server.process(dgram, now).dgram(); + assert_eq!(*server.state(), State::Confirmed); + client.process_input(dgram.unwrap(), now); + assert_eq!(*client.state(), State::Confirmed); } #[test] @@ -4322,21 +4333,28 @@ mod tests { } // Receive multiple packets and generate an ack-only packet. - fn ack_bytes( + fn ack_bytes( dest: &mut Connection, stream: u64, - in_dgrams: Vec, + in_dgrams: D, now: Instant, - ) -> (Vec, Vec) { + ) -> (Vec, Vec) + where + D: IntoIterator, + D::IntoIter: ExactSizeIterator, + { let mut srv_buf = [0; 4_096]; let mut recvd_frames = Vec::new(); + let in_dgrams = in_dgrams.into_iter(); + qdebug!([dest], "ack_bytes {} datagrams", in_dgrams.len()); for dgram in in_dgrams { recvd_frames.extend(dest.test_process_input(dgram, now)); } loop { let (bytes_read, _fin) = dest.stream_recv(stream, &mut srv_buf).unwrap(); + qtrace!([dest], "ack_bytes read {} bytes", bytes_read); if bytes_read == 0 { break; } @@ -4555,33 +4573,44 @@ mod tests { } // Should be in CARP now. - let cwnd1 = client.loss_recovery.cwnd(); now += Duration::from_millis(10); // Time passes. CARP -> CA - // Client: Send more data - let (mut c_tx_dgrams, next_now) = fill_cwnd(&mut client, 0, now); - now = next_now; + // Now make sure that we increase congestion window according to the + // accurate byte counting version of congestion avoidance. + // Check over several increases to be sure. + let mut expected_cwnd = client.loss_recovery.cwnd(); + for i in 0..5 { + println!("{}", i); + // Client: Send more data + let (mut c_tx_dgrams, next_now) = fill_cwnd(&mut client, 0, now); + now = next_now; - // Only sent 2 packets, to generate an ack but also keep cwnd increase - // small - c_tx_dgrams.truncate(2); + let c_tx_size: usize = c_tx_dgrams.iter().map(|d| d.len()).sum(); + println!( + "client sending {} bytes into cwnd of {}", + c_tx_size, + client.loss_recovery.cwnd() + ); + assert_eq!(c_tx_size, expected_cwnd); - // Generate ACK - let (s_tx_dgram, _) = ack_bytes(&mut server, 0, c_tx_dgrams, now); - - for dgram in s_tx_dgram { - client.test_process_input(dgram, now); + // Until we process all the packets, the congestion window remains the same. + // Note that we need the client to process ACK frames in stages, so split the + // datagrams into two, ensuring that we allow for an ACK for each batch. + let most = c_tx_dgrams.len() - usize::try_from(MAX_UNACKED_PKTS + 1).unwrap(); + let (s_tx_dgram, _) = ack_bytes(&mut server, 0, c_tx_dgrams.drain(..most), now); + for dgram in s_tx_dgram { + assert_eq!(client.loss_recovery.cwnd(), expected_cwnd); + client.process_input(dgram, now); + } + let (s_tx_dgram, _) = ack_bytes(&mut server, 0, c_tx_dgrams, now); + for dgram in s_tx_dgram { + assert_eq!(client.loss_recovery.cwnd(), expected_cwnd); + client.process_input(dgram, now); + } + expected_cwnd += MAX_DATAGRAM_SIZE; + assert_eq!(client.loss_recovery.cwnd(), expected_cwnd); } - - // ACK of pkts sent after start of recovery period should have caused - // exit from recovery period to just regular congestion avoidance. cwnd - // should now be a little higher but not as high as acked pkts during - // slow-start would cause it to be. - let cwnd2 = client.loss_recovery.cwnd(); - - assert!(cwnd2 > cwnd1); - assert!(cwnd2 < cwnd1 + 500); } fn induce_persistent_congestion( @@ -4593,16 +4622,19 @@ mod tests { // timer. This is rather brittle. now += AT_LEAST_PTO; + qtrace!([client], "first PTO"); let (c_tx_dgrams, next_now) = fill_cwnd(client, 0, now); now = next_now; assert_eq!(c_tx_dgrams.len(), 2); // Two PTO packets - now += Duration::from_secs(2); + qtrace!([client], "second PTO"); + now += AT_LEAST_PTO * 2; let (c_tx_dgrams, next_now) = fill_cwnd(client, 0, now); now = next_now; assert_eq!(c_tx_dgrams.len(), 2); // Two PTO packets - now += Duration::from_secs(4); + qtrace!([client], "third PTO"); + now += AT_LEAST_PTO * 4; let (c_tx_dgrams, next_now) = fill_cwnd(client, 0, now); now = next_now; assert_eq!(c_tx_dgrams.len(), 2); // Two PTO packets @@ -4610,9 +4642,9 @@ mod tests { // Generate ACK let (s_tx_dgram, _) = ack_bytes(server, 0, c_tx_dgrams, now); - // In PC now. + // An ACK for the third PTO causes persistent congestion. for dgram in s_tx_dgram { - client.test_process_input(dgram, now); + client.process_input(dgram, now); } assert_eq!(client.loss_recovery.cwnd(), MIN_CONG_WINDOW); @@ -4662,7 +4694,7 @@ mod tests { now += Duration::from_millis(100); for dgram in s_tx_dgram { - client.test_process_input(dgram, now); + client.process_input(dgram, now); } // send bytes that will be lost @@ -4722,12 +4754,12 @@ mod tests { // Make sure to flush any saved datagrams before doing this. let _ = peer.process_output(now()); - let dropped_before = peer.stats.dropped_rx; - let dups_before = peer.stats.dups_rx; + let before = peer.stats(); let out = peer.process(Some(pkt), now()); assert!(out.as_dgram_ref().is_none()); - assert_eq!(dropped, peer.stats.dropped_rx - dropped_before); - assert_eq!(dups, peer.stats.dups_rx - dups_before); + let after = peer.stats(); + assert_eq!(dropped, after.dropped_rx - before.dropped_rx); + assert_eq!(dups, after.dups_rx - before.dups_rx); } #[test] @@ -4784,6 +4816,7 @@ mod tests { let stream_id = sender.stream_create(StreamType::UniDi).unwrap(); assert!(sender.stream_send(stream_id, DEFAULT_STREAM_DATA).is_ok()); assert!(sender.stream_close_send(stream_id).is_ok()); + qdebug!([sender], "send_something on {}", stream_id); let dgram = sender.process(None, now).dgram(); dgram.expect("should have something to send") } @@ -5439,18 +5472,17 @@ mod tests { let c_hs4 = client.process(s_hs3, now + (INCR * 3)).dgram(); assert!(c_hs4.is_some()); // This will be acknowledged. - // Get an ACK for the client. + // Process c_hs2 and c_hs4, but skip c_hs3. + // Then get an ACK for the client. now += RTT / 2; - // Deliver the last one first, so that gets acknowledged. - // This won't generate an ACK, because it only contains an ACK. - let s_ack1 = server.process(c_hs4, now).dgram(); - assert!(s_ack1.is_none()); + // Deliver c_hs4 first, but don't generate a packet. + server.process_input(c_hs4.unwrap(), now); + let s_ack = server.process(c_hs2, now).dgram(); + assert!(s_ack.is_some()); // This includes an ACK, but it also includes HANDSHAKE_DONE, - // which we need to remove because that will cause the Handshake loss recovery - // state to be dropped. - let s_ack2 = server.process(c_hs2, now).dgram(); - assert!(s_ack2.is_some()); - let (s_hs_ack, _s_ap_ack) = split_datagram(s_ack2.unwrap()); + // which we need to remove because that will cause the Handshake loss + // recovery state to be dropped. + let (s_hs_ack, _s_ap_ack) = split_datagram(s_ack.unwrap()); // Now the client should start its loss recovery timer based on the ACK. now += RTT / 2; @@ -5596,17 +5628,17 @@ mod tests { // The client should process the datagram. It can't process the 1-RTT // packet until authentication completes though. So it saves it. now += RTT / 2; - assert_eq!(client.stats.dropped_rx, 0); + assert_eq!(client.stats().dropped_rx, 0); let _ = client.process(s2, now).dgram(); // This packet will contain an ACK, but we can ignore it. - assert_eq!(client.stats.dropped_rx, 0); + assert_eq!(client.stats().dropped_rx, 0); assert!(client.saved_datagram.is_some()); // After (successful) authentication, the packet is processed. maybe_authenticate(&mut client); let c3 = client.process(None, now).dgram(); assert!(c3.is_some()); - assert_eq!(client.stats.dropped_rx, 0); + assert_eq!(client.stats().dropped_rx, 0); assert!(client.saved_datagram.is_none()); // Allow the handshake to complete. @@ -5800,4 +5832,25 @@ mod tests { if *maximum_stream_data == RECV_BUFFER_SIZE as u64) )); } + + #[test] + fn corrupted_initial() { + let mut client = default_client(); + let mut server = default_server(); + let d = client.process(None, now()).dgram().unwrap(); + let mut corrupted = Vec::from(&d[..]); + // Find the last non-zero value and corrupt that. + let (idx, _) = corrupted + .iter() + .enumerate() + .rev() + .find(|(_, &v)| v != 0) + .unwrap(); + corrupted[idx] ^= 0x76; + let dgram = Datagram::new(d.source(), d.destination(), corrupted); + server.process_input(dgram, now()); + // The server should have received some packets, but dropped all of them. + assert_ne!(server.stats().packets_rx, 0); + assert_eq!(server.stats().packets_rx, server.stats().dropped_rx); + } } diff --git a/third_party/rust/neqo-transport/src/crypto.rs b/third_party/rust/neqo-transport/src/crypto.rs index 26d82e681178..934333561154 100644 --- a/third_party/rust/neqo-transport/src/crypto.rs +++ b/third_party/rust/neqo-transport/src/crypto.rs @@ -235,6 +235,12 @@ impl Crypto { self.streams.lost(token); } + /// Mark any outstanding frames in the indicated space as "lost" so + /// that they can be sent again. + pub fn resend_unacked(&mut self, space: PNSpace) { + self.streams.resend_unacked(space); + } + /// Discard state for a packet number space and return true /// if something was discarded. pub fn discard(&mut self, space: PNSpace) -> bool { @@ -274,6 +280,8 @@ pub struct CryptoDxState { } impl CryptoDxState { + #[allow(clippy::unknown_clippy_lints)] // Until we require rust 1.45. + #[allow(clippy::reversed_empty_ranges)] // To initialize an empty range. pub fn new( direction: CryptoDxDirection, epoch: Epoch, @@ -996,13 +1004,23 @@ impl CryptoStreams { self.get_mut(token.space) .unwrap() .tx - .mark_as_acked(token.offset, token.length) + .mark_as_acked(token.offset, token.length); } pub fn lost(&mut self, token: &CryptoRecoveryToken) { // See BZ 1624800, ignore lost packets in spaces we've dropped keys if let Some(cs) = self.get_mut(token.space) { - cs.tx.mark_as_lost(token.offset, token.length) + cs.tx.mark_as_lost(token.offset, token.length); + } + } + + /// Resend any Initial or Handshake CRYPTO frames that might be outstanding. + /// This can help speed up handshake times. + pub fn resend_unacked(&mut self, space: PNSpace) { + if space != PNSpace::ApplicationData { + if let Some(cs) = self.get_mut(space) { + cs.tx.unmark_sent(); + } } } diff --git a/third_party/rust/neqo-transport/src/events.rs b/third_party/rust/neqo-transport/src/events.rs index 211fc50d5c63..3ff61a4f3484 100644 --- a/third_party/rust/neqo-transport/src/events.rs +++ b/third_party/rust/neqo-transport/src/events.rs @@ -133,30 +133,25 @@ impl ConnectionEvents { self.events.borrow_mut().pop_front() } - #[allow(clippy::block_in_if_condition_stmt)] fn insert(&self, event: ConnectionEvent) { let mut q = self.events.borrow_mut(); // Special-case two enums that are not strictly PartialEq equal but that // we wish to avoid inserting duplicates. - if match &event { + let already_present = match &event { ConnectionEvent::SendStreamStopSending { stream_id, .. } => q.iter().any(|evt| { - matches!( - evt, ConnectionEvent::SendStreamStopSending { stream_id: x, .. } - if *x == *stream_id) + matches!(evt, ConnectionEvent::SendStreamStopSending { stream_id: x, .. } + if *x == *stream_id) }), ConnectionEvent::RecvStreamReset { stream_id, .. } => q.iter().any(|evt| { - matches!( - evt, ConnectionEvent::RecvStreamReset { stream_id: x, .. } - if *x == *stream_id) + matches!(evt, ConnectionEvent::RecvStreamReset { stream_id: x, .. } + if *x == *stream_id) }), _ => q.contains(&event), - } { - // Already in event list. - return; + }; + if !already_present { + q.push_back(event); } - - q.push_back(event); } fn remove(&self, f: F) diff --git a/third_party/rust/neqo-transport/src/frame.rs b/third_party/rust/neqo-transport/src/frame.rs index bb9d4922b785..065e12d024df 100644 --- a/third_party/rust/neqo-transport/src/frame.rs +++ b/third_party/rust/neqo-transport/src/frame.rs @@ -15,6 +15,7 @@ use crate::{AppError, ConnectionError, Error, Res, TransportError, ERROR_APPLICA use std::cmp::{min, Ordering}; use std::convert::TryFrom; +use std::ops::RangeInclusive; #[allow(clippy::module_name_repetitions)] pub type FrameType = u64; @@ -478,13 +479,13 @@ impl Frame { largest_acked: u64, first_ack_range: u64, ack_ranges: &[AckRange], - ) -> Res> { - let mut acked_ranges = Vec::new(); + ) -> Res>> { + let mut acked_ranges = Vec::with_capacity(ack_ranges.len() + 1); if largest_acked < first_ack_range { return Err(Error::FrameEncodingError); } - acked_ranges.push((largest_acked, largest_acked - first_ack_range)); + acked_ranges.push((largest_acked - first_ack_range)..=largest_acked); if !ack_ranges.is_empty() && largest_acked < first_ack_range + 1 { return Err(Error::FrameEncodingError); } @@ -502,7 +503,7 @@ impl Frame { if cur < r.range { return Err(Error::FrameEncodingError); } - acked_ranges.push((cur, cur - r.range)); + acked_ranges.push((cur - r.range)..=cur); if cur > r.range + 1 { cur -= r.range + 1; @@ -1055,7 +1056,7 @@ mod tests { fn test_decode_ack_frame() { let res = Frame::decode_ack_frame(7, 2, &[AckRange { gap: 0, range: 3 }]); assert!(res.is_ok()); - assert_eq!(res.unwrap(), vec![(7, 5), (3, 0)]); + assert_eq!(res.unwrap(), vec![5..=7, 0..=3]); } #[test] diff --git a/third_party/rust/neqo-transport/src/lib.rs b/third_party/rust/neqo-transport/src/lib.rs index 8b373cab7de3..c81b9c707578 100644 --- a/third_party/rust/neqo-transport/src/lib.rs +++ b/third_party/rust/neqo-transport/src/lib.rs @@ -36,6 +36,7 @@ pub use self::events::{ConnectionEvent, ConnectionEvents}; pub use self::frame::CloseError; pub use self::frame::StreamType; pub use self::packet::QuicVersion; +pub use self::stats::Stats; pub use self::stream_id::StreamId; const LOCAL_IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30); // 30 second diff --git a/third_party/rust/neqo-transport/src/pace.rs b/third_party/rust/neqo-transport/src/pace.rs index 25e4d6bebe67..624fb8622e6d 100644 --- a/third_party/rust/neqo-transport/src/pace.rs +++ b/third_party/rust/neqo-transport/src/pace.rs @@ -58,7 +58,7 @@ impl Pacer { /// the current time is). pub fn next(&self, rtt: Duration, cwnd: usize) -> Instant { if self.c >= self.p { - qtrace!([self], "next {}/{:?} no wait: {:?}", cwnd, rtt, self.t); + qtrace!([self], "next {}/{:?} no wait = {:?}", cwnd, rtt, self.t); self.t } else { // This is the inverse of the function in `spend`: @@ -66,16 +66,10 @@ impl Pacer { let r = rtt.as_nanos(); let d = r.saturating_mul(u128::try_from(self.p - self.c).unwrap()); let add = d / u128::try_from(cwnd * PACER_SPEEDUP).unwrap(); - let dt = u64::try_from(add).map(Duration::from_nanos).unwrap_or(rtt); - qtrace!( - [self], - "next {}/{:?} wait {:?}: {:?}", - cwnd, - rtt, - dt, - self.t + dt - ); - self.t + dt + let w = u64::try_from(add).map(Duration::from_nanos).unwrap_or(rtt); + let nxt = self.t + w; + qtrace!([self], "next {}/{:?} wait {:?} = {:?}", cwnd, rtt, w, nxt); + nxt } } diff --git a/third_party/rust/neqo-transport/src/packet/mod.rs b/third_party/rust/neqo-transport/src/packet/mod.rs index 4319d95ea884..0707ac37d9d2 100644 --- a/third_party/rust/neqo-transport/src/packet/mod.rs +++ b/third_party/rust/neqo-transport/src/packet/mod.rs @@ -123,6 +123,8 @@ pub struct PacketBuilder { impl PacketBuilder { /// Start building a short header packet. + #[allow(clippy::unknown_clippy_lints)] // Until we require rust 1.45. + #[allow(clippy::reversed_empty_ranges)] pub fn short(mut encoder: Encoder, key_phase: bool, dcid: &ConnectionId) -> Self { let header_start = encoder.len(); // TODO(mt) randomize the spin bit @@ -143,6 +145,8 @@ impl PacketBuilder { /// Start building a long header packet. /// For an Initial packet you will need to call initial_token(), /// even if the token is empty. + #[allow(clippy::unknown_clippy_lints)] // Until we require rust 1.45. + #[allow(clippy::reversed_empty_ranges)] // For initializing an empty range. pub fn long( mut encoder: Encoder, pt: PacketType, diff --git a/third_party/rust/neqo-transport/src/qlog.rs b/third_party/rust/neqo-transport/src/qlog.rs index 58a80d62e2eb..a654af932a8a 100644 --- a/third_party/rust/neqo-transport/src/qlog.rs +++ b/third_party/rust/neqo-transport/src/qlog.rs @@ -7,6 +7,7 @@ // Functions that handle capturing QLOG traces. use std::convert::TryFrom; +use std::ops::RangeInclusive; use std::string::String; use std::time::Duration; @@ -14,6 +15,7 @@ use qlog::{self, event::Event, PacketHeader, QuicFrame}; use neqo_common::{hex, qinfo, qlog::NeqoQlog, Decoder}; +use crate::connection::State; use crate::frame::{self, Frame}; use crate::packet::{DecryptedPacket, PacketNumber, PacketType, PublicPacket}; use crate::path::Path; @@ -109,13 +111,41 @@ fn connection_started(qlog: &mut NeqoQlog, path: &Path) { }) } -pub fn packet_sent(qlog: &mut NeqoQlog, pt: PacketType, pn: PacketNumber, body: &[u8]) { +pub fn connection_state_updated(qlog: &mut NeqoQlog, new: &State) { + qlog.add_event(|| { + Some(Event::connection_state_updated_min(match new { + State::Init => qlog::ConnectionState::Attempted, + State::WaitInitial => qlog::ConnectionState::Attempted, + State::Handshaking => qlog::ConnectionState::Handshake, + State::Connected => qlog::ConnectionState::Active, + State::Confirmed => qlog::ConnectionState::Active, + State::Closing { .. } => qlog::ConnectionState::Draining, + State::Draining { .. } => qlog::ConnectionState::Draining, + State::Closed { .. } => qlog::ConnectionState::Closed, + })) + }) +} + +pub fn packet_sent( + qlog: &mut NeqoQlog, + pt: PacketType, + pn: PacketNumber, + plen: usize, + body: &[u8], +) { qlog.add_event_with_stream(|stream| { let mut d = Decoder::from(body); stream.add_event(Event::packet_sent_min( to_qlog_pkt_type(pt), - PacketHeader::new(pn, None, None, None, None, None), + PacketHeader::new( + pn, + Some(u64::try_from(plen).unwrap()), + None, + None, + None, + None, + ), Some(Vec::new()), ))?; @@ -160,13 +190,24 @@ pub fn packets_lost(qlog: &mut NeqoQlog, pkts: &[SentPacket]) { }) } -pub fn packet_received(qlog: &mut NeqoQlog, payload: &DecryptedPacket) { +pub fn packet_received( + qlog: &mut NeqoQlog, + public_packet: &PublicPacket, + payload: &DecryptedPacket, +) { qlog.add_event_with_stream(|stream| { let mut d = Decoder::from(&payload[..]); stream.add_event(Event::packet_received( to_qlog_pkt_type(payload.packet_type()), - PacketHeader::new(payload.pn(), None, None, None, None, None), + PacketHeader::new( + payload.pn(), + Some(u64::try_from(public_packet.packet_len()).unwrap()), + None, + None, + None, + None, + ), Some(Vec::new()), None, None, @@ -310,10 +351,20 @@ fn frame_to_qlogframe(frame: &Frame) -> QuicFrame { first_ack_range, ack_ranges, } => { - let ack_ranges = + let ranges = Frame::decode_ack_frame(*largest_acknowledged, *first_ack_range, ack_ranges).ok(); - QuicFrame::ack(Some(ack_delay.to_string()), ack_ranges, None, None, None) + QuicFrame::ack( + Some(ack_delay.to_string()), + ranges.map(|all| { + all.into_iter() + .map(RangeInclusive::into_inner) + .collect::>() + }), + None, + None, + None, + ) } Frame::ResetStream { stream_id, diff --git a/third_party/rust/neqo-transport/src/recovery.rs b/third_party/rust/neqo-transport/src/recovery.rs index 47da8aca3b57..a578f32c099a 100644 --- a/third_party/rust/neqo-transport/src/recovery.rs +++ b/third_party/rust/neqo-transport/src/recovery.rs @@ -10,21 +10,26 @@ use std::cmp::{max, min}; use std::collections::BTreeMap; +use std::mem; +use std::ops::RangeInclusive; use std::time::{Duration, Instant}; use smallvec::{smallvec, SmallVec}; -use neqo_common::{qdebug, qinfo, qlog::NeqoQlog, qtrace, qwarn}; +use neqo_common::{qdebug, qinfo, qlog::NeqoQlog, qtrace}; use crate::cc::CongestionControl; use crate::crypto::CryptoRecoveryToken; use crate::flow_mgr::FlowControlRecoveryToken; use crate::qlog::{self, QlogMetric}; use crate::send_stream::StreamRecoveryToken; -use crate::tracking::{AckToken, PNSpace, SentPacket}; +use crate::stats::{Stats, StatsCell}; +use crate::tracking::{AckToken, PNSpace, PNSpaceSet, SentPacket}; use crate::LOCAL_IDLE_TIMEOUT; pub const GRANULARITY: Duration = Duration::from_millis(20); +/// The default value for the maximum time a peer can delay acknowledgment +/// of an ack-eliciting packet. pub const MAX_ACK_DELAY: Duration = Duration::from_millis(25); // Defined in -recovery 6.2 as 333ms but using lower value. const INITIAL_RTT: Duration = Duration::from_millis(100); @@ -96,6 +101,12 @@ impl RttVals { self.smoothed_rtt = (self.smoothed_rtt * 7 + rtt_sample) / 8; } self.samples += 1; + qtrace!( + "RTT latest={:?} -> estimate={:?}~{:?}", + self.latest_rtt, + self.smoothed_rtt, + self.rttvar + ); qlog::metrics_updated( &mut qlog, &[ @@ -139,6 +150,7 @@ impl Default for RttVals { pub struct SendProfile { limit: usize, pto: Option, + probe: PNSpaceSet, paced: bool, } @@ -150,6 +162,7 @@ impl SendProfile { Self { limit: max(ACK_ONLY_SIZE_LIMIT - 1, limit), pto: None, + probe: PNSpaceSet::default(), paced: false, } } @@ -159,27 +172,33 @@ impl SendProfile { Self { limit: ACK_ONLY_SIZE_LIMIT - 1, pto: None, + probe: PNSpaceSet::default(), paced: true, } } - pub fn new_pto(pn_space: PNSpace, mtu: usize) -> Self { + pub fn new_pto(pn_space: PNSpace, mtu: usize, probe: PNSpaceSet) -> Self { debug_assert!(mtu > ACK_ONLY_SIZE_LIMIT); + debug_assert!(probe[pn_space]); Self { limit: mtu, pto: Some(pn_space), + probe, paced: false, } } - pub fn pto(&self) -> bool { - self.pto.is_some() + /// Whether probing this space is helpful. This isn't necessarily the space + /// that caused the timer to pop, but it is helpful to send a PING in a space + /// that has the PTO timer armed. + pub fn should_probe(&self, space: PNSpace) -> bool { + self.probe[space] } /// Determine whether an ACK-only packet should be sent for the given packet /// number space. /// Send only ACKs either: when the space available is too small, or when a PTO - /// exists for a later packet number space (which could use extra space for data). + /// exists for a later packet number space (which should get the most space). pub fn ack_only(&self, space: PNSpace) -> bool { self.limit < ACK_ONLY_SIZE_LIMIT || self.pto.map_or(false, |sp| space < sp) } @@ -261,52 +280,92 @@ impl LossRecoverySpace { if self.in_flight_outstanding() { debug_assert!(self.pto_base_time.is_some()); self.pto_base_time - } else { + } else if self.space == PNSpace::ApplicationData { None + } else { + // Nasty special case to prevent handshake deadlocks. + // A client needs to keep the PTO timer armed to prevent a stall + // of the handshake. Technically, this has to stop once we receive + // an ACK of Handshake or 1-RTT, or when we receive HANDSHAKE_DONE, + // but a few extra probes won't hurt. + // A server shouldn't arm its PTO timer this way. The server sends + // ack-eliciting, in-flight packets immediately so this only + // happens when the server has nothing outstanding. If we had + // client authentication, this might cause some extra probes, + // but they would be harmless anyway. + self.pto_base_time } } pub fn on_packet_sent(&mut self, sent_packet: SentPacket) { if sent_packet.ack_eliciting() { self.pto_base_time = Some(sent_packet.time_sent); - if sent_packet.cc_in_flight() { - self.in_flight_outstanding += 1; - } + self.in_flight_outstanding += 1; + } else if self.space != PNSpace::ApplicationData && self.pto_base_time.is_none() { + // For Initial and Handshake spaces, make sure that we have a PTO baseline + // always. See `LossRecoverySpace::pto_base_time()` for details. + self.pto_base_time = Some(sent_packet.time_sent); } self.sent_packets.insert(sent_packet.pn, sent_packet); } - pub fn remove_packet(&mut self, pn: u64) -> Option { - if let Some(sent) = self.sent_packets.remove(&pn) { - if sent.cc_in_flight() { - debug_assert!(self.in_flight_outstanding > 0); - self.in_flight_outstanding -= 1; - } - Some(sent) - } else { - None - } - } + fn remove_packet(&mut self, p: &SentPacket) { + if p.ack_eliciting() { + debug_assert!(self.in_flight_outstanding > 0); + self.in_flight_outstanding -= 1; + if self.in_flight_outstanding == 0 { + qtrace!("remove_packet outstanding == 0 for space {}", self.space); - // Remove all the acked packets. Returns them in ascending order -- largest - // (i.e. highest PN) acked packet is last. - fn remove_acked(&mut self, acked_ranges: Vec<(u64, u64)>) -> (Vec, bool) { - let mut acked_packets = BTreeMap::new(); - let mut eliciting = false; - for (end, start) in acked_ranges { - // ^^ Notabug: see Frame::decode_ack_frame() - for pn in start..=end { - if let Some(sent) = self.remove_packet(pn) { - qdebug!("acked={}", pn); - eliciting |= sent.ack_eliciting(); - acked_packets.insert(pn, sent); + // See above comments; keep PTO armed for Initial/Handshake even + // if no outstanding packets. + if self.space == PNSpace::ApplicationData { + self.pto_base_time = None; } } } - ( - acked_packets.into_iter().map(|(_k, v)| v).collect(), - eliciting, - ) + } + + /// Remove all acknowledged packets. + /// Returns all the acknowledged packets, with the largest packet number first. + /// ...and a boolean indicating if any of those packets were ack-eliciting. + /// This operates more efficiently because it assumes that the input is sorted + /// in the order that an ACK frame is (from the top). + fn remove_acked(&mut self, acked_ranges: R, stats: &mut Stats) -> (Vec, bool) + where + R: IntoIterator>, + R::IntoIter: ExactSizeIterator, + { + let acked_ranges = acked_ranges.into_iter(); + let mut keep = Vec::with_capacity(acked_ranges.len()); + + let mut acked = Vec::new(); + let mut eliciting = false; + for range in acked_ranges { + let first_keep = *range.end() + 1; + if let Some((&first, _)) = self.sent_packets.range(range).next() { + let mut tail = self.sent_packets.split_off(&first); + if let Some((&next, _)) = tail.range(first_keep..).next() { + keep.push(tail.split_off(&next)); + } + for (_, p) in tail.into_iter().rev() { + self.remove_packet(&p); + eliciting |= p.ack_eliciting(); + if p.lost() { + stats.late_ack += 1; + } + if p.pto_fired() { + stats.pto_ack += 1; + } + acked.push(p); + } + } + } + + for mut k in keep.into_iter().rev() { + self.sent_packets.append(&mut k); + } + + (acked, eliciting) } /// Remove all tracked packets from the space. @@ -314,17 +373,49 @@ impl LossRecoverySpace { /// and when keys are dropped. fn remove_ignored(&mut self) -> impl Iterator { self.in_flight_outstanding = 0; - std::mem::take(&mut self.sent_packets) + mem::take(&mut self.sent_packets) .into_iter() .map(|(_, v)| v) } + /// Remove old packets that we've been tracking in case they get acknowledged. + /// We try to keep these around until a probe is sent for them, so it is + /// important that `cd` is set to at least the current PTO time; otherwise we + /// might remove all in-flight packets and stop sending probes. + fn remove_old_lost(&mut self, now: Instant, cd: Duration) { + let mut it = self.sent_packets.iter(); + // If the first item is not expired, do nothing. + if it.next().map_or(false, |(_, p)| p.expired(now, cd)) { + // Find the index of the first unexpired packet. + let to_remove = if let Some(first_keep) = + it.find_map(|(i, p)| if p.expired(now, cd) { None } else { Some(*i) }) + { + // Some packets haven't expired, so keep those. + let keep = self.sent_packets.split_off(&first_keep); + mem::replace(&mut self.sent_packets, keep) + } else { + // All packets are expired. + mem::take(&mut self.sent_packets) + }; + for (_, p) in to_remove { + self.remove_packet(&p); + } + } + } + + /// Detect lost packets. + /// `loss_delay` is the time we will wait before declaring something lost. + /// `cleanup_delay` is the time we will wait before cleaning up a lost packet. pub fn detect_lost_packets( &mut self, now: Instant, loss_delay: Duration, + cleanup_delay: Duration, lost_packets: &mut Vec, ) { + // Housekeeping. + self.remove_old_lost(now, cleanup_delay); + // Packets sent before this time are deemed lost. let lost_deadline = now - loss_delay; qtrace!( @@ -341,9 +432,6 @@ impl LossRecoverySpace { // Lost for retrans/CC purposes let mut lost_pns = SmallVec::<[_; 8]>::new(); - // Lost for we-can-actually-forget-about-it purposes - let mut really_lost_pns = SmallVec::<[_; 8]>::new(); - for (pn, packet) in self .sent_packets .iter_mut() @@ -351,14 +439,14 @@ impl LossRecoverySpace { .take_while(|(&k, _)| Some(k) < largest_acked) { if packet.time_sent <= lost_deadline { - qdebug!( + qtrace!( "lost={}, time sent {:?} is before lost_deadline {:?}", pn, packet.time_sent, lost_deadline ); } else if largest_acked >= Some(*pn + PACKET_THRESHOLD) { - qdebug!( + qtrace!( "lost={}, is >= {} from largest acked {:?}", pn, PACKET_THRESHOLD, @@ -372,15 +460,9 @@ impl LossRecoverySpace { if packet.declare_lost(now) { lost_pns.push(*pn); - } else if packet.expired(now, loss_delay * 2) { - really_lost_pns.push(*pn); } } - for pn in really_lost_pns { - self.remove_packet(pn).expect("lost packet missing"); - } - lost_packets.extend(lost_pns.iter().map(|pn| self.sent_packets[pn].clone())); } } @@ -393,16 +475,6 @@ pub(crate) struct LossRecoverySpaces { } impl LossRecoverySpaces { - pub fn new() -> Self { - Self { - spaces: smallvec![ - LossRecoverySpace::new(PNSpace::ApplicationData), - LossRecoverySpace::new(PNSpace::Handshake), - LossRecoverySpace::new(PNSpace::Initial), - ], - } - } - fn idx(space: PNSpace) -> usize { match space { PNSpace::ApplicationData => 0, @@ -423,7 +495,7 @@ impl LossRecoverySpaces { self.spaces.shrink_to_fit(); sp } - _ => panic!("discarding application space"), + PNSpace::ApplicationData => panic!("discarding application space"), }; let mut sp = sp.unwrap(); assert_eq!(sp.space(), space, "dropping spaces out of order"); @@ -437,37 +509,56 @@ impl LossRecoverySpaces { pub fn get_mut(&mut self, space: PNSpace) -> Option<&mut LossRecoverySpace> { self.spaces.get_mut(Self::idx(space)) } -} -impl LossRecoverySpaces { fn iter(&self) -> impl Iterator { self.spaces.iter() } + fn iter_mut(&mut self) -> impl Iterator { self.spaces.iter_mut() } } +impl Default for LossRecoverySpaces { + fn default() -> Self { + Self { + spaces: smallvec![ + LossRecoverySpace::new(PNSpace::ApplicationData), + LossRecoverySpace::new(PNSpace::Handshake), + LossRecoverySpace::new(PNSpace::Initial), + ], + } + } +} + #[derive(Debug)] struct PtoState { + /// The packet number space that caused the PTO to fire. space: PNSpace, + /// The number of probes that we have sent. count: usize, packets: usize, + /// The complete set of packet number spaces that can have probes sent. + probe: PNSpaceSet, } impl PtoState { - pub fn new(space: PNSpace) -> Self { + pub fn new(space: PNSpace, probe: PNSpaceSet) -> Self { + debug_assert!(probe[space]); Self { space, count: 1, packets: PTO_PACKET_COUNT, + probe, } } - pub fn pto(&mut self, space: PNSpace) { + pub fn pto(&mut self, space: PNSpace, probe: PNSpaceSet) { + debug_assert!(probe[space]); self.space = space; self.count += 1; self.packets = PTO_PACKET_COUNT; + self.probe = probe; } pub fn count(&self) -> usize { @@ -479,15 +570,17 @@ impl PtoState { pub fn send_profile(&mut self, mtu: usize) -> SendProfile { if self.packets > 0 { self.packets -= 1; - SendProfile::new_pto(self.space, mtu) + SendProfile::new_pto(self.space, mtu, self.probe) } else { SendProfile::new_limited(0) } } } -#[derive(Debug)] +#[derive(Debug, Default)] pub(crate) struct LossRecovery { + /// When the handshake was confirmed, if it has been. + confirmed_time: Option, pto_state: Option, rtt_vals: RttVals, cc: CongestionControl, @@ -495,16 +588,14 @@ pub(crate) struct LossRecovery { spaces: LossRecoverySpaces, qlog: NeqoQlog, + stats: StatsCell, } impl LossRecovery { - pub fn new() -> Self { + pub fn new(stats: StatsCell) -> Self { Self { - rtt_vals: RttVals::default(), - pto_state: None, - cc: CongestionControl::default(), - spaces: LossRecoverySpaces::new(), - qlog: NeqoQlog::disabled(), + stats, + ..Self::default() } } @@ -534,10 +625,6 @@ impl LossRecovery { self.spaces.get(pn_space).and_then(|sp| sp.largest_acked) } - pub fn pto(&self) -> Duration { - self.rtt_vals.pto(PNSpace::ApplicationData) - } - pub fn set_qlog(&mut self, qlog: NeqoQlog) { self.cc.set_qlog(qlog.clone()); self.qlog = qlog; @@ -582,7 +669,7 @@ impl LossRecovery { &mut self, pn_space: PNSpace, largest_acked: u64, - acked_ranges: Vec<(u64, u64)>, + acked_ranges: Vec>, ack_delay: Duration, now: Instant, ) -> (Vec, Vec) { @@ -593,24 +680,25 @@ impl LossRecovery { largest_acked ); + let stats = &mut *self.stats.borrow_mut(); let space = self .spaces .get_mut(pn_space) .expect("ACK on discarded space"); - let (acked_packets, any_ack_eliciting) = space.remove_acked(acked_ranges); + let (acked_packets, any_ack_eliciting) = space.remove_acked(acked_ranges, stats); if acked_packets.is_empty() { // No new information. return (Vec::new(), Vec::new()); } // Track largest PN acked per space - let prev_largest_acked_sent_time = space.largest_acked_sent_time; + let prev_largest_acked = space.largest_acked_sent_time; if Some(largest_acked) > space.largest_acked { space.largest_acked = Some(largest_acked); // If the largest acknowledged is newly acked and any newly acked // packet was ack-eliciting, update the RTT. (-recovery 5.1) - let largest_acked_pkt = acked_packets.last().expect("must be there"); + let largest_acked_pkt = acked_packets.first().expect("must be there"); space.largest_acked_sent_time = Some(largest_acked_pkt.time_sent); if any_ack_eliciting { let latest_rtt = now - largest_acked_pkt.time_sent; @@ -618,26 +706,36 @@ impl LossRecovery { .update_rtt(&mut self.qlog, latest_rtt, ack_delay); } } - self.cc.on_packets_acked(&acked_packets); + // Perform loss detection. + // PTO is used to remove lost packets from in-flight accounting. + // We need to ensure that we have sent any PTO probes before they are removed + // as we rely on the count of in-flight packets to determine whether to send + // another probe. Removing them too soon would result in not sending on PTO. let loss_delay = self.loss_delay(); - let mut lost_packets = Vec::new(); - self.spaces.get_mut(pn_space).unwrap().detect_lost_packets( - now, - loss_delay, - &mut lost_packets, - ); - // TODO Process ECN information if present. - self.cc.on_packets_lost( - now, - prev_largest_acked_sent_time, - self.rtt_vals.pto(pn_space), - &lost_packets, - ); + let cleanup = self.pto_period(pn_space); + let mut lost = Vec::new(); + self.spaces + .get_mut(pn_space) + .unwrap() + .detect_lost_packets(now, loss_delay, cleanup, &mut lost); + stats.lost += lost.len(); + + // Tell the congestion controller about any lost packets. + // The PTO for congestion control is the raw number, without exponential + // backoff, so that we can determine persistent congestion. + let pto_raw = self.pto_raw(pn_space); + self.cc + .on_packets_lost(now, prev_largest_acked, pto_raw, &lost); + + // This must happen after on_packets_lost. If in recovery, this could + // take us out, and then lost packets will start a new recovery period + // when it shouldn't. + self.cc.on_packets_acked(&acked_packets); self.pto_state = None; - (acked_packets, lost_packets) + (acked_packets, lost) } fn loss_delay(&self) -> Duration { @@ -660,14 +758,33 @@ impl LossRecovery { .collect() } + fn confirmed(&mut self, now: Instant) { + debug_assert!(self.confirmed_time.is_none()); + self.confirmed_time = Some(now); + // Up until now, the ApplicationData space has been ignored for PTO. + // So maybe fire a PTO. + if let Some(pto) = self.pto_time(PNSpace::ApplicationData) { + if pto < now { + let probes = PNSpaceSet::from(&[PNSpace::ApplicationData]); + self.fire_pto(PNSpace::ApplicationData, probes); + } + } + } + /// Discard state for a given packet number space. - pub fn discard(&mut self, space: PNSpace) { + pub fn discard(&mut self, space: PNSpace, now: Instant) { qdebug!([self], "Reset loss recovery state for {}", space); - // We just made progress, so discard PTO count. - self.pto_state = None; for p in self.spaces.drop_space(space) { self.cc.discard(&p); } + + // We just made progress, so discard PTO count. + // The spec says that clients should not do this until confirming that + // the server has completed address validation, but ignore that. + self.pto_state = None; + if space == PNSpace::Handshake { + self.confirmed(now); + } } /// Calculate when the next timeout is likely to be. This is the earlier of the loss timer @@ -698,23 +815,45 @@ impl LossRecovery { .map(|val| val + self.loss_delay()) } + // The borrow checker is a harsh mistress. + // It's important that calls to `RttVals::pto()` are routed through a central point + // because that ensures consistency, but we often have a mutable borrow on other + // pieces of `self` that prevents that. + // An associated function avoids another borrow on `&self`. + fn pto_raw_inner(rtt_vals: &RttVals, space: PNSpace) -> Duration { + rtt_vals.pto(space) + } + + // Borrow checker hack, see above. + fn pto_period_inner( + rtt_vals: &RttVals, + pto_state: &Option, + pn_space: PNSpace, + ) -> Duration { + Self::pto_raw_inner(rtt_vals, pn_space) + .checked_mul(1 << pto_state.as_ref().map_or(0, |p| p.count)) + .unwrap_or(LOCAL_IDLE_TIMEOUT * 2) + } + /// Get the Base PTO value, which is derived only from the `RTT` and `RTTvar` values. /// This is for those cases where you need a value for the time you might sensibly - /// wait for a packet to propagate. Using `3*raw_pto()` is common. - pub fn raw_pto(&self) -> Duration { - self.rtt_vals.pto(PNSpace::ApplicationData) + /// wait for a packet to propagate. Using `3*pto_raw(..)` is common. + pub fn pto_raw(&self, space: PNSpace) -> Duration { + Self::pto_raw_inner(&self.rtt_vals, space) + } + + /// Get the current PTO period for the given packet number space. + /// Unlike `pto_raw`, this includes calculation for the exponential backoff. + fn pto_period(&self, pn_space: PNSpace) -> Duration { + Self::pto_period_inner(&self.rtt_vals, &self.pto_state, pn_space) } // Calculate PTO time for the given space. fn pto_time(&self, pn_space: PNSpace) -> Option { - if let Some(space) = self.spaces.get(pn_space) { - space.pto_base_time().map(|t| { - t + self - .rtt_vals - .pto(pn_space) - .checked_mul(1 << self.pto_state.as_ref().map_or(0, |p| p.count)) - .unwrap_or(LOCAL_IDLE_TIMEOUT * 2) - }) + if self.confirmed_time.is_none() && pn_space == PNSpace::ApplicationData { + None + } else if let Some(space) = self.spaces.get(pn_space) { + space.pto_base_time().map(|t| t + self.pto_period(pn_space)) } else { None } @@ -723,12 +862,29 @@ impl LossRecovery { /// Find the earliest PTO time for all active packet number spaces. /// Ignore Application if either Initial or Handshake have an active PTO. fn earliest_pto(&self) -> Option { - self.pto_time(PNSpace::Initial) - .iter() - .chain(self.pto_time(PNSpace::Handshake).iter()) - .min() - .cloned() - .or_else(|| self.pto_time(PNSpace::ApplicationData)) + if self.confirmed_time.is_some() { + self.pto_time(PNSpace::ApplicationData) + } else { + self.pto_time(PNSpace::Initial) + .iter() + .chain(self.pto_time(PNSpace::Handshake).iter()) + .min() + .cloned() + } + } + + fn fire_pto(&mut self, pn_space: PNSpace, allow_probes: PNSpaceSet) { + if let Some(st) = &mut self.pto_state { + st.pto(pn_space, allow_probes); + } else { + self.pto_state = Some(PtoState::new(pn_space, allow_probes)); + } + qlog::metrics_updated( + &mut self.qlog, + &[QlogMetric::PtoCount( + self.pto_state.as_ref().unwrap().count(), + )], + ); } /// This checks whether the PTO timer has fired and fires it if needed. @@ -737,36 +893,26 @@ impl LossRecovery { /// we have to clone the `SentPacket` instance. fn maybe_fire_pto(&mut self, now: Instant, lost: &mut Vec) { let mut pto_space = None; + // The spaces in which we will allow probing. + let mut allow_probes = PNSpaceSet::default(); for pn_space in PNSpace::iter() { - // Skip early packet number spaces where the PTO timer hasn't fired. - // Once the timer for one space has fired, include higher spaces. Declaring more - // data as "lost" makes it more likely that PTO packets will include useful data. - if pto_space.is_none() && self.pto_time(*pn_space).map_or(true, |t| t > now) { - continue; - } - qdebug!([self], "PTO timer fired for {}", pn_space); - if let Some(space) = self.spaces.get_mut(*pn_space) { - pto_space = pto_space.or(Some(*pn_space)); - lost.extend(space.pto_packets(PTO_PACKET_COUNT).cloned()); - } else { - qwarn!([self], "PTO timer for dropped space {}", pn_space); + if let Some(t) = self.pto_time(*pn_space) { + allow_probes[*pn_space] = true; + if t <= now { + qdebug!([self], "PTO timer fired for {}", pn_space); + let space = self.spaces.get_mut(*pn_space).unwrap(); + lost.extend(space.pto_packets(PTO_PACKET_COUNT).cloned()); + + pto_space = pto_space.or(Some(*pn_space)); + } } } // This has to happen outside the loop. Increasing the PTO count here causes the // pto_time to increase which might cause PTO for later packet number spaces to not fire. if let Some(pn_space) = pto_space { - if let Some(st) = &mut self.pto_state { - st.pto(pn_space); - } else { - self.pto_state = Some(PtoState::new(pn_space)); - } - qlog::metrics_updated( - &mut self.qlog, - &[QlogMetric::PtoCount( - self.pto_state.as_ref().unwrap().count(), - )], - ); + qtrace!([self], "PTO {}, probing {:?}", pn_space, allow_probes); + self.fire_pto(pn_space, allow_probes); } } @@ -777,14 +923,16 @@ impl LossRecovery { let mut lost_packets = Vec::new(); for space in self.spaces.iter_mut() { let first = lost_packets.len(); // The first packet lost in this space. - space.detect_lost_packets(now, loss_delay, &mut lost_packets); + let pto = Self::pto_period_inner(&self.rtt_vals, &self.pto_state, space.space()); + space.detect_lost_packets(now, loss_delay, pto, &mut lost_packets); self.cc.on_packets_lost( now, space.largest_acked_sent_time, - self.rtt_vals.pto(space.space()), + Self::pto_raw_inner(&self.rtt_vals, space.space()), &lost_packets[first..], ) } + self.stats.borrow_mut().lost += lost_packets.len(); self.maybe_fire_pto(now, &mut lost_packets); lost_packets @@ -830,8 +978,9 @@ impl ::std::fmt::Display for LossRecovery { #[cfg(test)] mod tests { - use super::{LossRecovery, LossRecoverySpace, PNSpace, SentPacket}; + use super::{LossRecovery, LossRecoverySpace, PNSpace, SentPacket, INITIAL_RTT, MAX_ACK_DELAY}; use crate::packet::PacketType; + use crate::stats::Stats; use std::convert::TryInto; use std::rc::Rc; use std::time::{Duration, Instant}; @@ -915,7 +1064,6 @@ mod tests { true, Rc::default(), ON_SENT_SIZE, - true, )); } } @@ -926,15 +1074,49 @@ mod tests { lr.on_ack_received( PNSpace::ApplicationData, pn, - vec![(pn, pn)], + vec![pn..=pn], ACK_DELAY, pn_time(pn) + delay, ); } + fn add_sent(lrs: &mut LossRecoverySpace, packet_numbers: &[u64]) { + for &pn in packet_numbers { + lrs.on_packet_sent(SentPacket::new( + PacketType::Short, + pn, + pn_time(pn), + true, + Rc::default(), + ON_SENT_SIZE, + )); + } + } + + fn match_acked(acked: &[SentPacket], expected: &[u64]) { + assert!(acked.iter().map(|p| &p.pn).eq(expected)); + } + + #[test] + fn remove_acked() { + let mut lrs = LossRecoverySpace::new(PNSpace::ApplicationData); + let mut stats = Stats::default(); + add_sent(&mut lrs, &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + let (acked, _) = lrs.remove_acked(vec![], &mut stats); + assert!(acked.is_empty()); + let (acked, _) = lrs.remove_acked(vec![7..=8, 2..=4], &mut stats); + match_acked(&acked, &[8, 7, 4, 3, 2]); + let (acked, _) = lrs.remove_acked(vec![8..=11], &mut stats); + match_acked(&acked, &[10, 9]); + let (acked, _) = lrs.remove_acked(vec![0..=2], &mut stats); + match_acked(&acked, &[1]); + let (acked, _) = lrs.remove_acked(vec![5..=6], &mut stats); + match_acked(&acked, &[6, 5]); + } + #[test] fn initial_rtt() { - let mut lr = LossRecovery::new(); + let mut lr = LossRecovery::default(); lr.start_pacer(now()); pace(&mut lr, 1); let rtt = ms!(100); @@ -944,16 +1126,16 @@ mod tests { } /// An initial RTT for using with `setup_lr`. - const INITIAL_RTT: Duration = ms!(80); - const INITIAL_RTTVAR: Duration = ms!(40); + const TEST_RTT: Duration = ms!(80); + const TEST_RTTVAR: Duration = ms!(40); /// Send `n` packets (using PACING), then acknowledge the first. fn setup_lr(n: u64) -> LossRecovery { - let mut lr = LossRecovery::new(); + let mut lr = LossRecovery::default(); lr.start_pacer(now()); pace(&mut lr, n); - ack(&mut lr, 0, INITIAL_RTT); - assert_rtts(&lr, INITIAL_RTT, INITIAL_RTT, INITIAL_RTTVAR, INITIAL_RTT); + ack(&mut lr, 0, TEST_RTT); + assert_rtts(&lr, TEST_RTT, TEST_RTT, TEST_RTTVAR, TEST_RTT); assert_no_sent_times(&lr); lr } @@ -962,15 +1144,9 @@ mod tests { #[test] fn ack_delay_adjusted() { let mut lr = setup_lr(2); - ack(&mut lr, 1, INITIAL_RTT + ACK_DELAY); + ack(&mut lr, 1, TEST_RTT + ACK_DELAY); // RTT stays the same, but the RTTVAR is adjusted downwards. - assert_rtts( - &lr, - INITIAL_RTT, - INITIAL_RTT, - INITIAL_RTTVAR * 3 / 4, - INITIAL_RTT, - ); + assert_rtts(&lr, TEST_RTT, TEST_RTT, TEST_RTTVAR * 3 / 4, TEST_RTT); assert_no_sent_times(&lr); } @@ -980,15 +1156,15 @@ mod tests { let mut lr = setup_lr(2); let extra = ms!(8); assert!(extra < ACK_DELAY); - ack(&mut lr, 1, INITIAL_RTT + extra); - let expected_rtt = INITIAL_RTT + (extra / 8); - let expected_rttvar = (INITIAL_RTTVAR * 3 + extra) / 4; + ack(&mut lr, 1, TEST_RTT + extra); + let expected_rtt = TEST_RTT + (extra / 8); + let expected_rttvar = (TEST_RTTVAR * 3 + extra) / 4; assert_rtts( &lr, - INITIAL_RTT + extra, + TEST_RTT + extra, expected_rtt, expected_rttvar, - INITIAL_RTT, + TEST_RTT, ); assert_no_sent_times(&lr); } @@ -998,10 +1174,10 @@ mod tests { fn reduce_min_rtt() { let mut lr = setup_lr(2); let delta = ms!(4); - let reduced_rtt = INITIAL_RTT - delta; + let reduced_rtt = TEST_RTT - delta; ack(&mut lr, 1, reduced_rtt); - let expected_rtt = INITIAL_RTT - (delta / 8); - let expected_rttvar = (INITIAL_RTTVAR * 3 + delta) / 4; + let expected_rtt = TEST_RTT - (delta / 8); + let expected_rttvar = (TEST_RTTVAR * 3 + delta) / 4; assert_rtts(&lr, reduced_rtt, expected_rtt, expected_rttvar, reduced_rtt); assert_no_sent_times(&lr); } @@ -1011,7 +1187,7 @@ mod tests { fn no_new_acks() { let mut lr = setup_lr(1); let check = |lr: &LossRecovery| { - assert_rtts(&lr, INITIAL_RTT, INITIAL_RTT, INITIAL_RTTVAR, INITIAL_RTT); + assert_rtts(&lr, TEST_RTT, TEST_RTT, TEST_RTTVAR, TEST_RTT); assert_no_sent_times(&lr); }; check(&lr); @@ -1026,7 +1202,7 @@ mod tests { // Test time loss detection as part of handling a regular ACK. #[test] fn time_loss_detection_gap() { - let mut lr = LossRecovery::new(); + let mut lr = LossRecovery::default(); lr.start_pacer(now()); // Create a single packet gap, and have pn 0 time out. // This can't use the default pacing, which is too tight. @@ -1040,23 +1216,21 @@ mod tests { true, Rc::default(), ON_SENT_SIZE, - true, )); lr.on_packet_sent(SentPacket::new( PacketType::Short, 1, - pn_time(0) + INITIAL_RTT / 4, + pn_time(0) + TEST_RTT / 4, true, Rc::default(), ON_SENT_SIZE, - true, )); let (_, lost) = lr.on_ack_received( PNSpace::ApplicationData, 1, - vec![(1, 1)], + vec![1..=1], ACK_DELAY, - pn_time(0) + (INITIAL_RTT * 5 / 4), + pn_time(0) + (TEST_RTT * 5 / 4), ); assert_eq!(lost.len(), 1); assert_no_sent_times(&lr); @@ -1070,14 +1244,14 @@ mod tests { // We want to declare PN 2 as acknowledged before we declare PN 1 as lost. // For this to work, we need PACING above to be less than 1/8 of an RTT. let pn1_sent_time = pn_time(1); - let pn1_loss_time = pn1_sent_time + (INITIAL_RTT * 9 / 8); - let pn2_ack_time = pn_time(2) + INITIAL_RTT; + let pn1_loss_time = pn1_sent_time + (TEST_RTT * 9 / 8); + let pn2_ack_time = pn_time(2) + TEST_RTT; assert!(pn1_loss_time > pn2_ack_time); let (_, lost) = lr.on_ack_received( PNSpace::ApplicationData, 2, - vec![(2, 2)], + vec![2..=2], ACK_DELAY, pn2_ack_time, ); @@ -1093,7 +1267,7 @@ mod tests { let packets = lr.timeout(pn1_loss_time); assert_eq!(packets.len(), 1); // Checking for expiration with zero delay lets us check the loss time. - assert!(packets[0].expired(pn1_loss_time, Duration::from_secs(0))); + assert!(packets[0].expired(pn1_loss_time, Duration::new(0, 0))); assert_no_sent_times(&lr); } @@ -1105,7 +1279,7 @@ mod tests { let (_, lost) = lr.on_ack_received( PNSpace::ApplicationData, 4, - vec![(4, 2)], + vec![2..=4], ACK_DELAY, pn_time(4), ); @@ -1115,23 +1289,23 @@ mod tests { #[test] #[should_panic(expected = "discarding application space")] fn drop_app() { - let mut lr = LossRecovery::new(); - lr.discard(PNSpace::ApplicationData); + let mut lr = LossRecovery::default(); + lr.discard(PNSpace::ApplicationData, now()); } #[test] #[should_panic(expected = "dropping spaces out of order")] fn drop_out_of_order() { - let mut lr = LossRecovery::new(); - lr.discard(PNSpace::Handshake); + let mut lr = LossRecovery::default(); + lr.discard(PNSpace::Handshake, now()); } #[test] #[should_panic(expected = "ACK on discarded space")] fn ack_after_drop() { - let mut lr = LossRecovery::new(); + let mut lr = LossRecovery::default(); lr.start_pacer(now()); - lr.discard(PNSpace::Initial); + lr.discard(PNSpace::Initial, now()); lr.on_ack_received( PNSpace::Initial, 0, @@ -1143,7 +1317,7 @@ mod tests { #[test] fn drop_spaces() { - let mut lr = LossRecovery::new(); + let mut lr = LossRecovery::default(); lr.start_pacer(now()); lr.on_packet_sent(SentPacket::new( PacketType::Initial, @@ -1152,7 +1326,6 @@ mod tests { true, Rc::default(), ON_SENT_SIZE, - true, )); lr.on_packet_sent(SentPacket::new( PacketType::Handshake, @@ -1161,7 +1334,6 @@ mod tests { true, Rc::default(), ON_SENT_SIZE, - true, )); lr.on_packet_sent(SentPacket::new( PacketType::Short, @@ -1170,7 +1342,6 @@ mod tests { true, Rc::default(), ON_SENT_SIZE, - true, )); // Now put all spaces on the LR timer so we can see them. @@ -1179,30 +1350,24 @@ mod tests { PacketType::Handshake, PacketType::Short, ] { - let sent_pkt = - SentPacket::new(*sp, 1, pn_time(3), true, Rc::default(), ON_SENT_SIZE, true); + let sent_pkt = SentPacket::new(*sp, 1, pn_time(3), true, Rc::default(), ON_SENT_SIZE); let pn_space = PNSpace::from(sent_pkt.pt); lr.on_packet_sent(sent_pkt); - lr.on_ack_received( - pn_space, - 1, - vec![(1, 1)], - Duration::from_secs(0), - pn_time(3), - ); + lr.on_ack_received(pn_space, 1, vec![1..=1], Duration::from_secs(0), pn_time(3)); let mut lost = Vec::new(); lr.spaces.get_mut(pn_space).unwrap().detect_lost_packets( pn_time(3), - INITIAL_RTT, + TEST_RTT, + TEST_RTT * 3, // unused &mut lost, ); assert!(lost.is_empty()); } - lr.discard(PNSpace::Initial); + lr.discard(PNSpace::Initial, pn_time(3)); assert_sent_times(&lr, None, Some(pn_time(1)), Some(pn_time(2))); - lr.discard(PNSpace::Handshake); + lr.discard(PNSpace::Handshake, pn_time(3)); assert_sent_times(&lr, None, None, Some(pn_time(2))); // There are cases where we send a packet that is not subsequently tracked. @@ -1214,8 +1379,43 @@ mod tests { true, Rc::default(), ON_SENT_SIZE, - true, )); assert_sent_times(&lr, None, None, Some(pn_time(2))); } + + #[test] + fn rearm_pto_after_confirmed() { + let mut lr = LossRecovery::default(); + lr.start_pacer(now()); + lr.on_packet_sent(SentPacket::new( + PacketType::Handshake, + 0, + now(), + true, + Rc::default(), + ON_SENT_SIZE, + )); + lr.on_packet_sent(SentPacket::new( + PacketType::Short, + 0, + now(), + true, + Rc::default(), + ON_SENT_SIZE, + )); + + assert_eq!(lr.pto_time(PNSpace::ApplicationData), None); + lr.discard(PNSpace::Initial, pn_time(1)); + assert_eq!(lr.pto_time(PNSpace::ApplicationData), None); + + // Expiring state after the PTO on the ApplicationData space has + // expired should result in setting a PTO state. + let expected_pto = pn_time(2) + (INITIAL_RTT * 3) + MAX_ACK_DELAY; + lr.discard(PNSpace::Handshake, expected_pto); + let profile = lr.send_profile(expected_pto, 10000); + assert!(profile.pto.is_some()); + assert!(!profile.should_probe(PNSpace::Initial)); + assert!(!profile.should_probe(PNSpace::Handshake)); + assert!(profile.should_probe(PNSpace::ApplicationData)); + } } diff --git a/third_party/rust/neqo-transport/src/recv_stream.rs b/third_party/rust/neqo-transport/src/recv_stream.rs index 0a208aa5b318..779adeeffa9b 100644 --- a/third_party/rust/neqo-transport/src/recv_stream.rs +++ b/third_party/rust/neqo-transport/src/recv_stream.rs @@ -79,7 +79,7 @@ impl RxStreamOrderer { // (In-order frames will take this path, with no overlap) let overlap = prev_end.saturating_sub(new_start); if overlap != 0 { - let truncate_to = new_data.len() - overlap as usize; + let truncate_to = prev_vec.len() - overlap as usize; prev_vec.truncate(truncate_to) } qtrace!( @@ -556,6 +556,132 @@ impl RecvStream { mod tests { use super::*; use crate::frame::Frame; + use std::ops::Range; + + fn recv_ranges(ranges: &[Range], available: usize) { + const ZEROES: &[u8] = &[0; 100]; + + let mut s = RxStreamOrderer::default(); + for r in ranges { + let data = ZEROES[..usize::try_from(r.end - r.start).unwrap()].to_vec(); + s.inbound_frame(r.start, data).unwrap(); + } + + let mut buf = vec![0xff; 100]; + let mut total_recvd = 0; + loop { + let recvd = s.read(&mut buf); + total_recvd += recvd; + if recvd == 0 { + assert_eq!(total_recvd, available); + break; + } + } + } + + #[test] + fn recv_noncontiguous() { + // Non-contiguous with the start, no data available. + recv_ranges(&[10..20], 0); + } + + /// Overlaps with the start of a 10..20 range of bytes. + #[test] + fn recv_overlap_start() { + // Overlap the start, with a larger new value. + // More overlap than not. + recv_ranges(&[10..20, 4..18, 0..4], 20); + // Overlap the start, with a larger new value. + // Less overlap than not. + recv_ranges(&[10..20, 2..15, 0..2], 20); + // Overlap the start, with a smaller new value. + // More overlap than not. + recv_ranges(&[10..20, 8..14, 0..8], 20); + // Overlap the start, with a smaller new value. + // Less overlap than not. + recv_ranges(&[10..20, 6..13, 0..6], 20); + + // Again with some of the first range split in two. + recv_ranges(&[10..11, 11..20, 4..18, 0..4], 20); + recv_ranges(&[10..11, 11..20, 2..15, 0..2], 20); + recv_ranges(&[10..11, 11..20, 8..14, 0..8], 20); + recv_ranges(&[10..11, 11..20, 6..13, 0..6], 20); + + // Again with a gap in the first range. + recv_ranges(&[10..11, 12..20, 4..18, 0..4], 20); + recv_ranges(&[10..11, 12..20, 2..15, 0..2], 20); + recv_ranges(&[10..11, 12..20, 8..14, 0..8], 20); + recv_ranges(&[10..11, 12..20, 6..13, 0..6], 20); + } + + /// Overlaps with the end of a 10..20 range of bytes. + #[test] + fn recv_overlap_end() { + // Overlap the end, with a larger new value. + // More overlap than not. + recv_ranges(&[10..20, 12..25, 0..10], 25); + // Overlap the end, with a larger new value. + // Less overlap than not. + recv_ranges(&[10..20, 17..33, 0..10], 33); + // Overlap the end, with a smaller new value. + // More overlap than not. + recv_ranges(&[10..20, 15..21, 0..10], 21); + // Overlap the end, with a smaller new value. + // Less overlap than not. + recv_ranges(&[10..20, 17..25, 0..10], 25); + + // Again with some of the first range split in two. + recv_ranges(&[10..19, 19..20, 12..25, 0..10], 25); + recv_ranges(&[10..19, 19..20, 17..33, 0..10], 33); + recv_ranges(&[10..19, 19..20, 15..21, 0..10], 21); + recv_ranges(&[10..19, 19..20, 17..25, 0..10], 25); + + // Again with a gap in the first range. + recv_ranges(&[10..18, 19..20, 12..25, 0..10], 25); + recv_ranges(&[10..18, 19..20, 17..33, 0..10], 33); + recv_ranges(&[10..18, 19..20, 15..21, 0..10], 21); + recv_ranges(&[10..18, 19..20, 17..25, 0..10], 25); + } + + /// Complete overlaps with the start of a 10..20 range of bytes. + #[test] + fn recv_overlap_complete() { + // Complete overlap, more at the end. + recv_ranges(&[10..20, 9..23, 0..9], 23); + // Complete overlap, more at the start. + recv_ranges(&[10..20, 3..23, 0..3], 23); + // Complete overlap, to end. + recv_ranges(&[10..20, 5..20, 0..5], 20); + // Complete overlap, from start. + recv_ranges(&[10..20, 10..27, 0..10], 27); + // Complete overlap, from 0 and more. + recv_ranges(&[10..20, 0..23], 23); + + // Again with the first range split in two. + recv_ranges(&[10..14, 14..20, 9..23, 0..9], 23); + recv_ranges(&[10..14, 14..20, 3..23, 0..3], 23); + recv_ranges(&[10..14, 14..20, 5..20, 0..5], 20); + recv_ranges(&[10..14, 14..20, 10..27, 0..10], 27); + recv_ranges(&[10..14, 14..20, 0..23], 23); + + // Again with the a gap in the first range. + recv_ranges(&[10..13, 14..20, 9..23, 0..9], 23); + recv_ranges(&[10..13, 14..20, 3..23, 0..3], 23); + recv_ranges(&[10..13, 14..20, 5..20, 0..5], 20); + recv_ranges(&[10..13, 14..20, 10..27, 0..10], 27); + recv_ranges(&[10..13, 14..20, 0..23], 23); + } + + /// An overlap with no new bytes. + #[test] + fn recv_overlap_duplicate() { + recv_ranges(&[10..20, 11..12, 0..10], 20); + recv_ranges(&[10..20, 10..15, 0..10], 20); + recv_ranges(&[10..20, 14..20, 0..10], 20); + // Now with the first range split. + recv_ranges(&[10..14, 14..20, 10..15, 0..10], 20); + recv_ranges(&[10..15, 16..20, 21..25, 10..25, 0..10], 25); + } #[test] fn test_stream_rx() { @@ -701,7 +827,7 @@ mod tests { #[test] fn test_stream_flowc_update() { - let flow_mgr = Rc::new(RefCell::new(FlowMgr::default())); + let flow_mgr = Rc::default(); let conn_events = ConnectionEvents::default(); let frame1 = vec![0; RX_STREAM_DATA_WINDOW as usize]; diff --git a/third_party/rust/neqo-transport/src/send_stream.rs b/third_party/rust/neqo-transport/src/send_stream.rs index 0a6ddceed30a..5ed340ea38b6 100644 --- a/third_party/rust/neqo-transport/src/send_stream.rs +++ b/third_party/rust/neqo-transport/src/send_stream.rs @@ -8,11 +8,12 @@ use std::cell::RefCell; use std::cmp::{max, min}; -use std::collections::{hash_map::IterMut, BTreeMap, HashMap, VecDeque}; +use std::collections::{BTreeMap, VecDeque}; use std::convert::{TryFrom, TryInto}; use std::mem; use std::rc::Rc; +use indexmap::IndexMap; use smallvec::SmallVec; use neqo_common::{qdebug, qerror, qinfo, qtrace}; @@ -208,11 +209,11 @@ impl RangeTracker { fn unmark_range(&mut self, off: u64, len: usize) { if len == 0 { - qinfo!("unmark 0-length range at {}", off); + qdebug!("unmark 0-length range at {}", off); return; } - let len = len as u64; + let len = u64::try_from(len).unwrap(); let end_off = off + len; let mut to_remove = SmallVec::<[_; 8]>::new(); @@ -225,7 +226,7 @@ impl RangeTracker { // Check for overlap if *cur_off + *cur_len > off { if *cur_state == RangeState::Acked { - qinfo!( + qdebug!( "Attempted to unmark Acked range {}-{} with unmark_range {}-{}", cur_off, cur_len, @@ -240,7 +241,7 @@ impl RangeTracker { } if *cur_state == RangeState::Acked { - qinfo!( + qdebug!( "Attempted to unmark Acked range {}-{} with unmark_range {}-{}", cur_off, cur_len, @@ -271,6 +272,11 @@ impl RangeTracker { self.used.insert(new_cur_off, (new_cur_len, cur_state)); } } + + /// Unmark all sent ranges. + pub fn unmark_sent(&mut self) { + self.unmark_range(0, usize::try_from(self.highest_offset()).unwrap()); + } } /// Buffer to contain queued bytes and track their state. @@ -355,6 +361,11 @@ impl TxBuffer { self.ranges.unmark_range(offset, len) } + /// Forget about anything that was marked as sent. + pub fn unmark_sent(&mut self) { + self.ranges.unmark_sent(); + } + fn data_limit(&self) -> u64 { self.buffered() as u64 + self.retired } @@ -501,14 +512,7 @@ impl SendStream { pub fn mark_as_sent(&mut self, offset: u64, len: usize, fin: bool) { if let Some(buf) = self.state.tx_buf_mut() { buf.mark_as_sent(offset, len); - if offset + len as u64 == self.max_stream_data { - self.flow_mgr - .borrow_mut() - .stream_data_blocked(self.stream_id, self.max_stream_data); - } - if self.flow_mgr.borrow().conn_credit_avail() == 0 { - self.flow_mgr.borrow_mut().data_blocked(); - } + self.send_blocked_if_space_needed(0); }; if fin { @@ -617,14 +621,14 @@ impl SendStream { self.send_internal(buf, true) } - fn send_blocked(&mut self, len: u64) { - if self.credit_avail() < len { + fn send_blocked_if_space_needed(&mut self, needed_space: u64) { + if self.credit_avail() <= needed_space { self.flow_mgr .borrow_mut() .stream_data_blocked(self.stream_id, self.max_stream_data); } - if self.flow_mgr.borrow().conn_credit_avail() < len { + if self.flow_mgr.borrow().conn_credit_avail() <= needed_space { self.flow_mgr.borrow_mut().data_blocked(); } } @@ -648,8 +652,8 @@ impl SendStream { let buf = if buf.is_empty() || (self.avail() == 0) { return Ok(0); } else if self.avail() < buf.len() { - self.send_blocked(buf.len() as u64); if atomic { + self.send_blocked_if_space_needed(buf.len() as u64); return Ok(0); } else { &buf[..self.avail()] @@ -729,7 +733,7 @@ impl SendStream { } #[derive(Debug, Default)] -pub(crate) struct SendStreams(HashMap); +pub(crate) struct SendStreams(IndexMap); impl SendStreams { pub fn get(&self, id: StreamId) -> Res<&SendStream> { @@ -818,9 +822,9 @@ impl SendStreams { impl<'a> IntoIterator for &'a mut SendStreams { type Item = (&'a StreamId, &'a mut SendStream); - type IntoIter = IterMut<'a, StreamId, SendStream>; + type IntoIter = indexmap::map::IterMut<'a, StreamId, SendStream>; - fn into_iter(self) -> IterMut<'a, StreamId, SendStream> { + fn into_iter(self) -> indexmap::map::IterMut<'a, StreamId, SendStream> { self.0.iter_mut() } } @@ -874,6 +878,59 @@ mod tests { assert_eq!(rt.acked_from_zero(), 400); } + #[test] + fn unmark_sent_start() { + let mut rt = RangeTracker::default(); + + rt.mark_range(0, 5, RangeState::Sent); + assert_eq!(rt.highest_offset(), 5); + assert_eq!(rt.acked_from_zero(), 0); + + rt.unmark_sent(); + assert_eq!(rt.highest_offset(), 0); + assert_eq!(rt.acked_from_zero(), 0); + assert_eq!(rt.first_unmarked_range(), (0, None)); + } + + #[test] + fn unmark_sent_middle() { + let mut rt = RangeTracker::default(); + + rt.mark_range(0, 5, RangeState::Acked); + assert_eq!(rt.highest_offset(), 5); + assert_eq!(rt.acked_from_zero(), 5); + rt.mark_range(5, 5, RangeState::Sent); + assert_eq!(rt.highest_offset(), 10); + assert_eq!(rt.acked_from_zero(), 5); + rt.mark_range(10, 5, RangeState::Acked); + assert_eq!(rt.highest_offset(), 15); + assert_eq!(rt.acked_from_zero(), 5); + assert_eq!(rt.first_unmarked_range(), (15, None)); + + rt.unmark_sent(); + assert_eq!(rt.highest_offset(), 15); + assert_eq!(rt.acked_from_zero(), 5); + assert_eq!(rt.first_unmarked_range(), (5, Some(5))); + } + + #[test] + fn unmark_sent_end() { + let mut rt = RangeTracker::default(); + + rt.mark_range(0, 5, RangeState::Acked); + assert_eq!(rt.highest_offset(), 5); + assert_eq!(rt.acked_from_zero(), 5); + rt.mark_range(5, 5, RangeState::Sent); + assert_eq!(rt.highest_offset(), 10); + assert_eq!(rt.acked_from_zero(), 5); + assert_eq!(rt.first_unmarked_range(), (10, None)); + + rt.unmark_sent(); + assert_eq!(rt.highest_offset(), 5); + assert_eq!(rt.acked_from_zero(), 5); + assert_eq!(rt.first_unmarked_range(), (5, None)); + } + #[test] fn truncate_front() { let mut v = VecDeque::new(); @@ -1307,7 +1364,12 @@ mod tests { // assert non-atomic write works assert_eq!(s.send(b"abc").unwrap(), 2); - // assert that STREAM_DATA_BLOCKED is sent. + assert_eq!(s.next_bytes(), Some((0, &b"ab"[..]))); + // STREAM_DATA_BLOCKED is not sent yet. + assert!(flow_mgr.borrow_mut().next().is_none()); + + // STREAM_DATA_BLOCKED is queued once bytes using all credit are sent. + s.mark_as_sent(0, 2, false); assert_eq!( flow_mgr.borrow_mut().next().unwrap(), Frame::StreamDataBlocked { @@ -1329,7 +1391,12 @@ mod tests { // assert non-atomic write works assert_eq!(s.send(b"abcd").unwrap(), 3); - // assert that STREAM_DATA_BLOCKED is sent. + assert_eq!(s.next_bytes(), Some((2, &b"abc"[..]))); + // DATA_BLOCKED is not sent yet. + assert!(flow_mgr.borrow_mut().next().is_none()); + + // DATA_BLOCKED is queued once bytes using all credit are sent. + s.mark_as_sent(2, 3, false); assert_eq!( flow_mgr.borrow_mut().next().unwrap(), Frame::DataBlocked { data_limit: 0x5 } diff --git a/third_party/rust/neqo-transport/src/stats.rs b/third_party/rust/neqo-transport/src/stats.rs index 09bf9d8c373b..baab5e044bcb 100644 --- a/third_party/rust/neqo-transport/src/stats.rs +++ b/third_party/rust/neqo-transport/src/stats.rs @@ -5,38 +5,89 @@ // except according to those terms. // Tracking of some useful statistics. +#![deny(clippy::pedantic)] -use neqo_common::qwarn; +use neqo_common::qinfo; +use std::cell::RefCell; +use std::fmt::{self, Debug}; +use std::ops::Deref; +use std::rc::Rc; -#[derive(Default, Debug)] /// Connection statistics +#[derive(Default, Clone)] +#[allow(clippy::module_name_repetitions)] pub struct Stats { - conn_display_info: String, + info: String, - /// Total packets received + /// Total packets received, including all the bad ones. pub packets_rx: usize, - /// Total packets sent - pub packets_tx: usize, - /// Duplicate packets received + /// Duplicate packets received. pub dups_rx: usize, - /// Dropped datagrams, or parts thereof + /// Dropped packets or dropped garbage. pub dropped_rx: usize, - /// resumption used + + /// Total packets sent. + pub packets_tx: usize, + /// Total number of packets that are declared lost. + pub lost: usize, + /// Late acknowledgments, for packets that were declared lost already. + pub late_ack: usize, + /// Acknowledgments for packets that contained data that was marked + /// for retransmission when the PTO timer popped. + pub pto_ack: usize, + + /// Whether the connection was resumed successfully. pub resumed: bool, } impl Stats { - pub fn init(&mut self, conn_info: String) { - self.conn_display_info = conn_info; + pub fn init(&mut self, info: String) { + self.info = info; } pub fn pkt_dropped(&mut self, reason: impl AsRef) { self.dropped_rx += 1; - qwarn!( - [self.conn_display_info], + qinfo!( + [self.info], "Dropped received packet: {}; Total: {}", reason.as_ref(), self.dropped_rx ) } } + +impl Debug for Stats { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + writeln!(f, "stats for {}", self.info)?; + writeln!( + f, + " rx: {} drop {} dup {}", + self.packets_rx, self.dropped_rx, self.dups_rx + )?; + writeln!( + f, + " tx: {} lost {} lateack {} ptoack {}", + self.packets_tx, self.lost, self.late_ack, self.pto_ack + )?; + write!(f, " resumed: {} ", self.resumed) + } +} + +#[derive(Default, Clone)] +#[allow(clippy::module_name_repetitions)] +pub struct StatsCell { + stats: Rc>, +} + +impl Deref for StatsCell { + type Target = RefCell; + fn deref(&self) -> &Self::Target { + &*self.stats + } +} + +impl Debug for StatsCell { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.stats.borrow().fmt(f) + } +} diff --git a/third_party/rust/neqo-transport/src/tracking.rs b/third_party/rust/neqo-transport/src/tracking.rs index 0e31354f8add..28fd0d3c2fb4 100644 --- a/third_party/rust/neqo-transport/src/tracking.rs +++ b/third_party/rust/neqo-transport/src/tracking.rs @@ -10,6 +10,7 @@ use std::collections::VecDeque; use std::convert::TryInto; +use std::ops::{Index, IndexMut}; use std::rc::Rc; use std::time::{Duration, Instant}; @@ -63,6 +64,62 @@ impl From for PNSpace { } } +#[derive(Clone, Copy, Default)] +pub struct PNSpaceSet { + initial: bool, + handshake: bool, + application_data: bool, +} + +impl Index for PNSpaceSet { + type Output = bool; + + fn index(&self, space: PNSpace) -> &Self::Output { + match space { + PNSpace::Initial => &self.initial, + PNSpace::Handshake => &self.handshake, + PNSpace::ApplicationData => &self.application_data, + } + } +} + +impl IndexMut for PNSpaceSet { + fn index_mut(&mut self, space: PNSpace) -> &mut Self::Output { + match space { + PNSpace::Initial => &mut self.initial, + PNSpace::Handshake => &mut self.handshake, + PNSpace::ApplicationData => &mut self.application_data, + } + } +} + +impl> From for PNSpaceSet { + fn from(spaces: T) -> Self { + let mut v = Self::default(); + for sp in spaces.as_ref() { + v[*sp] = true; + } + v + } +} + +impl std::fmt::Debug for PNSpaceSet { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let mut first = true; + f.write_str("(")?; + for sp in PNSpace::iter() { + if self[*sp] { + if !first { + f.write_str("+")?; + first = false; + } + std::fmt::Display::fmt(sp, f)?; + } + } + f.write_str(")") + } +} + #[derive(Debug, Clone)] pub struct SentPacket { pub pt: PacketType, @@ -72,10 +129,9 @@ pub struct SentPacket { pub tokens: Rc>, time_declared_lost: Option, - /// After a PTO, the packet has been released. + /// After a PTO, this is true when the packet has been released. pto: bool, - in_flight: bool, pub size: usize, } @@ -87,7 +143,6 @@ impl SentPacket { ack_eliciting: bool, tokens: Rc>, size: usize, - in_flight: bool, ) -> Self { Self { pt, @@ -98,7 +153,6 @@ impl SentPacket { time_declared_lost: None, pto: false, size, - in_flight, } } @@ -107,12 +161,6 @@ impl SentPacket { self.ack_eliciting } - /// Returns `true` if the packet counts requires congestion control accounting. - /// The specification uses the term "in flight" for this. - pub fn cc_in_flight(&self) -> bool { - self.in_flight - } - /// Whether the packet has been declared lost. pub fn lost(&self) -> bool { self.time_declared_lost.is_some() @@ -122,8 +170,10 @@ impl SentPacket { /// congestion controller is pending. /// Returns `true` if the packet counts as being "in flight", /// and has not previously been declared lost. + /// Note that this should count packets that contain only ACK and PADDING, + /// but we don't send PADDING, so we don't track that. pub fn cc_outstanding(&self) -> bool { - self.cc_in_flight() && !self.lost() + self.ack_eliciting() && !self.lost() } /// Declare the packet as lost. Returns `true` if this is the first time. @@ -146,6 +196,11 @@ impl SentPacket { } } + /// Whether the packet contents were cleared out after a PTO. + pub fn pto_fired(&self) -> bool { + self.pto + } + /// On PTO, we need to get the recovery tokens so that we can ensure that /// the frames we sent can be sent again in the PTO packet(s). Do that just once. pub fn pto(&mut self) -> bool { @@ -234,7 +289,6 @@ impl PacketRange { /// Requires that other is equal to this, or a larger range. pub fn acknowledged(&mut self, other: &Self) { if (other.smallest <= self.smallest) && (other.largest >= self.largest) { - qinfo!([self], "Acknowledged"); self.ack_needed = false; } } @@ -248,7 +302,7 @@ impl ::std::fmt::Display for PacketRange { /// The ACK delay we use. pub const ACK_DELAY: Duration = Duration::from_millis(20); // 20ms -pub const MAX_UNACKED_PKTS: u64 = 1; +pub const MAX_UNACKED_PKTS: usize = 1; const MAX_TRACKED_RANGES: usize = 32; const MAX_ACKS_PER_FRAME: usize = 32; @@ -271,7 +325,7 @@ pub struct RecvdPackets { largest_pn_time: Option, // The time that we should be sending an ACK. ack_time: Option, - pkts_since_last_ack: u64, + pkts_since_last_ack: usize, } impl RecvdPackets { @@ -296,7 +350,7 @@ impl RecvdPackets { fn ack_now(&self, now: Instant) -> bool { match self.ack_time { Some(t) => t <= now, - _ => false, + None => false, } } @@ -392,7 +446,7 @@ impl RecvdPackets { while cur.smallest > ack.largest { cur = match range_iter.next() { Some(c) => c, - _ => return, + None => return, }; } cur.acknowledged(&ack); @@ -428,7 +482,7 @@ impl RecvdPackets { let first = match iter.next() { Some(v) => v, - _ => return None, // Nothing to send. + None => return None, // Nothing to send. }; let mut ack_ranges = Vec::new(); let mut last = first.smallest; @@ -495,7 +549,7 @@ impl AckTracker { self.spaces.shrink_to_fit(); sp } - _ => panic!("discarding application space"), + PNSpace::ApplicationData => panic!("discarding application space"), }; assert_eq!(sp.unwrap().space, space, "dropping spaces out of order"); } @@ -556,11 +610,12 @@ impl Default for AckTracker { #[cfg(test)] mod tests { use super::{ - AckTracker, Duration, Instant, PNSpace, RecoveryToken, RecvdPackets, ACK_DELAY, + AckTracker, Duration, Instant, PNSpace, PNSpaceSet, RecoveryToken, RecvdPackets, ACK_DELAY, MAX_TRACKED_RANGES, MAX_UNACKED_PKTS, }; use lazy_static::lazy_static; use std::collections::HashSet; + use std::convert::TryFrom; lazy_static! { static ref NOW: Instant = Instant::now(); @@ -646,7 +701,8 @@ mod tests { assert!(!rp.ack_now(*NOW)); // Some packets won't cause an ACK to be needed. - for num in 0..MAX_UNACKED_PKTS { + let max_unacked = u64::try_from(MAX_UNACKED_PKTS).unwrap(); + for num in 0..max_unacked { rp.set_received(*NOW, num, true); assert_eq!(Some(*NOW + ACK_DELAY), rp.ack_time()); assert!(!rp.ack_now(*NOW)); @@ -654,7 +710,7 @@ mod tests { } // Exceeding MAX_UNACKED_PKTS will move the ACK time to now. - rp.set_received(*NOW, MAX_UNACKED_PKTS, true); + rp.set_received(*NOW, max_unacked, true); assert_eq!(Some(*NOW), rp.ack_time()); assert!(rp.ack_now(*NOW)); } @@ -783,4 +839,39 @@ mod tests { Some(*NOW) ); } + + #[test] + fn pnspaceset_default() { + let set = PNSpaceSet::default(); + assert!(!set[PNSpace::Initial]); + assert!(!set[PNSpace::Handshake]); + assert!(!set[PNSpace::ApplicationData]); + } + + #[test] + fn pnspaceset_from() { + let set = PNSpaceSet::from(&[PNSpace::Initial]); + assert!(set[PNSpace::Initial]); + assert!(!set[PNSpace::Handshake]); + assert!(!set[PNSpace::ApplicationData]); + + let set = PNSpaceSet::from(&[PNSpace::Handshake, PNSpace::Initial]); + assert!(set[PNSpace::Initial]); + assert!(set[PNSpace::Handshake]); + assert!(!set[PNSpace::ApplicationData]); + + let set = PNSpaceSet::from(&[PNSpace::ApplicationData, PNSpace::ApplicationData]); + assert!(!set[PNSpace::Initial]); + assert!(!set[PNSpace::Handshake]); + assert!(set[PNSpace::ApplicationData]); + } + + #[test] + fn pnspaceset_copy() { + let set = PNSpaceSet::from(&[PNSpace::Handshake, PNSpace::ApplicationData]); + let copy = set; + assert!(!copy[PNSpace::Initial]); + assert!(copy[PNSpace::Handshake]); + assert!(copy[PNSpace::ApplicationData]); + } } diff --git a/third_party/rust/neqo-transport/tests/network.rs b/third_party/rust/neqo-transport/tests/network.rs new file mode 100644 index 000000000000..480188739f92 --- /dev/null +++ b/third_party/rust/neqo-transport/tests/network.rs @@ -0,0 +1,168 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![cfg_attr(feature = "deny-warnings", deny(warnings))] +#![warn(clippy::pedantic)] + +#[macro_use] +mod sim; + +use neqo_transport::{ConnectionError, Error, State}; +use sim::{ + connection::{ConnectionNode, ReachState, ReceiveData, SendData}, + network::{Delay, Drop, TailDrop}, + Simulator, +}; +use std::ops::Range; +use std::time::Duration; + +/// The amount of transfer. Much more than this takes a surprising amount of time. +const TRANSFER_AMOUNT: usize = 1 << 20; // 1M +const ZERO: Duration = Duration::from_millis(0); +const DELAY: Duration = Duration::from_millis(50); +const DELAY_RANGE: Range = DELAY..Duration::from_millis(55); +const JITTER: Duration = Duration::from_millis(10); + +simulate!( + connect_direct, + [ + ConnectionNode::new_client(boxed![ReachState::new(State::Confirmed)]), + ConnectionNode::new_server(boxed![ReachState::new(State::Confirmed)]), + ] +); + +simulate!( + idle_timeout, + [ + ConnectionNode::new_client(boxed![ + ReachState::new(State::Confirmed), + ReachState::new(State::Closed(ConnectionError::Transport( + Error::IdleTimeout + ))) + ]), + ConnectionNode::new_server(boxed![ + ReachState::new(State::Confirmed), + ReachState::new(State::Closed(ConnectionError::Transport( + Error::IdleTimeout + ))) + ]), + ] +); + +simulate!( + idle_timeout_crazy_rtt, + [ + ConnectionNode::new_client(boxed![ + ReachState::new(State::Confirmed), + ReachState::new(State::Closed(ConnectionError::Transport( + Error::IdleTimeout + ))) + ]), + Delay::new(Duration::from_secs(15)..Duration::from_secs(15)), + Drop::percentage(10), + ConnectionNode::new_server(boxed![ + ReachState::new(State::Confirmed), + ReachState::new(State::Closed(ConnectionError::Transport( + Error::IdleTimeout + ))) + ]), + Delay::new(Duration::from_secs(10)..Duration::from_secs(10)), + Drop::percentage(10), + ], +); + +simulate!( + transfer, + [ + ConnectionNode::new_client(boxed![SendData::new(TRANSFER_AMOUNT)]), + ConnectionNode::new_server(boxed![ReceiveData::new(TRANSFER_AMOUNT)]), + ] +); + +simulate!( + connect_fixed_rtt, + [ + ConnectionNode::new_client(boxed![ReachState::new(State::Confirmed)]), + Delay::new(DELAY..DELAY), + ConnectionNode::new_server(boxed![ReachState::new(State::Confirmed)]), + Delay::new(DELAY..DELAY), + ], +); + +simulate!( + connect_taildrop_jitter, + [ + ConnectionNode::new_client(boxed![ReachState::new(State::Confirmed)]), + TailDrop::dsl_uplink(), + Delay::new(ZERO..JITTER), + ConnectionNode::new_server(boxed![ReachState::new(State::Confirmed)]), + TailDrop::dsl_downlink(), + Delay::new(ZERO..JITTER), + ], +); + +simulate!( + connect_taildrop, + [ + ConnectionNode::new_client(boxed![ReachState::new(State::Confirmed)]), + TailDrop::dsl_uplink(), + ConnectionNode::new_server(boxed![ReachState::new(State::Confirmed)]), + TailDrop::dsl_downlink(), + ], +); + +simulate!( + transfer_delay_drop, + [ + ConnectionNode::new_client(boxed![SendData::new(TRANSFER_AMOUNT)]), + Delay::new(DELAY_RANGE), + Drop::percentage(1), + ConnectionNode::new_server(boxed![ReceiveData::new(TRANSFER_AMOUNT)]), + Delay::new(DELAY_RANGE), + Drop::percentage(1), + ], +); + +simulate!( + transfer_taildrop, + [ + ConnectionNode::new_client(boxed![SendData::new(TRANSFER_AMOUNT)]), + TailDrop::dsl_uplink(), + ConnectionNode::new_server(boxed![ReceiveData::new(TRANSFER_AMOUNT)]), + TailDrop::dsl_downlink(), + ], +); + +simulate!( + transfer_taildrop_jitter, + [ + ConnectionNode::new_client(boxed![SendData::new(TRANSFER_AMOUNT)]), + TailDrop::dsl_uplink(), + Delay::new(ZERO..JITTER), + ConnectionNode::new_server(boxed![ReceiveData::new(TRANSFER_AMOUNT)]), + TailDrop::dsl_downlink(), + Delay::new(ZERO..JITTER), + ], +); + +/// This test is a nasty piece of work. Delays are anything from 0 to 50ms and 1% of +/// packets get dropped. +#[test] +fn transfer_fixed_seed() { + let mut sim = Simulator::new( + "transfer_fixed_seed", + boxed![ + ConnectionNode::new_client(boxed![SendData::new(TRANSFER_AMOUNT)]), + Delay::new(ZERO..DELAY), + Drop::percentage(1), + ConnectionNode::new_server(boxed![ReceiveData::new(TRANSFER_AMOUNT)]), + Delay::new(ZERO..DELAY), + Drop::percentage(1), + ], + ); + sim.seed_str("117f65d90ee5c1a7fb685f3af502c7730ba5d31866b758d98f5e3c2117cf9b86"); + sim.run(); +} diff --git a/third_party/rust/neqo-transport/tests/server.rs b/third_party/rust/neqo-transport/tests/server.rs index 84ee4dce82a9..0fb77417a6aa 100644 --- a/third_party/rust/neqo-transport/tests/server.rs +++ b/third_party/rust/neqo-transport/tests/server.rs @@ -571,10 +571,10 @@ fn mitm_retry() { // Now to start the epic process of decrypting the packet, // rewriting the header to remove the token, and then re-encrypting. let client_initial2 = client_initial2.unwrap(); - let (protected_header, dcid, scid, payload) = decode_initial_header(&client_initial2); + let (protected_header, d_cid, s_cid, payload) = decode_initial_header(&client_initial2); // Now we have enough information to make keys. - let (aead, hp) = client_initial_aead_and_hp(&dcid); + let (aead, hp) = client_initial_aead_and_hp(&d_cid); let (header, pn) = remove_header_protection(&hp, protected_header, payload); let pn_len = header.len() - protected_header.len(); @@ -588,8 +588,8 @@ fn mitm_retry() { // Now re-encode without the token. let mut enc = Encoder::with_capacity(header.len()); enc.encode(&header[..5]) - .encode_vec(1, dcid) - .encode_vec(1, scid) + .encode_vec(1, d_cid) + .encode_vec(1, s_cid) .encode_vvec(&[]) .encode_varint(u64::try_from(payload.len()).unwrap()); let pn_offset = enc.len(); @@ -645,8 +645,8 @@ fn bad_client_initial() { let mut server = default_server(); let dgram = client.process(None, now()).dgram().expect("a datagram"); - let (header, dcid, scid, payload) = decode_initial_header(&dgram); - let (aead, hp) = client_initial_aead_and_hp(dcid); + let (header, d_cid, s_cid, payload) = decode_initial_header(&dgram); + let (aead, hp) = client_initial_aead_and_hp(d_cid); let (fixed_header, pn) = remove_header_protection(&hp, header, payload); let payload = &payload[(fixed_header.len() - header.len())..]; @@ -663,8 +663,8 @@ fn bad_client_initial() { header_enc .encode_byte(0xc0) // Initial with 1 byte packet number. .encode_uint(4, QuicVersion::default().as_u32()) - .encode_vec(1, dcid) - .encode_vec(1, scid) + .encode_vec(1, d_cid) + .encode_vec(1, s_cid) .encode_vvec(&[]) .encode_varint(u64::try_from(payload_enc.len() + aead.expansion() + 1).unwrap()) .encode_byte(u8::try_from(pn).unwrap()); @@ -732,16 +732,16 @@ fn version_negotiation() { let vn = server.process(Some(damaged), now()).dgram(); let mut dec = Decoder::from(&input[5..]); // Skip past version. - let dcid = dec.decode_vec(1).expect("client DCID").to_vec(); - let scid = dec.decode_vec(1).expect("client SCID").to_vec(); + let d_cid = dec.decode_vec(1).expect("client DCID").to_vec(); + let s_cid = dec.decode_vec(1).expect("client SCID").to_vec(); // We should have received a VN packet. let vn = vn.expect("a vn packet"); let mut dec = Decoder::from(&vn[1..]); // Skip first byte. assert_eq!(dec.decode_uint(4).expect("VN"), 0); - assert_eq!(dec.decode_vec(1).expect("VN DCID"), &scid[..]); - assert_eq!(dec.decode_vec(1).expect("VN SCID"), &dcid[..]); + assert_eq!(dec.decode_vec(1).expect("VN DCID"), &s_cid[..]); + assert_eq!(dec.decode_vec(1).expect("VN SCID"), &d_cid[..]); let mut found = false; while dec.remaining() > 0 { let v = dec.decode_uint(4).expect("supported version"); diff --git a/third_party/rust/neqo-transport/tests/sim/connection.rs b/third_party/rust/neqo-transport/tests/sim/connection.rs new file mode 100644 index 000000000000..62d9a437a9b8 --- /dev/null +++ b/third_party/rust/neqo-transport/tests/sim/connection.rs @@ -0,0 +1,299 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(clippy::module_name_repetitions)] + +use super::{Node, Rng}; +use neqo_common::{qdebug, qtrace, Datagram}; +use neqo_crypto::AuthenticationStatus; +use neqo_transport::{Connection, ConnectionEvent, Output, State, StreamId, StreamType}; +use std::cmp::min; +use std::fmt::{self, Debug}; +use std::time::Instant; + +/// The status of the processing of an event. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum GoalStatus { + /// The event didn't result in doing anything; the goal is waiting for something. + Waiting, + /// An action was taken as a result of the event. + Active, + /// The goal was accomplished. + Done, +} + +/// A goal for the connection. +/// Goals can be accomplished in any order. +pub trait ConnectionGoal { + fn init(&mut self, _c: &mut Connection, _now: Instant) {} + /// Perform some processing. + fn process(&mut self, _c: &mut Connection, _now: Instant) -> GoalStatus { + GoalStatus::Waiting + } + /// Handle an event from the provided connection, returning `true` when the + /// goal is achieved. + fn handle_event(&mut self, c: &mut Connection, e: &ConnectionEvent, now: Instant) + -> GoalStatus; +} + +pub struct ConnectionNode { + c: Connection, + goals: Vec>, +} + +impl ConnectionNode { + pub fn new_client(goals: impl IntoIterator>) -> Self { + Self { + c: test_fixture::default_client(), + goals: goals.into_iter().collect(), + } + } + + pub fn new_server(goals: impl IntoIterator>) -> Self { + Self { + c: test_fixture::default_server(), + goals: goals.into_iter().collect(), + } + } + + #[allow(dead_code)] + pub fn clear_goals(&mut self) { + self.goals.clear(); + } + + #[allow(dead_code)] + pub fn add_goal(&mut self, goal: Box) { + self.goals.push(goal); + } + + /// Process all goals using the given closure and return whether any were active. + fn process_goals(&mut self, mut f: F) -> bool + where + F: FnMut(&mut Box, &mut Connection) -> GoalStatus, + { + // Waiting on drain_filter... + // self.goals.drain_filter(|g| f(g, &mut self.c, &e)).count(); + let mut active = false; + let mut i = 0; + while i < self.goals.len() { + let status = f(&mut self.goals[i], &mut self.c); + if status == GoalStatus::Done { + self.goals.remove(i); + active = true; + } else { + active |= status == GoalStatus::Active; + i += 1; + } + } + active + } +} + +impl Node for ConnectionNode { + fn init(&mut self, _rng: Rng, now: Instant) { + for g in &mut self.goals { + g.init(&mut self.c, now); + } + } + + fn process(&mut self, mut d: Option, now: Instant) -> Output { + let _ = self.process_goals(|goal, c| goal.process(c, now)); + loop { + let res = self.c.process(d.take(), now); + + let mut active = false; + while let Some(e) = self.c.next_event() { + qtrace!([self.c], "received event {:?}", e); + + // Perform authentication automatically. + if matches!(e, ConnectionEvent::AuthenticationNeeded) { + self.c.authenticated(AuthenticationStatus::Ok, now); + } + + active |= self.process_goals(|goal, c| goal.handle_event(c, &e, now)) + } + // Exit at this point if the connection produced a datagram. + // We also exit if none of the goals were active, as there is + // no point trying again if they did nothing. + if matches!(res, Output::Datagram(_)) || !active { + return res; + } + qdebug!([self.c], "no datagram and goal activity, looping"); + } + } + + fn done(&self) -> bool { + self.goals.is_empty() + } + + fn print_summary(&self, test_name: &str) { + println!("{}: {:?}", test_name, self.c.stats()); + } +} + +impl Debug for ConnectionNode { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.c, f) + } +} + +#[derive(Debug, Clone)] +pub struct ReachState { + target: State, +} + +impl ReachState { + pub fn new(target: State) -> Self { + Self { target } + } +} + +impl ConnectionGoal for ReachState { + fn handle_event( + &mut self, + _c: &mut Connection, + e: &ConnectionEvent, + _now: Instant, + ) -> GoalStatus { + if matches!(e, ConnectionEvent::StateChange(state) if *state == self.target) { + GoalStatus::Done + } else { + GoalStatus::Waiting + } + } +} + +#[derive(Debug)] +pub struct SendData { + remaining: usize, + stream_id: Option, +} + +impl SendData { + pub fn new(amount: usize) -> Self { + Self { + remaining: amount, + stream_id: None, + } + } + + fn make_stream(&mut self, c: &mut Connection) { + if self.stream_id.is_none() { + if let Ok(stream_id) = c.stream_create(StreamType::UniDi) { + qdebug!([c], "made stream {} for sending", stream_id); + self.stream_id = Some(StreamId::new(stream_id)); + } + } + } + + fn send(&mut self, c: &mut Connection, stream_id: StreamId) -> GoalStatus { + const DATA: &[u8] = &[0; 4096]; + let stream_id = stream_id.as_u64(); + let mut status = GoalStatus::Waiting; + loop { + let end = min(self.remaining, DATA.len()); + let sent = c.stream_send(stream_id, &DATA[..end]).unwrap(); + if sent == 0 { + return status; + } + self.remaining -= sent; + qtrace!("sent {} remaining {}", sent, self.remaining); + if self.remaining == 0 { + c.stream_close_send(stream_id).unwrap(); + return GoalStatus::Done; + } + status = GoalStatus::Active; + } + } +} + +impl ConnectionGoal for SendData { + fn init(&mut self, c: &mut Connection, _now: Instant) { + self.make_stream(c); + } + + fn process(&mut self, c: &mut Connection, _now: Instant) -> GoalStatus { + if let Some(stream_id) = self.stream_id { + self.send(c, stream_id) + } else { + GoalStatus::Waiting + } + } + + fn handle_event( + &mut self, + c: &mut Connection, + e: &ConnectionEvent, + _now: Instant, + ) -> GoalStatus { + match e { + ConnectionEvent::SendStreamCreatable { + stream_type: StreamType::UniDi, + } + // TODO(mt): remove the second condition when #842 is fixed. + | ConnectionEvent::StateChange(_) => { + self.make_stream(c); + GoalStatus::Active + } + + ConnectionEvent::SendStreamWritable { stream_id } + if Some(*stream_id) == self.stream_id => + { + self.send(c, *stream_id) + } + + // If we sent data in 0-RTT, then we didn't track how much we should + // have sent. This is trivial to fix if 0-RTT testing is ever needed. + ConnectionEvent::ZeroRttRejected => panic!("not supported"), + _ => GoalStatus::Waiting, + } + } +} + +/// Receive a prescribed amount of data from any stream. +#[derive(Debug)] +pub struct ReceiveData { + remaining: usize, +} + +impl ReceiveData { + pub fn new(amount: usize) -> Self { + Self { remaining: amount } + } + + fn recv(&mut self, c: &mut Connection, stream_id: StreamId) -> GoalStatus { + let mut buf = vec![0; 4096]; + let mut status = GoalStatus::Waiting; + loop { + let end = min(self.remaining, buf.len()); + let (recvd, _) = c.stream_recv(stream_id.as_u64(), &mut buf[..end]).unwrap(); + qtrace!("received {} remaining {}", recvd, self.remaining); + if recvd == 0 { + return status; + } + self.remaining -= recvd; + if self.remaining == 0 { + return GoalStatus::Done; + } + status = GoalStatus::Active; + } + } +} + +impl ConnectionGoal for ReceiveData { + fn handle_event( + &mut self, + c: &mut Connection, + e: &ConnectionEvent, + _now: Instant, + ) -> GoalStatus { + if let ConnectionEvent::RecvStreamReadable { stream_id } = e { + self.recv(c, StreamId::new(*stream_id)) + } else { + GoalStatus::Waiting + } + } +} diff --git a/third_party/rust/neqo-transport/tests/sim/delay.rs b/third_party/rust/neqo-transport/tests/sim/delay.rs new file mode 100644 index 000000000000..95188c0562a5 --- /dev/null +++ b/third_party/rust/neqo-transport/tests/sim/delay.rs @@ -0,0 +1,98 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(clippy::module_name_repetitions)] + +use super::{Node, Rng}; +use neqo_common::Datagram; +use neqo_transport::Output; +use std::collections::BTreeMap; +use std::convert::TryFrom; +use std::fmt::{self, Debug}; +use std::ops::Range; +use std::time::{Duration, Instant}; + +/// An iterator that shares a `Random` instance and produces uniformly +/// random `Duration`s within a specified range. +pub struct RandomDelay { + start: Duration, + max: u64, + rng: Option, +} + +impl RandomDelay { + /// Make a new random `Duration` generator. This panics if the range provided + /// is inverted (i.e., `bounds.start > bounds.end`), or spans 2^64 + /// or more nanoseconds. + /// A zero-length range means that random values won't be taken from the Rng + pub fn new(bounds: Range) -> Self { + let max = u64::try_from((bounds.end - bounds.start).as_nanos()).unwrap(); + Self { + start: bounds.start, + max, + rng: None, + } + } + + pub fn set_rng(&mut self, rng: Rng) { + self.rng = Some(rng); + } + + pub fn next(&mut self) -> Duration { + let mut rng = self.rng.as_ref().unwrap().borrow_mut(); + let r = rng.random_from(0..self.max); + self.start + Duration::from_nanos(r) + } +} + +pub struct Delay { + random: RandomDelay, + queue: BTreeMap, +} + +impl Delay { + pub fn new(bounds: Range) -> Self { + Self { + random: RandomDelay::new(bounds), + queue: BTreeMap::default(), + } + } + + fn insert(&mut self, d: Datagram, now: Instant) { + let mut t = now + self.random.next(); + while self.queue.contains_key(&t) { + // This is a little inefficient, but it avoids drops on collisions, + // which are super-common for a fixed delay. + t += Duration::from_nanos(1); + } + self.queue.insert(t, d); + } +} + +impl Node for Delay { + fn init(&mut self, rng: Rng, _now: Instant) { + self.random.set_rng(rng); + } + + fn process(&mut self, d: Option, now: Instant) -> Output { + if let Some(dgram) = d { + self.insert(dgram, now); + } + if let Some((&k, _)) = self.queue.range(..=now).next() { + Output::Datagram(self.queue.remove(&k).unwrap()) + } else if let Some(&t) = self.queue.keys().next() { + Output::Callback(t - now) + } else { + Output::None + } + } +} + +impl Debug for Delay { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("delay") + } +} diff --git a/third_party/rust/neqo-transport/tests/sim/drop.rs b/third_party/rust/neqo-transport/tests/sim/drop.rs new file mode 100644 index 000000000000..d42913d99d62 --- /dev/null +++ b/third_party/rust/neqo-transport/tests/sim/drop.rs @@ -0,0 +1,71 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(clippy::module_name_repetitions)] + +use super::{Node, Rng}; +use neqo_common::{qtrace, Datagram}; +use neqo_transport::Output; +use std::fmt::{self, Debug}; +use std::time::Instant; + +/// A random dropper. +pub struct Drop { + threshold: u64, + max: u64, + rng: Option, +} + +impl Drop { + /// Make a new random drop generator. Each `drop` is called, this generates a + /// random value between 0 and `max` (exclusive). If this value is less than + /// `threshold` a value of `true` is returned. + pub fn new(threshold: u64, max: u64) -> Self { + Self { + threshold, + max, + rng: None, + } + } + + /// Generate random drops with the given percentage. + pub fn percentage(pct: u8) -> Self { + // Multiply by 10 so that the random number generator works more efficiently. + Self::new(u64::from(pct) * 10, 1000) + } + + pub fn drop(&mut self) -> bool { + let mut rng = self.rng.as_ref().unwrap().borrow_mut(); + let r = rng.random_from(0..self.max); + r < self.threshold + } +} + +impl Node for Drop { + fn init(&mut self, rng: Rng, _now: Instant) { + self.rng = Some(rng); + } + + // Pass any datagram provided directly out, but drop some of them. + fn process(&mut self, d: Option, _now: Instant) -> Output { + if let Some(dgram) = d { + if self.drop() { + qtrace!("drop {}", dgram.len()); + Output::None + } else { + Output::Datagram(dgram) + } + } else { + Output::None + } + } +} + +impl Debug for Drop { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("drop") + } +} diff --git a/third_party/rust/neqo-transport/tests/sim/mod.rs b/third_party/rust/neqo-transport/tests/sim/mod.rs new file mode 100644 index 000000000000..95754107dcd9 --- /dev/null +++ b/third_party/rust/neqo-transport/tests/sim/mod.rs @@ -0,0 +1,225 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Tests with simulated network +#![cfg_attr(feature = "deny-warnings", deny(warnings))] +#![warn(clippy::pedantic)] + +pub mod connection; +mod delay; +mod drop; +pub mod rng; +mod taildrop; + +use neqo_common::{qdebug, qtrace, Datagram, Encoder}; +use neqo_transport::Output; +use rng::Random; +use std::cell::RefCell; +use std::cmp::min; +use std::convert::TryFrom; +use std::fmt::Debug; +use std::rc::Rc; +use std::time::{Duration, Instant}; +use test_fixture::{self, now}; + +use NodeState::{Active, Idle, Waiting}; + +pub mod network { + pub use super::delay::Delay; + pub use super::drop::Drop; + pub use super::taildrop::TailDrop; +} + +type Rng = Rc>; + +/// A macro that turns a list of values into boxed versions of the same. +#[macro_export] +macro_rules! boxed { + [$($v:expr),+ $(,)?] => { + vec![ $( Box::new($v) as _ ),+ ] + }; +} + +/// Create a simulation test case. This takes either two or three arguments. +/// The two argument form takes a bare name (`ident`), a comma, and an array of +/// items that implement `Node`. +/// The three argument form adds a setup block that can be used to construct a +/// complex value that is then shared between all nodes. The values in the +/// three-argument form have to be closures (or functions) that accept a reference +/// to the value returned by the setup. +#[macro_export] +macro_rules! simulate { + ($n:ident, [ $($v:expr),+ $(,)? ] $(,)?) => { + simulate!($n, (), [ $(|_| $v),+ ]); + }; + ($n:ident, $setup:expr, [ $( $v:expr ),+ $(,)? ] $(,)?) => { + #[test] + fn $n() { + let fixture = $setup; + let mut nodes: Vec> = Vec::new(); + $( + let f: Box _> = Box::new($v); + nodes.push(Box::new(f(&fixture))); + )* + let mut sim = Simulator::new(stringify!($n), nodes); + if let Ok(seed) = std::env::var("SIMULATION_SEED") { + sim.seed_str(seed); + } + sim.run(); + } + }; +} + +pub trait Node: Debug { + fn init(&mut self, _rng: Rng, _now: Instant) {} + /// Perform processing. This optionally takes a datagram and produces either + /// another data, a time that the simulator needs to wait, or nothing. + fn process(&mut self, d: Option, now: Instant) -> Output; + /// An node can report when it considers itself "done". + fn done(&self) -> bool { + true + } + fn print_summary(&self, _test_name: &str) {} +} + +/// The state of a single node. Nodes will be activated if they are `Active` +/// or if the previous node in the loop generated a datagram. Nodes that return +/// `true` from `Node::done` will be activated as normal. +#[derive(Debug, PartialEq)] +enum NodeState { + /// The node just produced a datagram. It should be activated again as soon as possible. + Active, + /// The node is waiting. + Waiting(Instant), + /// The node became idle. + Idle, +} + +#[derive(Debug)] +struct NodeHolder { + node: Box, + state: NodeState, +} + +impl NodeHolder { + fn ready(&self, now: Instant) -> bool { + match self.state { + Active => true, + Waiting(t) => t >= now, + Idle => false, + } + } +} + +pub struct Simulator { + name: String, + nodes: Vec, + rng: Rng, +} + +impl Simulator { + pub fn new(name: impl AsRef, nodes: impl IntoIterator>) -> Self { + let name = String::from(name.as_ref()); + // The first node is marked as Active, the rest are idle. + let mut it = nodes.into_iter(); + let nodes = it + .next() + .map(|node| NodeHolder { + node, + state: Active, + }) + .into_iter() + .chain(it.map(|node| NodeHolder { node, state: Idle })) + .collect::>(); + Self { + name, + nodes, + rng: Rc::default(), + } + } + + pub fn seed(&mut self, seed: [u8; 32]) { + self.rng = Rc::new(RefCell::new(Random::new(seed))); + } + + /// Seed from a hex string. + /// Though this is convenient, it panics if this isn't a 64 character hex string. + pub fn seed_str(&mut self, seed: impl AsRef) { + let seed = Encoder::from_hex(seed); + self.seed(<[u8; 32]>::try_from(&seed[..]).unwrap()); + } + + fn next_time(&self, now: Instant) -> Instant { + let mut next = None; + for n in &self.nodes { + match n.state { + Idle => continue, + Active => return now, + Waiting(a) => next = Some(next.map_or(a, |b| min(a, b))), + } + } + let next = next.expect("a node cannot be idle and not done"); + qdebug!([self.name], "advancing time by {:?}", next - now); + next + } + + /// Runs the simulation. + pub fn run(mut self) -> Duration { + let start = now(); + let mut now = start; + let mut dgram = None; + + for n in &mut self.nodes { + n.node.init(self.rng.clone(), now); + } + println!("{}: seed {}", self.name, self.rng.borrow().seed_str()); + + let real_start = Instant::now(); + loop { + for n in &mut self.nodes { + if dgram.is_none() && !n.ready(now) { + qdebug!([self.name], "skipping {:?}", n.node); + continue; + } + + qdebug!([self.name], "processing {:?}", n.node); + let res = n.node.process(dgram.take(), now); + n.state = match res { + Output::Datagram(d) => { + qtrace!([self.name], " => datagram {}", d.len()); + dgram = Some(d); + Active + } + Output::Callback(delay) => { + qtrace!([self.name], " => callback {:?}", delay); + assert_ne!(delay, Duration::new(0, 0)); + Waiting(now + delay) + } + Output::None => { + qtrace!([self.name], " => nothing"); + assert!(n.node.done(), "nodes have to be done when they go idle"); + Idle + } + }; + } + + if self.nodes.iter().all(|n| n.node.done()) { + let real_elapsed = Instant::now() - real_start; + println!("{}: real elapsed time: {:?}", self.name, real_elapsed); + let elapsed = now - start; + println!("{}: simulated elapsed time: {:?}", self.name, elapsed); + for n in &self.nodes { + n.node.print_summary(&self.name); + } + return elapsed; + } + + if dgram.is_none() { + now = self.next_time(now); + } + } + } +} diff --git a/third_party/rust/neqo-transport/tests/sim/net.rs b/third_party/rust/neqo-transport/tests/sim/net.rs new file mode 100644 index 000000000000..754426f895f4 --- /dev/null +++ b/third_party/rust/neqo-transport/tests/sim/net.rs @@ -0,0 +1,111 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use super::rng::RandomDuration; +use super::{Node, Rng}; +use neqo_common::Datagram; +use neqo_transport::Output; +use std::collections::BTreeMap; +use std::fmt::{self, Debug}; +use std::iter; +use std::ops::Range; +use std::time::{Duration, Instant}; + +/// +pub struct RandomDrop { + threshold: u64, + max: u64, + rng: Rng, +} + +impl RandomDuration { + /// Make a new random `Duration` generator. This asserts if the range provided + /// is inverted (i.e., `bounds.start > bounds.end`), or spans 2^64 + /// or more nanoseconds. + /// A zero-length range means that random values won't be taken from the Rng + pub fn new(bounds: Range, rng: Rng) -> Self { + let max = u64::try_from((bounds.end - bounds.start).as_nanos()).unwrap(); + Self { + start: bounds.start, + max, + rng, + } + } + + fn next(&mut self) -> Duration { + let r = if self.max == 0 { + Duration::new(0, 0) + } else { + self.rng.borrow_mut().random_from(0..self.max) + } + self.start + Duration::from_nanos(r) + } +} + +enum DelayState { + New(Range), + Ready(RandomDuration), +} + +pub struct Delay { + state: DelayState, + queue: BTreeMap, +} + +impl Delay +{ + pub fn new(bounds: Range) -> Self + { + Self { + State: DelayState::New(bounds), + queue: BTreeMap::default(), + } + } + + fn insert(&mut self, d: Datagram, now: Instant) { + let mut t = if let State::Ready(r) = self.state { + now + self.source.next() + } else { + unreachable!(); + } + while self.queue.contains_key(&t) { + // This is a little inefficient, but it avoids drops on collisions, + // which are super-common for a fixed delay. + t += Duration::from_nanos(1); + } + self.queue.insert(t, d); + } +} + +impl Node for Delay +{ + fn init(&mut self, rng: Rng, now: Instant) { + if let DelayState::New(bounds) = self.state { + self.state = RandomDuration::new(bounds); + } else { + unreachable!(); + } + } + + fn process(&mut self, d: Option, now: Instant) -> Output { + if let Some(dgram) = d { + self.insert(dgram, now); + } + if let Some((&k, _)) = self.queue.range(..now).nth(0) { + Output::Datagram(self.queue.remove(&k).unwrap()) + } else if let Some(&t) = self.queue.keys().nth(0) { + Output::Callback(t - now) + } else { + Output::None + } + } +} + +impl Debug for Delay { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("delay") + } +} diff --git a/third_party/rust/neqo-transport/tests/sim/rng.rs b/third_party/rust/neqo-transport/tests/sim/rng.rs new file mode 100644 index 000000000000..d314e8b36fee --- /dev/null +++ b/third_party/rust/neqo-transport/tests/sim/rng.rs @@ -0,0 +1,81 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use neqo_common::Decoder; +use std::convert::TryFrom; +use std::ops::Range; + +/// An implementation of a xoshiro256** pseudorandom generator. +pub struct Random { + state: [u64; 4], +} + +impl Random { + pub fn new(seed: [u8; 32]) -> Self { + assert!(seed.iter().any(|&x| x != 0)); + let mut dec = Decoder::from(&seed); + Self { + state: [ + dec.decode_uint(8).unwrap(), + dec.decode_uint(8).unwrap(), + dec.decode_uint(8).unwrap(), + dec.decode_uint(8).unwrap(), + ], + } + } + + pub fn random(&mut self) -> u64 { + let result = (self.state[1].overflowing_mul(5).0) + .rotate_right(7) + .overflowing_mul(9) + .0; + let t = self.state[1] << 17; + + self.state[2] ^= self.state[0]; + self.state[3] ^= self.state[1]; + self.state[1] ^= self.state[2]; + self.state[0] ^= self.state[3]; + + self.state[2] ^= t; + self.state[3] = self.state[3].rotate_right(45); + + result + } + + /// Generate a random value from the range. + /// If the range is empty or inverted (`range.start > range.end`), then + /// this returns the value of `range.start` without generating any random values. + pub fn random_from(&mut self, range: Range) -> u64 { + let max = range.end.saturating_sub(range.start); + if max == 0 { + return range.start; + } + + let shift = (max - 1).leading_zeros(); + assert_ne!(max, 0); + loop { + let r = self.random() >> shift; + if r < max { + return range.start + r; + } + } + } + + /// Get the seed necessary to continue from this point. + pub fn seed_str(&self) -> String { + format!( + "{:8x}{:8x}{:8x}{:8x}", + self.state[0], self.state[1], self.state[2], self.state[3], + ) + } +} + +impl Default for Random { + fn default() -> Self { + let buf = neqo_crypto::random(32); + Random::new(<[u8; 32]>::try_from(&buf[..]).unwrap()) + } +} diff --git a/third_party/rust/neqo-transport/tests/sim/taildrop.rs b/third_party/rust/neqo-transport/tests/sim/taildrop.rs new file mode 100644 index 000000000000..7346b2717827 --- /dev/null +++ b/third_party/rust/neqo-transport/tests/sim/taildrop.rs @@ -0,0 +1,184 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(clippy::module_name_repetitions)] + +use super::Node; +use neqo_common::{qtrace, Datagram}; +use neqo_transport::Output; +use std::cmp::max; +use std::collections::VecDeque; +use std::convert::TryFrom; +use std::fmt::{self, Debug}; +use std::time::{Duration, Instant}; + +/// One second in nanoseconds. +const ONE_SECOND_NS: u128 = 1_000_000_000; + +/// This models a link with a tail drop router at the front of it. +pub struct TailDrop { + /// An overhead associated with each entry. This accounts for + /// layer 2, IP, and UDP overheads. + overhead: usize, + /// The rate at which bytes egress the link, in bytes per second. + rate: usize, + /// The depth of the queue, in bytes. + capacity: usize, + + /// A counter for how many bytes are enqueued. + used: usize, + /// A queue of unsent bytes. + queue: VecDeque, + /// The time that the next datagram can enter the link. + next_deque: Option, + + /// Any sub-ns delay from the last enqueue. + sub_ns_delay: u32, + /// The time it takes a byte to exit the other end of the link. + delay: Duration, + /// The packets that are on the link and when they can be delivered. + on_link: VecDeque<(Instant, Datagram)>, + + /// The number of packets received. + received: usize, + /// The number of packets dropped. + dropped: usize, + /// The number of packets delivered. + delivered: usize, + /// The maximum amount of queue capacity ever used. + /// As packets leave the queue as soon as they start being used, this doesn't + /// count them. + maxq: usize, +} + +impl TailDrop { + /// Make a new taildrop node with the given rate, queue capacity, and link delay. + pub fn new(rate: usize, capacity: usize, delay: Duration) -> Self { + Self { + overhead: 64, + rate, + capacity, + used: 0, + queue: VecDeque::new(), + next_deque: None, + sub_ns_delay: 0, + delay, + on_link: VecDeque::new(), + received: 0, + dropped: 0, + delivered: 0, + maxq: 0, + } + } + + /// A tail drop queue on a 10Mbps link (approximated to 1 million bytes per second) + /// with a fat 32k buffer (about 30ms), and the default forward delay of 50ms. + pub fn dsl_uplink() -> Self { + TailDrop::new(1_000_000, 32_768, Duration::from_millis(50)) + } + + /// Cut downlink to one fifth of the uplink (2Mbps), and reduce the buffer to 1/4. + pub fn dsl_downlink() -> Self { + TailDrop::new(200_000, 8_192, Duration::from_millis(50)) + } + + /// How "big" is this datagram, accounting for overheads. + /// This approximates by using the same overhead for storing in the queue + /// and for sending on the wire. + fn size(&self, d: &Datagram) -> usize { + d.len() + self.overhead + } + + /// Start sending a datagram. + fn send(&mut self, d: Datagram, now: Instant) { + // How many bytes are we "transmitting"? + let sz = u128::try_from(self.size(&d)).unwrap(); + + // Calculate how long it takes to put the packet on the link. + // Perform the calculation based on 2^32 seconds and save any remainder. + // This ensures that high rates and small packets don't result in rounding + // down times too badly. + // Duration consists of a u64 and a u32, so we have 32 high bits to spare. + let t = sz * (ONE_SECOND_NS << 32) / u128::try_from(self.rate).unwrap() + + u128::from(self.sub_ns_delay); + let send_ns = u64::try_from(t >> 32).unwrap(); + assert_ne!(send_ns, 0, "sending a packet takes <1ns"); + self.sub_ns_delay = u32::try_from(t & u128::from(u32::MAX)).unwrap(); + let deque_time = now + Duration::from_nanos(send_ns); + self.next_deque = Some(deque_time); + + // Now work out when the packet is fully received at the other end of + // the link. Setup to deliver the packet then. + let delivery_time = deque_time + self.delay; + self.on_link.push_back((delivery_time, d)); + } + + /// Enqueue for sending. Maybe. If this overflows the queue, drop it instead. + fn maybe_enqueue(&mut self, d: Datagram, now: Instant) { + self.received += 1; + if self.next_deque.is_none() { + // Nothing in the queue and nothing still sending. + debug_assert!(self.queue.is_empty()); + self.send(d, now); + } else if self.used + self.size(&d) <= self.capacity { + self.used += self.size(&d); + self.maxq = max(self.maxq, self.used); + self.queue.push_back(d); + } else { + qtrace!("taildrop dropping {} bytes", d.len()); + self.dropped += 1; + } + } + + /// If the last packet that was sending has been sent, start sending + /// the next one. + fn maybe_send(&mut self, now: Instant) { + if self.next_deque.as_ref().map_or(false, |t| *t <= now) { + if let Some(d) = self.queue.pop_front() { + self.used -= self.size(&d); + self.send(d, now); + } else { + self.next_deque = None; + self.sub_ns_delay = 0; + } + } + } +} + +impl Node for TailDrop { + fn process(&mut self, d: Option, now: Instant) -> Output { + if let Some(dgram) = d { + self.maybe_enqueue(dgram, now); + } + + self.maybe_send(now); + + if let Some((t, _)) = self.on_link.front() { + if *t <= now { + let (_, d) = self.on_link.pop_front().unwrap(); + self.delivered += 1; + Output::Datagram(d) + } else { + Output::Callback(*t - now) + } + } else { + Output::None + } + } + + fn print_summary(&self, test_name: &str) { + println!( + "{}: taildrop: rx {} drop {} tx {} maxq {}", + test_name, self.received, self.dropped, self.delivered, self.maxq, + ); + } +} + +impl Debug for TailDrop { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("taildrop") + } +} diff --git a/third_party/rust/which/.cargo-checksum.json b/third_party/rust/which/.cargo-checksum.json new file mode 100644 index 000000000000..edcbf0949d7a --- /dev/null +++ b/third_party/rust/which/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"9be96b396b493821236993d76ff6eb2cbce0656755c1f5f060760fc096c33abc","LICENSE.txt":"0041560f5d419c30e1594567f3b7ac2bc078ff6a68f437e0348ba85d9cf99112","README.md":"c2a28e930966fd95eda4241ef8eb91a5a8cd8ba1cece67da736b13978503e3e9","appveyor.yml":"833f1ff9263272ab1e51bd8297c9c3fe7a3f104b89c95f5740b186e0b8274210","src/checker.rs":"1bb01f5f143d848f16ba136f44f4f671220120cfb8fb8e14fc69b38e161b82e5","src/error.rs":"e2a05f24be87abc908753f91b3d37dff4fc4a232e9c5bee3dfd33112ea21cc97","src/finder.rs":"7d1460473c3217f788b2a62ab2f5359af78cca6c5f5106b5c6a936b1c7cbd331","src/helper.rs":"1ea08b0c4675ac46ee71ef2225bdef8a6d055a9644d5d42ba94f0c8e9d2d3279","src/lib.rs":"3a305ed0359918dc4b1fac385b5a670cb0629c61707edce2db66e17db3edbd4c","tests/basic.rs":"a0c49fa4348761b24598916dfcaba7064fd838278c1f428101e587931fc1b20a"},"package":"d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"} \ No newline at end of file diff --git a/third_party/rust/which/Cargo.toml b/third_party/rust/which/Cargo.toml new file mode 100644 index 000000000000..567792b70dc0 --- /dev/null +++ b/third_party/rust/which/Cargo.toml @@ -0,0 +1,36 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "which" +version = "3.1.1" +authors = ["Harry Fei "] +description = "A Rust equivalent of Unix command \"which\". Locate installed executable in cross platforms." +documentation = "https://docs.rs/which/" +readme = "README.md" +keywords = ["which", "which-rs", "unix", "command"] +categories = ["os", "filesystem"] +license = "MIT" +repository = "https://github.com/harryfei/which-rs.git" +[dependencies.failure] +version = "0.1.7" +features = ["std"] +optional = true +default-features = false + +[dependencies.libc] +version = "0.2.65" +[dev-dependencies.tempdir] +version = "0.3.7" + +[features] +default = ["failure"] diff --git a/third_party/rust/which/LICENSE.txt b/third_party/rust/which/LICENSE.txt new file mode 100644 index 000000000000..369139bbd1ec --- /dev/null +++ b/third_party/rust/which/LICENSE.txt @@ -0,0 +1,19 @@ +Copyright (c) 2015 fangyuanziti + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/third_party/rust/which/README.md b/third_party/rust/which/README.md new file mode 100644 index 000000000000..2c8d7e11f2b1 --- /dev/null +++ b/third_party/rust/which/README.md @@ -0,0 +1,34 @@ +[![Travis Build Status](https://travis-ci.org/harryfei/which-rs.svg?branch=master)](https://travis-ci.org/harryfei/which-rs) +[![Appveyor Build status](https://ci.appveyor.com/api/projects/status/1y40b135iaixs9x6?svg=true)](https://ci.appveyor.com/project/HarryFei/which-rs) + +# which + +A Rust equivalent of Unix command "which". Locate installed executable in cross platforms. + +## Support platforms + +* Linux +* Windows +* macOS + +## Example + +To find which rustc exectable binary is using. + +``` rust +use which::which; + +let result = which::which("rustc").unwrap(); +assert_eq!(result, PathBuf::from("/usr/bin/rustc")); +``` + +## Errors + +By default this crate exposes a [`failure`] based error. This is optional, disable the default +features to get an error type implementing the standard library `Error` trait. + +[`failure`]: https://crates.io/crates/failure + +## Documentation + +The documentation is [available online](https://docs.rs/which/). diff --git a/third_party/rust/which/appveyor.yml b/third_party/rust/which/appveyor.yml new file mode 100644 index 000000000000..e8beb0767ca1 --- /dev/null +++ b/third_party/rust/which/appveyor.yml @@ -0,0 +1,29 @@ +os: Visual Studio 2015 + +environment: + matrix: + - channel: stable + target: x86_64-pc-windows-msvc + - channel: stable + target: i686-pc-windows-msvc + - channel: stable + target: x86_64-pc-windows-gnu + - channel: stable + target: i686-pc-windows-gnu +install: + # Set PATH for MinGW toolset + - if %target% == x86_64-pc-windows-gnu set PATH=%PATH%;C:\msys64\mingw64\bin + - if %target% == i686-pc-windows-gnu set PATH=%PATH%;C:\msys64\mingw32\bin + + # Install Rust toolset + - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe + - rustup-init -yv --default-toolchain %channel% --default-host %target% + - set PATH=%PATH%;%USERPROFILE%\.cargo\bin + - rustc -vV + - cargo -vV + +build: false + +test_script: + - cargo test + - cargo test --no-default-features diff --git a/third_party/rust/which/src/checker.rs b/third_party/rust/which/src/checker.rs new file mode 100644 index 000000000000..602171122d81 --- /dev/null +++ b/third_party/rust/which/src/checker.rs @@ -0,0 +1,70 @@ +use finder::Checker; +#[cfg(unix)] +use libc; +#[cfg(unix)] +use std::ffi::CString; +use std::fs; +#[cfg(unix)] +use std::os::unix::ffi::OsStrExt; +use std::path::Path; + +pub struct ExecutableChecker; + +impl ExecutableChecker { + pub fn new() -> ExecutableChecker { + ExecutableChecker + } +} + +impl Checker for ExecutableChecker { + #[cfg(unix)] + fn is_valid(&self, path: &Path) -> bool { + CString::new(path.as_os_str().as_bytes()) + .and_then(|c| Ok(unsafe { libc::access(c.as_ptr(), libc::X_OK) == 0 })) + .unwrap_or(false) + } + + #[cfg(windows)] + fn is_valid(&self, _path: &Path) -> bool { + true + } +} + +pub struct ExistedChecker; + +impl ExistedChecker { + pub fn new() -> ExistedChecker { + ExistedChecker + } +} + +impl Checker for ExistedChecker { + fn is_valid(&self, path: &Path) -> bool { + fs::metadata(path) + .map(|metadata| metadata.is_file()) + .unwrap_or(false) + } +} + +pub struct CompositeChecker { + checkers: Vec>, +} + +impl CompositeChecker { + pub fn new() -> CompositeChecker { + CompositeChecker { + checkers: Vec::new(), + } + } + + pub fn add_checker(mut self, checker: Box) -> CompositeChecker { + self.checkers.push(checker); + self + } +} + +impl Checker for CompositeChecker { + fn is_valid(&self, path: &Path) -> bool { + self.checkers.iter().all(|checker| checker.is_valid(path)) + } +} diff --git a/third_party/rust/which/src/error.rs b/third_party/rust/which/src/error.rs new file mode 100644 index 000000000000..c75fe4cd1b7e --- /dev/null +++ b/third_party/rust/which/src/error.rs @@ -0,0 +1,92 @@ +#[cfg(feature = "failure")] +use failure::{Backtrace, Context, Fail}; +use std; +use std::fmt::{self, Display}; + +#[derive(Debug)] +pub struct Error { + #[cfg(feature = "failure")] + inner: Context, + #[cfg(not(feature = "failure"))] + inner: ErrorKind, +} + +// To suppress false positives from cargo-clippy +#[cfg_attr(feature = "cargo-clippy", allow(empty_line_after_outer_attr))] +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub enum ErrorKind { + BadAbsolutePath, + BadRelativePath, + CannotFindBinaryPath, + CannotGetCurrentDir, + CannotCanonicalize, +} + +#[cfg(feature = "failure")] +impl Fail for ErrorKind {} + +impl Display for ErrorKind { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let display = match *self { + ErrorKind::BadAbsolutePath => "Bad absolute path", + ErrorKind::BadRelativePath => "Bad relative path", + ErrorKind::CannotFindBinaryPath => "Cannot find binary path", + ErrorKind::CannotGetCurrentDir => "Cannot get current directory", + ErrorKind::CannotCanonicalize => "Cannot canonicalize path", + }; + f.write_str(display) + } +} + +#[cfg(feature = "failure")] +impl Fail for Error { + fn cause(&self) -> Option<&dyn Fail> { + self.inner.cause() + } + + fn backtrace(&self) -> Option<&Backtrace> { + self.inner.backtrace() + } +} + +#[cfg(not(feature = "failure"))] +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + Display::fmt(&self.inner, f) + } +} + +impl Error { + pub fn kind(&self) -> ErrorKind { + #[cfg(feature = "failure")] + { + *self.inner.get_context() + } + #[cfg(not(feature = "failure"))] + { + self.inner + } + } +} + +impl From for Error { + fn from(kind: ErrorKind) -> Error { + Error { + #[cfg(feature = "failure")] + inner: Context::new(kind), + #[cfg(not(feature = "failure"))] + inner: kind, + } + } +} + +#[cfg(feature = "failure")] +impl From> for Error { + fn from(inner: Context) -> Error { + Error { inner } + } +} + +pub type Result = std::result::Result; diff --git a/third_party/rust/which/src/finder.rs b/third_party/rust/which/src/finder.rs new file mode 100644 index 000000000000..2519aa88c49c --- /dev/null +++ b/third_party/rust/which/src/finder.rs @@ -0,0 +1,155 @@ +use error::*; +#[cfg(windows)] +use helper::has_executable_extension; +use std::env; +use std::ffi::OsStr; +#[cfg(windows)] +use std::ffi::OsString; +use std::iter; +use std::path::{Path, PathBuf}; + +pub trait Checker { + fn is_valid(&self, path: &Path) -> bool; +} + +trait PathExt { + fn has_separator(&self) -> bool; + + fn to_absolute

(self, cwd: P) -> PathBuf + where + P: AsRef; +} + +impl PathExt for PathBuf { + fn has_separator(&self) -> bool { + self.components().count() > 1 + } + + fn to_absolute

(self, cwd: P) -> PathBuf + where + P: AsRef, + { + if self.is_absolute() { + self + } else { + let mut new_path = PathBuf::from(cwd.as_ref()); + new_path.push(self); + new_path + } + } +} + +pub struct Finder; + +impl Finder { + pub fn new() -> Finder { + Finder + } + + pub fn find( + &self, + binary_name: T, + paths: Option, + cwd: V, + binary_checker: &dyn Checker, + ) -> Result + where + T: AsRef, + U: AsRef, + V: AsRef, + { + let path = PathBuf::from(&binary_name); + + let binary_path_candidates: Box> = if path.has_separator() { + // Search binary in cwd if the path have a path separator. + let candidates = Self::cwd_search_candidates(path, cwd).into_iter(); + Box::new(candidates) + } else { + // Search binary in PATHs(defined in environment variable). + let p = paths.ok_or(ErrorKind::CannotFindBinaryPath)?; + let paths: Vec<_> = env::split_paths(&p).collect(); + + let candidates = Self::path_search_candidates(path, paths).into_iter(); + + Box::new(candidates) + }; + + for p in binary_path_candidates { + // find a valid binary + if binary_checker.is_valid(&p) { + return Ok(p); + } + } + + // can't find any binary + return Err(ErrorKind::CannotFindBinaryPath.into()); + } + + fn cwd_search_candidates(binary_name: PathBuf, cwd: C) -> impl IntoIterator + where + C: AsRef, + { + let path = binary_name.to_absolute(cwd); + + Self::append_extension(iter::once(path)) + } + + fn path_search_candidates

( + binary_name: PathBuf, + paths: P, + ) -> impl IntoIterator + where + P: IntoIterator, + { + let new_paths = paths.into_iter().map(move |p| p.join(binary_name.clone())); + + Self::append_extension(new_paths) + } + + #[cfg(unix)] + fn append_extension

(paths: P) -> impl IntoIterator + where + P: IntoIterator, + { + paths + } + + #[cfg(windows)] + fn append_extension

(paths: P) -> impl IntoIterator + where + P: IntoIterator, + { + // Read PATHEXT env variable and split it into vector of String + let path_exts = + env::var_os("PATHEXT").unwrap_or(OsString::from(env::consts::EXE_EXTENSION)); + + let exe_extension_vec = env::split_paths(&path_exts) + .filter_map(|e| e.to_str().map(|e| e.to_owned())) + .collect::>(); + + paths + .into_iter() + .flat_map(move |p| -> Box> { + // Check if path already have executable extension + if has_executable_extension(&p, &exe_extension_vec) { + Box::new(iter::once(p)) + } else { + // Appended paths with windows executable extensions. + // e.g. path `c:/windows/bin` will expend to: + // c:/windows/bin.COM + // c:/windows/bin.EXE + // c:/windows/bin.CMD + // ... + let ps = exe_extension_vec.clone().into_iter().map(move |e| { + // Append the extension. + let mut p = p.clone().to_path_buf().into_os_string(); + p.push(e); + + PathBuf::from(p) + }); + + Box::new(ps) + } + }) + } +} diff --git a/third_party/rust/which/src/helper.rs b/third_party/rust/which/src/helper.rs new file mode 100644 index 000000000000..71658a00349f --- /dev/null +++ b/third_party/rust/which/src/helper.rs @@ -0,0 +1,40 @@ +use std::path::Path; + +/// Check if given path has extension which in the given vector. +pub fn has_executable_extension, S: AsRef>(path: T, exts_vec: &Vec) -> bool { + let ext = path.as_ref().extension().and_then(|e| e.to_str()); + match ext { + Some(ext) => exts_vec + .iter() + .any(|e| ext.eq_ignore_ascii_case(&e.as_ref()[1..])), + _ => false, + } +} + +#[cfg(test)] +mod test { + use super::*; + use std::path::PathBuf; + + #[test] + fn test_extension_in_extension_vector() { + // Case insensitive + assert!(has_executable_extension( + PathBuf::from("foo.exe"), + &vec![".COM", ".EXE", ".CMD"] + )); + + assert!(has_executable_extension( + PathBuf::from("foo.CMD"), + &vec![".COM", ".EXE", ".CMD"] + )); + } + + #[test] + fn test_extension_not_in_extension_vector() { + assert!(!has_executable_extension( + PathBuf::from("foo.bar"), + &vec![".COM", ".EXE", ".CMD"] + )); + } +} diff --git a/third_party/rust/which/src/lib.rs b/third_party/rust/which/src/lib.rs new file mode 100644 index 000000000000..42a6963e401e --- /dev/null +++ b/third_party/rust/which/src/lib.rs @@ -0,0 +1,271 @@ +//! which +//! +//! A Rust equivalent of Unix command `which(1)`. +//! # Example: +//! +//! To find which rustc executable binary is using: +//! +//! ``` norun +//! use which::which; +//! +//! let result = which::which("rustc").unwrap(); +//! assert_eq!(result, PathBuf::from("/usr/bin/rustc")); +//! +//! ``` + +#[cfg(feature = "failure")] +extern crate failure; +extern crate libc; + +#[cfg(feature = "failure")] +use failure::ResultExt; +mod checker; +mod error; +mod finder; +#[cfg(windows)] +mod helper; + +use std::env; +use std::fmt; +use std::path; + +use std::ffi::OsStr; + +use checker::CompositeChecker; +use checker::ExecutableChecker; +use checker::ExistedChecker; +pub use error::*; +use finder::Finder; + +/// Find a exectable binary's path by name. +/// +/// If given an absolute path, returns it if the file exists and is executable. +/// +/// If given a relative path, returns an absolute path to the file if +/// it exists and is executable. +/// +/// If given a string without path separators, looks for a file named +/// `binary_name` at each directory in `$PATH` and if it finds an executable +/// file there, returns it. +/// +/// # Example +/// +/// ``` norun +/// use which::which; +/// use std::path::PathBuf; +/// +/// let result = which::which("rustc").unwrap(); +/// assert_eq!(result, PathBuf::from("/usr/bin/rustc")); +/// +/// ``` +pub fn which>(binary_name: T) -> Result { + #[cfg(feature = "failure")] + let cwd = env::current_dir().context(ErrorKind::CannotGetCurrentDir)?; + #[cfg(not(feature = "failure"))] + let cwd = env::current_dir().map_err(|_| ErrorKind::CannotGetCurrentDir)?; + + which_in(binary_name, env::var_os("PATH"), &cwd) +} + +/// Find `binary_name` in the path list `paths`, using `cwd` to resolve relative paths. +pub fn which_in(binary_name: T, paths: Option, cwd: V) -> Result +where + T: AsRef, + U: AsRef, + V: AsRef, +{ + let binary_checker = CompositeChecker::new() + .add_checker(Box::new(ExistedChecker::new())) + .add_checker(Box::new(ExecutableChecker::new())); + + let finder = Finder::new(); + + finder.find(binary_name, paths, cwd, &binary_checker) +} + +/// An owned, immutable wrapper around a `PathBuf` containing the path of an executable. +/// +/// The constructed `PathBuf` is the output of `which` or `which_in`, but `which::Path` has the +/// advantage of being a type distinct from `std::path::Path` and `std::path::PathBuf`. +/// +/// It can be beneficial to use `which::Path` instead of `std::path::Path` when you want the type +/// system to enforce the need for a path that exists and points to a binary that is executable. +/// +/// Since `which::Path` implements `Deref` for `std::path::Path`, all methods on `&std::path::Path` +/// are also available to `&which::Path` values. +#[derive(Clone, PartialEq)] +pub struct Path { + inner: path::PathBuf, +} + +impl Path { + /// Returns the path of an executable binary by name. + /// + /// This calls `which` and maps the result into a `Path`. + pub fn new>(binary_name: T) -> Result { + which(binary_name).map(|inner| Path { inner }) + } + + /// Returns the path of an executable binary by name in the path list `paths` and using the + /// current working directory `cwd` to resolve relative paths. + /// + /// This calls `which_in` and maps the result into a `Path`. + pub fn new_in(binary_name: T, paths: Option, cwd: V) -> Result + where + T: AsRef, + U: AsRef, + V: AsRef, + { + which_in(binary_name, paths, cwd).map(|inner| Path { inner }) + } + + /// Returns a reference to a `std::path::Path`. + pub fn as_path(&self) -> &path::Path { + self.inner.as_path() + } + + /// Consumes the `which::Path`, yielding its underlying `std::path::PathBuf`. + pub fn into_path_buf(self) -> path::PathBuf { + self.inner + } +} + +impl fmt::Debug for Path { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&self.inner, f) + } +} + +impl std::ops::Deref for Path { + type Target = path::Path; + + fn deref(&self) -> &path::Path { + self.inner.deref() + } +} + +impl AsRef for Path { + fn as_ref(&self) -> &path::Path { + self.as_path() + } +} + +impl AsRef for Path { + fn as_ref(&self) -> &OsStr { + self.as_os_str() + } +} + +impl Eq for Path {} + +impl PartialEq for Path { + fn eq(&self, other: &path::PathBuf) -> bool { + self.inner == *other + } +} + +impl PartialEq for path::PathBuf { + fn eq(&self, other: &Path) -> bool { + *self == other.inner + } +} + +/// An owned, immutable wrapper around a `PathBuf` containing the _canonical_ path of an +/// executable. +/// +/// The constructed `PathBuf` is the result of `which` or `which_in` followed by +/// `Path::canonicalize`, but `CanonicalPath` has the advantage of being a type distinct from +/// `std::path::Path` and `std::path::PathBuf`. +/// +/// It can be beneficial to use `CanonicalPath` instead of `std::path::Path` when you want the type +/// system to enforce the need for a path that exists, points to a binary that is executable, is +/// absolute, has all components normalized, and has all symbolic links resolved +/// +/// Since `CanonicalPath` implements `Deref` for `std::path::Path`, all methods on +/// `&std::path::Path` are also available to `&CanonicalPath` values. +#[derive(Clone, PartialEq)] +pub struct CanonicalPath { + inner: path::PathBuf, +} + +impl CanonicalPath { + /// Returns the canonical path of an executable binary by name. + /// + /// This calls `which` and `Path::canonicalize` and maps the result into a `CanonicalPath`. + pub fn new>(binary_name: T) -> Result { + which(binary_name) + .and_then(|p| { + p.canonicalize() + .map_err(|_| ErrorKind::CannotCanonicalize.into()) + }) + .map(|inner| CanonicalPath { inner }) + } + + /// Returns the canonical path of an executable binary by name in the path list `paths` and + /// using the current working directory `cwd` to resolve relative paths. + /// + /// This calls `which` and `Path::canonicalize` and maps the result into a `CanonicalPath`. + pub fn new_in(binary_name: T, paths: Option, cwd: V) -> Result + where + T: AsRef, + U: AsRef, + V: AsRef, + { + which_in(binary_name, paths, cwd) + .and_then(|p| { + p.canonicalize() + .map_err(|_| ErrorKind::CannotCanonicalize.into()) + }) + .map(|inner| CanonicalPath { inner }) + } + + /// Returns a reference to a `std::path::Path`. + pub fn as_path(&self) -> &path::Path { + self.inner.as_path() + } + + /// Consumes the `which::CanonicalPath`, yielding its underlying `std::path::PathBuf`. + pub fn into_path_buf(self) -> path::PathBuf { + self.inner + } +} + +impl fmt::Debug for CanonicalPath { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&self.inner, f) + } +} + +impl std::ops::Deref for CanonicalPath { + type Target = path::Path; + + fn deref(&self) -> &path::Path { + self.inner.deref() + } +} + +impl AsRef for CanonicalPath { + fn as_ref(&self) -> &path::Path { + self.as_path() + } +} + +impl AsRef for CanonicalPath { + fn as_ref(&self) -> &OsStr { + self.as_os_str() + } +} + +impl Eq for CanonicalPath {} + +impl PartialEq for CanonicalPath { + fn eq(&self, other: &path::PathBuf) -> bool { + self.inner == *other + } +} + +impl PartialEq for path::PathBuf { + fn eq(&self, other: &CanonicalPath) -> bool { + *self == other.inner + } +} diff --git a/third_party/rust/which/tests/basic.rs b/third_party/rust/which/tests/basic.rs new file mode 100644 index 000000000000..b6ba30d4f589 --- /dev/null +++ b/third_party/rust/which/tests/basic.rs @@ -0,0 +1,307 @@ +extern crate tempdir; +extern crate which; + +use std::env; +use std::ffi::{OsStr, OsString}; +use std::fs; +use std::io; +use std::path::{Path, PathBuf}; +use tempdir::TempDir; + +struct TestFixture { + /// Temp directory. + pub tempdir: TempDir, + /// $PATH + pub paths: OsString, + /// Binaries created in $PATH + pub bins: Vec, +} + +const SUBDIRS: &'static [&'static str] = &["a", "b", "c"]; +const BIN_NAME: &'static str = "bin"; + +#[cfg(unix)] +fn mk_bin(dir: &Path, path: &str, extension: &str) -> io::Result { + use std::os::unix::fs::OpenOptionsExt; + let bin = dir.join(path).with_extension(extension); + fs::OpenOptions::new() + .write(true) + .create(true) + .mode(0o666 | (libc::S_IXUSR as u32)) + .open(&bin) + .and_then(|_f| bin.canonicalize()) +} + +fn touch(dir: &Path, path: &str, extension: &str) -> io::Result { + let b = dir.join(path).with_extension(extension); + fs::File::create(&b).and_then(|_f| b.canonicalize()) +} + +#[cfg(windows)] +fn mk_bin(dir: &Path, path: &str, extension: &str) -> io::Result { + touch(dir, path, extension) +} + +impl TestFixture { + // tmp/a/bin + // tmp/a/bin.exe + // tmp/a/bin.cmd + // tmp/b/bin + // tmp/b/bin.exe + // tmp/b/bin.cmd + // tmp/c/bin + // tmp/c/bin.exe + // tmp/c/bin.cmd + pub fn new() -> TestFixture { + let tempdir = TempDir::new("which_tests").unwrap(); + let mut builder = fs::DirBuilder::new(); + builder.recursive(true); + let mut paths = vec![]; + let mut bins = vec![]; + for d in SUBDIRS.iter() { + let p = tempdir.path().join(d); + builder.create(&p).unwrap(); + bins.push(mk_bin(&p, &BIN_NAME, "").unwrap()); + bins.push(mk_bin(&p, &BIN_NAME, "exe").unwrap()); + bins.push(mk_bin(&p, &BIN_NAME, "cmd").unwrap()); + paths.push(p); + } + TestFixture { + tempdir: tempdir, + paths: env::join_paths(paths).unwrap(), + bins: bins, + } + } + + #[allow(dead_code)] + pub fn touch(&self, path: &str, extension: &str) -> io::Result { + touch(self.tempdir.path(), &path, &extension) + } + + pub fn mk_bin(&self, path: &str, extension: &str) -> io::Result { + mk_bin(self.tempdir.path(), &path, &extension) + } +} + +fn _which>(f: &TestFixture, path: T) -> which::Result { + which::CanonicalPath::new_in(path, Some(f.paths.clone()), f.tempdir.path()) +} + +#[test] +#[cfg(unix)] +fn it_works() { + use std::process::Command; + let result = which::Path::new("rustc"); + assert!(result.is_ok()); + + let which_result = Command::new("which").arg("rustc").output(); + + assert_eq!( + String::from(result.unwrap().to_str().unwrap()), + String::from_utf8(which_result.unwrap().stdout) + .unwrap() + .trim() + ); +} + +#[test] +#[cfg(unix)] +fn test_which() { + let f = TestFixture::new(); + assert_eq!(_which(&f, &BIN_NAME).unwrap(), f.bins[0]) +} + +#[test] +#[cfg(windows)] +fn test_which() { + let f = TestFixture::new(); + assert_eq!(_which(&f, &BIN_NAME).unwrap(), f.bins[1]) +} + +#[test] +#[cfg(unix)] +fn test_which_extension() { + let f = TestFixture::new(); + let b = Path::new(&BIN_NAME).with_extension(""); + assert_eq!(_which(&f, &b).unwrap(), f.bins[0]) +} + +#[test] +#[cfg(windows)] +fn test_which_extension() { + let f = TestFixture::new(); + let b = Path::new(&BIN_NAME).with_extension("cmd"); + assert_eq!(_which(&f, &b).unwrap(), f.bins[2]) +} + +#[test] +fn test_which_not_found() { + let f = TestFixture::new(); + assert!(_which(&f, "a").is_err()); +} + +#[test] +fn test_which_second() { + let f = TestFixture::new(); + let b = f.mk_bin("b/another", env::consts::EXE_EXTENSION).unwrap(); + assert_eq!(_which(&f, "another").unwrap(), b); +} + +#[test] +#[cfg(unix)] +fn test_which_absolute() { + let f = TestFixture::new(); + assert_eq!( + _which(&f, &f.bins[3]).unwrap(), + f.bins[3].canonicalize().unwrap() + ); +} + +#[test] +#[cfg(windows)] +fn test_which_absolute() { + let f = TestFixture::new(); + assert_eq!( + _which(&f, &f.bins[4]).unwrap(), + f.bins[4].canonicalize().unwrap() + ); +} + +#[test] +#[cfg(windows)] +fn test_which_absolute_path_case() { + // Test that an absolute path with an uppercase extension + // is accepted. + let f = TestFixture::new(); + let p = &f.bins[4]; + assert_eq!(_which(&f, &p).unwrap(), f.bins[4].canonicalize().unwrap()); +} + +#[test] +#[cfg(unix)] +fn test_which_absolute_extension() { + let f = TestFixture::new(); + // Don't append EXE_EXTENSION here. + let b = f.bins[3].parent().unwrap().join(&BIN_NAME); + assert_eq!(_which(&f, &b).unwrap(), f.bins[3].canonicalize().unwrap()); +} + +#[test] +#[cfg(windows)] +fn test_which_absolute_extension() { + let f = TestFixture::new(); + // Don't append EXE_EXTENSION here. + let b = f.bins[4].parent().unwrap().join(&BIN_NAME); + assert_eq!(_which(&f, &b).unwrap(), f.bins[4].canonicalize().unwrap()); +} + +#[test] +#[cfg(unix)] +fn test_which_relative() { + let f = TestFixture::new(); + assert_eq!( + _which(&f, "b/bin").unwrap(), + f.bins[3].canonicalize().unwrap() + ); +} + +#[test] +#[cfg(windows)] +fn test_which_relative() { + let f = TestFixture::new(); + assert_eq!( + _which(&f, "b/bin").unwrap(), + f.bins[4].canonicalize().unwrap() + ); +} + +#[test] +#[cfg(unix)] +fn test_which_relative_extension() { + // test_which_relative tests a relative path without an extension, + // so test a relative path with an extension here. + let f = TestFixture::new(); + let b = Path::new("b/bin").with_extension(env::consts::EXE_EXTENSION); + assert_eq!(_which(&f, &b).unwrap(), f.bins[3].canonicalize().unwrap()); +} + +#[test] +#[cfg(windows)] +fn test_which_relative_extension() { + // test_which_relative tests a relative path without an extension, + // so test a relative path with an extension here. + let f = TestFixture::new(); + let b = Path::new("b/bin").with_extension("cmd"); + assert_eq!(_which(&f, &b).unwrap(), f.bins[5].canonicalize().unwrap()); +} + +#[test] +#[cfg(windows)] +fn test_which_relative_extension_case() { + // Test that a relative path with an uppercase extension + // is accepted. + let f = TestFixture::new(); + let b = Path::new("b/bin").with_extension("EXE"); + assert_eq!(_which(&f, &b).unwrap(), f.bins[4].canonicalize().unwrap()); +} + +#[test] +#[cfg(unix)] +fn test_which_relative_leading_dot() { + let f = TestFixture::new(); + assert_eq!( + _which(&f, "./b/bin").unwrap(), + f.bins[3].canonicalize().unwrap() + ); +} + +#[test] +#[cfg(windows)] +fn test_which_relative_leading_dot() { + let f = TestFixture::new(); + assert_eq!( + _which(&f, "./b/bin").unwrap(), + f.bins[4].canonicalize().unwrap() + ); +} + +#[test] +#[cfg(unix)] +fn test_which_non_executable() { + // Shouldn't return non-executable files. + let f = TestFixture::new(); + f.touch("b/another", "").unwrap(); + assert!(_which(&f, "another").is_err()); +} + +#[test] +#[cfg(unix)] +fn test_which_absolute_non_executable() { + // Shouldn't return non-executable files, even if given an absolute path. + let f = TestFixture::new(); + let b = f.touch("b/another", "").unwrap(); + assert!(_which(&f, &b).is_err()); +} + +#[test] +#[cfg(unix)] +fn test_which_relative_non_executable() { + // Shouldn't return non-executable files. + let f = TestFixture::new(); + f.touch("b/another", "").unwrap(); + assert!(_which(&f, "b/another").is_err()); +} + +#[test] +#[cfg(feature = "failure")] +fn test_failure() { + let f = TestFixture::new(); + + let run = || -> std::result::Result { + // Test the conversion to failure + let p = _which(&f, "./b/bin")?; + Ok(p.into_path_buf()) + }; + + let _ = run(); +}