Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.83.0
rustc 1.84.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC assertions)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC assertions)
x86-64 GCCRS 14.2 (GCC)
Options
Source code
#![feature(try_trait)] #![feature(never_type)] #![feature(unchecked_math)] #![feature(step_trait_ext)] #![feature(step_trait)] use std::{char, convert::TryFrom, iter::Step, mem, ops::Try}; #[allow(non_camel_case_types)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] struct ch(char); unsafe impl Step for ch { fn steps_between(&ch(start): &Self, &ch(end): &Self) -> Option<usize> { let start = start as u32; let end = end as u32; if start <= end { let count = end - start + 1; if start < 0xD800 && 0xE000 <= end { usize::try_from(count - 0x800).ok() } else { usize::try_from(count).ok() } } else { None } } fn forward_checked(ch(start): Self, count: usize) -> std::option::Option<Self> { let start = start as u32; let mut res = Step::forward_checked(start, count)?; if start < 0xD800 && 0xD800 <= res { res = Step::forward_checked(res, 0x800)?; } if res <= char::MAX as u32 { Some(unsafe { char::from_u32_unchecked(res) }).map(ch) } else { None } } unsafe fn forward_unchecked(ch(start): Self, count: usize) -> Self { let start = start as u32; let mut res = Step::forward_unchecked(start, count); if start < 0xD800 && 0xD800 <= res { res = Step::forward_unchecked(res, 0x800); } ch(char::from_u32_unchecked(res)) } fn backward_checked(ch(start): Self, count: usize) -> std::option::Option<Self> { let start = start as u32; let mut res = Step::backward_checked(start, count)?; if start >= 0xE000 && 0xE000 > res { res = Step::backward_checked(res, 0x800)?; } Some(unsafe { char::from_u32_unchecked(res) }).map(ch) } unsafe fn backward_unchecked(ch(start): Self, count: usize) -> Self { let start = start as u32; let mut res = Step::backward_unchecked(start, count); if start >= 0xE000 && 0xE000 > res { res = Step::backward_unchecked(res, 0x800); } ch(char::from_u32_unchecked(res)) } } extern "C" { fn f(_: u32); } pub fn external_iter_all() { for ch(c) in RangeInclusive::new(ch('\0'), ch(char::MAX)) { unsafe { f(c as u32) } } } // Inline version of RangeInclusive below, so we can patch // RangeInclusive::next to use Step::forward_unchecked #[derive(Clone, PartialEq, Eq, Hash)] struct RangeInclusive<Idx> { pub(crate) start: Idx, pub(crate) end: Idx, // This field is: // - `false` upon construction // - `false` when iteration has yielded an element and the iterator is not exhausted // - `true` when iteration has been used to exhaust the iterator // // This is required to support PartialEq and Hash without a PartialOrd bound or specialization. pub(crate) exhausted: bool, } impl<Idx> RangeInclusive<Idx> { pub const fn new(start: Idx, end: Idx) -> Self { Self { start, end, exhausted: false, } } } impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> { pub fn is_empty(&self) -> bool { self.exhausted || !(self.start <= self.end) } } impl<A: Step> Iterator for RangeInclusive<A> { type Item = A; #[inline] fn next(&mut self) -> Option<A> { if self.is_empty() { return None; } let is_iterating = self.start < self.end; Some(if is_iterating { let n = unsafe { Step::forward_unchecked(self.start.clone(), 1) }; mem::replace(&mut self.start, n) } else { self.exhausted = true; self.start.clone() }) } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { if self.is_empty() { return (0, Some(0)); } match Step::steps_between(&self.start, &self.end) { Some(hint) => (hint.saturating_add(1), hint.checked_add(1)), None => (usize::MAX, None), } } #[inline] fn nth(&mut self, n: usize) -> Option<A> { if self.is_empty() { return None; } if let Some(plus_n) = Step::forward_checked(self.start.clone(), n) { use std::cmp::Ordering::*; match plus_n.partial_cmp(&self.end) { Some(Less) => { self.start = Step::forward(plus_n.clone(), 1); return Some(plus_n); } Some(Equal) => { self.start = plus_n.clone(); self.exhausted = true; return Some(plus_n); } _ => {} } } self.start = self.end.clone(); self.exhausted = true; None } #[inline] fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok = B>, { if self.is_empty() { return Try::from_ok(init); } let mut accum = init; while self.start < self.end { let n = Step::forward(self.start.clone(), 1); let n = mem::replace(&mut self.start, n); accum = f(accum, n)?; } self.exhausted = true; if self.start == self.end { accum = f(accum, self.start.clone())?; } Try::from_ok(accum) } #[inline] fn fold<B, F>(mut self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B, { #[inline] fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> { move |acc, x| Ok(f(acc, x)) } self.try_fold(init, ok(f)).unwrap() } #[inline] fn last(mut self) -> Option<A> { self.next_back() } #[inline] fn min(mut self) -> Option<A> { self.next() } #[inline] fn max(mut self) -> Option<A> { self.next_back() } } impl<A: Step> DoubleEndedIterator for RangeInclusive<A> { #[inline] fn next_back(&mut self) -> Option<A> { if self.is_empty() { return None; } let is_iterating = self.start < self.end; Some(if is_iterating { let n = Step::backward(self.end.clone(), 1); mem::replace(&mut self.end, n) } else { self.exhausted = true; self.end.clone() }) } #[inline] fn nth_back(&mut self, n: usize) -> Option<A> { if self.is_empty() { return None; } if let Some(minus_n) = Step::backward_checked(self.end.clone(), n) { use std::cmp::Ordering::*; match minus_n.partial_cmp(&self.start) { Some(Greater) => { self.end = Step::backward(minus_n.clone(), 1); return Some(minus_n); } Some(Equal) => { self.end = minus_n.clone(); self.exhausted = true; return Some(minus_n); } _ => {} } } self.end = self.start.clone(); self.exhausted = true; None } #[inline] fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok = B>, { if self.is_empty() { return Try::from_ok(init); } let mut accum = init; while self.start < self.end { let n = Step::backward(self.end.clone(), 1); let n = mem::replace(&mut self.end, n); accum = f(accum, n)?; } self.exhausted = true; if self.start == self.end { accum = f(accum, self.start.clone())?; } Try::from_ok(accum) } #[inline] fn rfold<B, F>(mut self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B, { #[inline] fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> { move |acc, x| Ok(f(acc, x)) } self.try_rfold(init, ok(f)).unwrap() } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree