Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Inline copied and next methods #108048

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/core/src/iter/adapters/copied.rs
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ where
{
type Item = T;

#[inline]
fn next(&mut self) -> Option<T> {
self.it.next().copied()
}
1 change: 1 addition & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
@@ -1794,6 +1794,7 @@ impl<T> Option<&T> {
/// let copied = opt_x.copied();
/// assert_eq!(copied, Some(12));
/// ```
#[inline]
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "copied", since = "1.35.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
13 changes: 13 additions & 0 deletions tests/codegen/issue-107681-unwrap_unchecked-optimized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// compile-flags: -C opt-level=3

#![crate_type = "lib"]

extern crate core;
use core::{iter::Copied, slice::Iter};

// Make sure that the use of unwrap_unchecked is optimized accordingly.
// i.e., there are no branch jumps.
pub unsafe fn unwrap_unchecked_optimized(x: &mut Copied<Iter<'_, u32>>) -> u32 {
// CHECK-NOT: br
x.next().unwrap_unchecked()
}