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

array::try_map #79713

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
55 changes: 55 additions & 0 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,61 @@ impl<T, const N: usize> [T; N] {
unsafe { crate::mem::transmute_copy::<_, [U; N]>(&dst) }
}

/// A fallible function `f` applied to each element on array `self` in order to
/// return an array the same size as `self` or the first error encountered.
///
/// # Examples
///
/// ```
/// #![feature(array_try_map)]
/// #![feature(array_map)]
/// let a = ["1", "2", "3"];
/// let b = a.try_map(|v| v.parse::<u32>()).unwrap().map(|v| v + 1);
/// assert_eq!(b, [2, 3, 4]);
///
/// let a = ["1", "2a", "3"];
/// let b = a.try_map(|v| v.parse::<u32>());
/// assert!(b.is_err());
/// ```
#[unstable(feature = "array_try_map", issue = "79711")]
pub fn try_map<F, E, U>(self, mut f: F) -> Result<[U; N], E>
where
F: FnMut(T) -> Result<U, E>,
{
use crate::mem::MaybeUninit;
struct Guard<T, const N: usize> {
dst: *mut T,
initialized: usize,
}

impl<T, const N: usize> Drop for Guard<T, N> {
fn drop(&mut self) {
debug_assert!(self.initialized <= N);

let initialized_part =
crate::ptr::slice_from_raw_parts_mut(self.dst, self.initialized);
// SAFETY: this raw slice will contain only initialized objects
// that's why, it is allowed to drop it.
unsafe {
crate::ptr::drop_in_place(initialized_part);
}
}
}
let mut dst = MaybeUninit::uninit_array::<N>();
let mut guard: Guard<U, N> =
Guard { dst: MaybeUninit::slice_as_mut_ptr(&mut dst), initialized: 0 };
for (src, dst) in IntoIter::new(self).zip(&mut dst) {
dst.write(f(src)?);
guard.initialized += 1;
}
// FIXME: Convert to crate::mem::transmute once it works with generics.
// unsafe { crate::mem::transmute::<[MaybeUninit<U>; N], [U; N]>(dst) }
crate::mem::forget(guard);
// SAFETY: At this point we've properly initialized the whole array
// and we just need to cast it to the correct type.
unsafe { Ok(crate::mem::transmute_copy::<_, [U; N]>(&dst)) }
}

