about summary refs log tree commit diff
path: root/src/libcore
AgeCommit message (Collapse)AuthorLines
2019-10-23Add tracking issue for the `matches!` macroSimon Sapin-1/+1
https://github.com/rust-lang/rust/issues/65721
2019-10-23Move the `matches!` macro to the preludeSimon Sapin-15/+26
2019-10-23Add `core::macros::matches!( $expr, $pat ) -> bool`Simon Sapin-2/+15
# Motivation This macro is: * General-purpose (not domain-specific) * Simple (the implementation is short) * Very popular [on crates.io](https://crates.io/crates/matches) (currently 37th in all-time downloads) * The two previous points combined make it number one in [left-pad index](https://twitter.com/bascule/status/1184523027888988160) score As such, I feel it is a good candidate for inclusion in the standard library. In fact I already felt that way five years ago: https://github.com/rust-lang/rust/pull/14685 (Although the proof of popularity was not as strong at the time.) Back then, the main concern was that this macro may not be quite universally-enough useful to belong in the prelude. # API Therefore, this PR adds the macro such that using it requires one of: ``` use core::macros::matches; use std::macros::matches; ``` Like arms of a `match` expression, the macro supports multiple patterns separated by `|` and optionally followed by `if` and a guard expression: ``` let foo = 'f'; assert!(matches!(foo, 'A'..='Z' | 'a'..='z')); let bar = Some(4); assert!(matches!(bar, Some(x) if x > 2)); ``` # Implementation constraints A combination of reasons make it tricky for a standard library macro not to be in the prelude. Currently, all public `macro_rules` macros in the standard library macros end up “in the prelude” of every crate not through `use std::prelude::v1::*;` like for other kinds of items, but through `#[macro_use]` on `extern crate std;`. (Both are injected by `src/libsyntax_ext/standard_library_imports.rs`.) `#[macro_use]` seems to import every macro that is available at the top-level of a crate, even if through a `pub use` re-export. Therefore, for `matches!` not to be in the prelude, we need it to be inside of a module rather than at the root of `core` or `std`. However, the only way to make a `macro_rules` macro public outside of the crate where it is defined appears to be `#[macro_export]`. This exports the macro at the root of the crate regardless of which module defines it. See [macro scoping]( https://doc.rust-lang.org/reference/macros-by-example.html#scoping-exporting-and-importing) in the reference. Therefore, the macro needs to be defined in a crate that is not `core` or `std`. # Implementation This PR adds a new `matches_macro` crate as a private implementation detail of the standard library. This crate is `#![no_core]` so that libcore can depend on it. It contains a `macro_rules` definition with `#[macro_export]`. libcore and libstd each have a new public `macros` module that contains a `pub use` re-export of the macro. Both the module and the macro are unstable, for now. The existing private `macros` modules are renamed `prelude_macros`, though their respective source remains in `macros.rs` files.
2019-10-22Apply clippy::single_match suggestionMateusz Mikuła-3/+2
2019-10-22Apply clippy::redundant_pattern_matching suggestionMateusz Mikuła-1/+1
2019-10-22Apply clippy::useless_let_if_seq suggestionMateusz Mikuła-5/+4
2019-10-21Rollup merge of #65092 - tspiteri:const-is-pow2, r=oli-obkMazdak Farrokhzad-2/+2
make is_power_of_two a const function This makes `is_power_of_two` a const function by using `&` instead of short-circuiting `&&`; Rust supports bitwise `&` for `bool` and short-circuiting is not required in the existing expression. I don't think this needs a const-hack label as I don't find the changed code less readable, if anything I prefer that it is clearer that short circuiting is not used. @oli-obk
2019-10-21improve readability of is_power_of_twoTrevor Spiteri-1/+1
2019-10-21Rollup merge of #65638 - dsincl12:master, r=CentrilMazdak Farrokhzad-2/+2
Rename the default argument 'def' to 'default' Fixes: #65492
2019-10-21Rollup merge of #65633 - Rantanen:doc-example-paths, r=CentrilMazdak Farrokhzad-4/+4
Remove leading :: from paths in doc examples Noted some pre-2018 path syntax in the doc examples, for example: https://doc.rust-lang.org/std/process/fn.exit.html ```rust fn main() { ::std::process::exit(match run_app() { Ok(_) => 0, ... ``` Couldn't find an existing issue on this (then again, "::" makes for an annoying thing to search for) so if there is already something fixing this and/or there's a reason to not fix it, just close this PR. (Also fixed indentation in the `process::exit()` docs)
2019-10-20Rename the default argument 'def' to 'default'David Sinclair-2/+2
Fixes: #65492
2019-10-20Remove leading :: from paths in doc examplesMikko Rantanen-4/+4
2019-10-20Rollup merge of #65600 - integer32llc:bye-bye-ref, r=CentrilMazdak Farrokhzad-2/+2
Remove unneeded `ref` from docs Will reduce confusion like in https://users.rust-lang.org/t/help-understanding-the-ref-t-syntax/33779 since match ergonomics means you (almost) never have to say `ref` anymore! There might be more like this, but I don't have a checkout on my computer right this second and I'm on slow wifi and GitHub search isn't powerful enough and that's my story.
2019-10-20Rollup merge of #64996 - lzutao:inline-ptr-null, r=oli-obkMazdak Farrokhzad-2/+2
Inline `ptr::null(_mut)` even in debug builds I think we should treat `ptr::null(_mut)` as a constant. As It may help reduce code size in debug build. See godbolt link: https://godbolt.org/z/b9YMtD
2019-10-19Remove unneeded `ref` from docsCarol (Nichols || Goulding)-2/+2
Will reduce confusion like in https://users.rust-lang.org/t/help-understanding-the-ref-t-syntax/33779 since match ergonomics means you (almost) never have to say `ref` anymore!
2019-10-19Stabilize `Option::flatten`Ethan Brierley-3/+1
2019-10-18Rollup merge of #65549 - t-rapp:tr-wrapping-rotate-docs, r=jonas-schievinkTyler Mandry-2/+2
Fix left/right shift typo in wrapping rotate docs This makes the note similar to the one found on rotate functions for primitive types like i32/u32.
2019-10-18Rollup merge of #65496 - tspiteri:euc-div-panic, r=KodrAusTyler Mandry-2/+10
properly document panics in div_euclid and rem_euclid For signed numbers, document that `div_euclid` and `rem_euclid` panic not just when `rhs` is 0, but also when the division overflows. For unsigned numbers, document that `div_euclid` and `rem_euclid` panic when `rhs` is 0.
2019-10-18Rollup merge of #65016 - lzutao:inline-mem-constfn, r=oli-obkTyler Mandry-2/+2
Always inline `mem::{size_of,align_of}` in debug builds Those two are const fn and do not have any arguments. Inlining helps reducing generated code size in debug builds. See also #64996.
2019-10-18Fix left/right shift typo in wrapping rotate docsTobias Rapp-2/+2
This makes the note similar to the one found on rotate functions for primitive types like i32/u32.
2019-10-17properly document panics in div_euclid and rem_euclidTrevor Spiteri-2/+10
2019-10-17Rollup merge of #65478 - RalfJung:write, r=jonas-schievinkMazdak Farrokhzad-2/+2
fmt::Write is about string slices, not byte slices No idea why the docs talk about bytes, maybe a copy-paste error?
2019-10-17Rollup merge of #65475 - lzutao:eg_type_name, r=CentrilMazdak Farrokhzad-0/+9
add example for type_name So users of this function could at least expect what its output for current compiler version.
2019-10-17Rollup merge of #65237 - KodrAus:fix/map-entry-err, r=sfacklerMazdak Farrokhzad-7/+49
Move debug_map assertions after check for err Fixes #65231 We have some assertions in `DebugMap` to catch broken implementations of `Debug` that produce malformed entries. These checks don't make sense if formatting fails partway through. This PR moves those assertions to within the `and_then` closures along with the other formatting logic, so they're only checked if the map hasn't failed to format an entry already.
2019-10-16Upgrade Emscripten targets to use upstream LLVM backendThomas Lively-54/+18
- Compatible with Emscripten 1.38.46-upstream or later upstream. - Refactors the Emscripten target spec to share code with other wasm targets. - Replaces the old incorrect wasm32 C call ABI with the correct one, preserving the old one as wasm32_bindgen_compat for wasm-bindgen compatibility. - Updates the varargs ABI used by Emscripten and deletes the old one. - Removes the obsolete wasm32-experimental-emscripten target. - Uses EMCC_CFLAGS on CI to avoid the timeout problems with #63649.
2019-10-16fmt::Write is about string slices, not byte slicesRalf Jung-2/+2
2019-10-16add example for type_nameLzu Tao-0/+9
2019-10-13Rollup merge of #65370 - Cerberuser:patch-1, r=jonas-schievinkMazdak Farrokhzad-5/+5
Add `dyn` to `Any` documentation I noticed that in documentation to `Any` trait the old trait object syntax is used, which could be confusing for newcomers, since we generally recommend using `dyn Trait` instead of just `Trait`. This PR changes the documentation comment, so that it uses `&dyn Any`, `&mut dyn Any` and `Box<dyn Any>`, correspondingly.
2019-10-13Rollup merge of #65336 - BO41:typo, r=petrochenkovMazdak Farrokhzad-1/+1
Fix typo in task::Waker fixes #65323 in `libstd/error.rs` there are a few mentions of `trait@Send` and `trait@Sync`. Are they wrong as well?
2019-10-13Rollup merge of #65312 - tspiteri:signed-sat-mul, r=dtolnayMazdak Farrokhzad-1/+1
improve performance of signed saturating_mul Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6 Fixes #65309.
2019-10-13Rollup merge of #65214 - Amanieu:cfg_atomic, r=alexcrichtonMazdak Farrokhzad-70/+92
Split non-CAS atomic support off into target_has_atomic_load_store This PR implements my proposed changes in https://github.com/rust-lang/rust/issues/32976#issuecomment-518542029 by removing `target_has_atomic = "cas"` and splitting `target_has_atomic` into two separate `cfg`s: * `target_has_atomic = 8/16/32/64/128`: This indicates the largest width that the target can atomically CAS (which implies support for all atomic operations). * ` target_has_atomic_load_store = 8/16/32/64/128`: This indicates the largest width that the target can support loading or storing atomically (but may not support CAS). cc #32976 r? @alexcrichton
2019-10-13Added code elementCerberuser-1/+1
Co-Authored-By: Jonas Schievink <jonasschievink@gmail.com>
2019-10-13Add `dyn` to `Any` documentationCerberuser-5/+5
I noticed that in documentation to `Any` trait the old trait object syntax is used, which could be confusing for newcomers, since we generally recommend using `dyn Trait` instead of just `Trait`. This PR changes the documentation comment, so that it uses `&dyn Any`, `&mut dyn Any` and `Box<dyn Any>`, correspondingly.
2019-10-13Rollup merge of #65339 - RalfJung:atomic-ordering, r=CentrilMazdak Farrokhzad-25/+27
do not reference LLVM for our concurrency memory model Fixes https://github.com/rust-lang/rust/issues/65282
2019-10-13Rollup merge of #65165 - BO41:char_docs, r=varkorMazdak Farrokhzad-63/+109
Improve docs on some char boolean methods simple revival of #61794 (also rustfmt on rest of file :) Documentation for `is_xid_start()` and `is_xid_continue()` couldn't be improved since both methods got remove from this repository r? @dtolnay cc @JohnCSimon
2019-10-12fix link targetsRalf Jung-12/+12
2019-10-12it's C++20Ralf Jung-7/+7
2019-10-12do not reference LLVM for our concurrency memory modelRalf Jung-25/+27
2019-10-12Fix typoBO41-1/+1
2019-10-12Improve docs on some char boolean methodsBO41-63/+109
2019-10-11improve performance of signed saturating_mulTrevor Spiteri-1/+1
Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6
2019-10-11Make <*const/mut T>::offset_from `const fn`Oliver Scherer-1/+21
2019-10-10move debug_map assertions after check for errAshley Mannix-7/+49
2019-10-08Stabilize mem::take (mem_take)Jon Gjengset-6/+1
Tracking issue: https://github.com/rust-lang/rust/issues/61129
2019-10-08Split non-CAS atomic support off into target_has_atomic_load_storeAmanieu d'Antras-70/+92
2019-10-08Rollup merge of #65046 - sinkuu:cell_reorder, r=shepmasterMazdak Farrokhzad-46/+46
Make `Cell::new` method come first in documentation Methods to create a thing usually comes first in `std` documentation, and `Cell` has been an exception. Also, `T: Copy` specialized methods should not be on top of the page. (This had led me to miss that most of its methods are not bounded by `Copy`...)
2019-10-08Rollup merge of #64726 - andrewbanchich:unimplemented, r=rkruppeMazdak Farrokhzad-15/+32
rewrite documentation for unimplemented! to clarify use The current docs for `unimplemented!` seem to miss the point of this macro. > This can be useful if you are prototyping and are just looking to have your code type-check, or if you're implementing a trait that requires multiple methods, and you're only planning on using one of them. You could also return a `()` if you just want your code to type-check. I think `unimplemented!` is useful for when you want your program to exit when it reaches an unimplemented area. I rewrote the explanation and gave examples of both forms of this macro that I think clarify its use a little better.
2019-10-06rewrite documentation for unimplemented!Andrew Banchich-15/+32
2019-10-05Rollup merge of #65151 - tmandry:revert-emscripten-upgrade, r=tmandryTyler Mandry-18/+54
Revert #63649 - "Upgrade Emscripten targets to use upstream LLVM backend" This change caused the runtime of the linux-asmjs builder to nearly double from 2+ hours to about 4 hours, which happens to be the bors timeout. (It made it in barely under 4 hours when it was merged.) This is causing timeouts on all new changes. This reverts commit 7870050796e5904a0fc85ecbe6fa6dde1cfe0c91, reversing changes made to 2e7244807a7878f6eca3eb7d97ae9b413aa49014.
2019-10-05Rollup merge of #65106 - Mark-Simulacrum:unused-attr-allow, r=CentrilTyler Mandry-0/+2
Allow unused attributes to avoid incremental bug cc #65023 This isn't labeled as fixing that issue because it's not really a fix, just a patch.