/// 'Zips up' two arrays into a single array of pairs.
///
/// `zip()` returns a new array where every element is a tuple where the
Expand Down
2 changes: 1 addition & 1 deletion src/doc/book
Submodule book updated 217 files
2 changes: 1 addition & 1 deletion src/doc/nomicon
2 changes: 1 addition & 1 deletion src/doc/reference
2 changes: 1 addition & 1 deletion src/tools/cargo
Submodule cargo updated 74 files
+7 −14 .github/workflows/main.yml
+3 −3 Cargo.toml
+0 −4 crates/cargo-test-support/src/lib.rs
+4 −17 crates/cargo-test-support/src/paths.rs
+6 −17 crates/crates-io/lib.rs
+0 −8 crates/credential/README.md
+0 −13 crates/credential/cargo-credential-1password/Cargo.toml
+0 −323 crates/credential/cargo-credential-1password/src/main.rs
+0 −14 crates/credential/cargo-credential-gnome-secret/Cargo.toml
+0 −3 crates/credential/cargo-credential-gnome-secret/build.rs
+0 −210 crates/credential/cargo-credential-gnome-secret/src/main.rs
+0 −12 crates/credential/cargo-credential-macos-keychain/Cargo.toml
+0 −50 crates/credential/cargo-credential-macos-keychain/src/main.rs
+0 −12 crates/credential/cargo-credential-wincred/Cargo.toml
+0 −99 crates/credential/cargo-credential-wincred/src/main.rs
+0 −10 crates/credential/cargo-credential/Cargo.toml
+0 −41 crates/credential/cargo-credential/README.md
+0 −86 crates/credential/cargo-credential/src/lib.rs
+0 −42 src/bin/cargo/commands/logout.rs
+0 −3 src/bin/cargo/commands/mod.rs
+1 −1 src/cargo/core/compiler/fingerprint.rs
+42 −23 src/cargo/core/compiler/standard_lib.rs
+2 −18 src/cargo/core/compiler/unit_dependencies.rs
+0 −2 src/cargo/core/features.rs
+0 −4 src/cargo/core/package.rs
+14 −2 src/cargo/core/resolver/types.rs
+3 −3 src/cargo/core/summary.rs
+2 −5 src/cargo/ops/cargo_compile.rs
+4 −6 src/cargo/ops/cargo_doc.rs
+30 −39 src/cargo/ops/cargo_new.rs
+9 −14 src/cargo/ops/cargo_output_metadata.rs
+2 −2 src/cargo/ops/mod.rs
+57 −127 src/cargo/ops/registry.rs
+0 −236 src/cargo/ops/registry/auth.rs
+0 −7 src/cargo/ops/tree/graph.rs
+8 −7 src/cargo/ops/tree/mod.rs
+3 −25 src/cargo/ops/vendor.rs
+30 −53 src/cargo/util/config/mod.rs
+0 −81 src/cargo/util/paths.rs
+4 −2 src/cargo/util/toml/mod.rs
+7 −8 src/doc/src/faq.md
+10 −7 src/doc/src/reference/build-scripts.md
+11 −21 src/doc/src/reference/config.md
+0 −8 src/doc/src/reference/environment-variables.md
+9 −8 src/doc/src/reference/manifest.md
+3 −3 src/doc/src/reference/semver.md
+0 −176 src/doc/src/reference/unstable.md
+4 −5 tests/testsuite/bad_config.rs
+6 −3 tests/testsuite/build.rs
+2 −126 tests/testsuite/build_script.rs
+4 −2 tests/testsuite/cache_messages.rs
+12 −5 tests/testsuite/check.rs
+0 −488 tests/testsuite/credential_process.rs
+19 −13 tests/testsuite/features.rs
+11 −1 tests/testsuite/features2.rs
+4 −2 tests/testsuite/fix.rs
+0 −82 tests/testsuite/logout.rs
+0 −2 tests/testsuite/main.rs
+0 −50 tests/testsuite/metadata.rs
+1 −0 tests/testsuite/mock-std/library/test/Cargo.toml
+1 −0 tests/testsuite/mock-std/library/test/src/lib.rs
+9 −0 tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml
+2 −0 tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs
+12 −0 tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml
+9 −0 tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs
+11 −0 tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml
+8 −0 tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs
+11 −0 tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml
+8 −0 tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs
+1 −0 tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml
+1 −0 tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml
+1 −0 tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml
+4 −32 tests/testsuite/new.rs
+88 −73 tests/testsuite/standard_lib.rs
2 changes: 1 addition & 1 deletion src/tools/rustfmt
Submodule rustfmt updated 55 files
+1 −56 CHANGELOG.md
+92 −112 Cargo.lock
+10 −10 Cargo.toml
+0 −50 Configurations.md
+94 −94 src/attr.rs
+10 −13 src/closures.rs
+0 −3 src/config/mod.rs
+0 −12 src/config/options.rs
+18 −28 src/expr.rs
+43 −114 src/items.rs
+2 −2 src/lib.rs
+4 −2 src/macros.rs
+1 −1 src/modules/visitor.rs
+22 −83 src/reorder.rs
+1 −1 src/skip.rs
+1 −9 src/syntux/parser.rs
+73 −192 src/types.rs
+1 −2 src/utils.rs
+2 −6 src/visitor.rs
+0 −17 tests/source/configs/group_imports/StdExternalCrate-merge_imports.rs
+0 −6 tests/source/configs/group_imports/StdExternalCrate-nested.rs
+0 −17 tests/source/configs/group_imports/StdExternalCrate-no_reorder.rs
+0 −15 tests/source/configs/group_imports/StdExternalCrate.rs
+0 −10 tests/source/expr.rs
+0 −14 tests/source/extern.rs
+0 −11 tests/source/issue-2781.rs
+0 −21 tests/source/issue-4243.rs
+0 −16 tests/source/issue-4244.rs
+0 −26 tests/source/issue-4245.rs
+0 −20 tests/source/issue-4577.rs
+0 −19 tests/source/issue_4584.rs
+0 −7 tests/source/negative-impl.rs
+0 −11 tests/source/structs.rs
+0 −6 tests/source/type.rs
+0 −16 tests/target/configs/group_imports/StdExternalCrate-merge_imports.rs
+0 −7 tests/target/configs/group_imports/StdExternalCrate-nested.rs
+0 −15 tests/target/configs/group_imports/StdExternalCrate-no_reorder.rs
+0 −13 tests/target/configs/group_imports/StdExternalCrate.rs
+0 −22 tests/target/configs/where_single_line/true-with-brace-style.rs
+0 −10 tests/target/expr.rs
+0 −6 tests/target/extern.rs
+0 −11 tests/target/issue-2781.rs
+0 −7 tests/target/issue-4029.rs
+0 −8 tests/target/issue-4115.rs
+0 −18 tests/target/issue-4152.rs
+0 −28 tests/target/issue-4243.rs
+0 −20 tests/target/issue-4244.rs
+0 −34 tests/target/issue-4245.rs
+0 −5 tests/target/issue-4313.rs
+0 −15 tests/target/issue-4577.rs
+0 −5 tests/target/issue_4545.rs
+0 −32 tests/target/issue_4584.rs
+0 −14 tests/target/negative-impl.rs
+0 −14 tests/target/structs.rs
+0 −4 tests/target/type.rs