Skip to content

Conversation

GrigorenkoPV
Copy link
Contributor

@GrigorenkoPV GrigorenkoPV commented Nov 20, 2023

Tracking issue: #143801

A more generalized version of filter_map, which allows to return some data on failure.

Safety

As far as I can tell, E cannot contain any 'b data, so it is impossible to duplicate the &'b [mut] reference into the RefCell's data.

Other than this E, everything is analogous to the already stable filter_map.

Try / Residual

I have considered generalizing this to use the Try & Residual just like #79711 does for array::try_map, but it does not seem to be possible: we want to essentially .map_err(|e| (orig, e)) but this does not seem to be supported with Try. (Plus I am not even sure if it is possible to express the fact that &U in Try::Output would have to have the same lifetime as the &T input of F.)

ACP

As far as I can tell, this is not mandatory, and the implementation is small enough to probably be smaller than the doc I would have to write.

rust-lang/libs-team#341

rust-lang/libs-team#586

@rustbot
Copy link
Collaborator

rustbot commented Nov 20, 2023

r? @Mark-Simulacrum

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 20, 2023
@Mark-Simulacrum Mark-Simulacrum added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 9, 2023
@Mark-Simulacrum
Copy link
Member

ACPs are the recommended way to get the libs-api team to approve new unstable APIs. I've marked this as waiting-on-team which I believe also is in their queue.

(libs-api -- we may want to update the guidance on the linked docs to just require an ACP)

@m-ou-se
Copy link
Member

m-ou-se commented Dec 19, 2023

As far as I can tell, this is not mandatory, and the implementation is small enough to probably be smaller than the doc I would have to write.

The motivation is an important part of the ACP. We do need to understand the motivation, ideally with clear use cases, before accepting new (unstable) APIs.

@Amanieu Amanieu added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Dec 19, 2023
@Dylan-DPC Dylan-DPC added the needs-acp This change is blocked on the author creating an ACP. label Feb 17, 2024
@Dylan-DPC
Copy link
Member

@GrigorenkoPV any updates on creating the ACP? if you have done so, kindly add it to the pr description. Thanks

@GrigorenkoPV
Copy link
Contributor Author

The ACP: rust-lang/libs-team#341

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 21, 2024
@Dylan-DPC Dylan-DPC added S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. and removed needs-acp This change is blocked on the author creating an ACP. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 22, 2024
@stegaBOB
Copy link

Thanks for your work on this @GrigorenkoPV! We would love for this method to be added. @Mark-Simulacrum is there anything else needed here to get it across the finish line? I'd be happy to put some time into this if I can help in any way.

@Dylan-DPC
Copy link
Member

@stegaBOB this is waiting on the ACP getting merged, so we need to wait fro that and nothing is expected from author or reviewer about it.

@bors
Copy link
Collaborator

bors commented Dec 9, 2024

☔ The latest upstream changes (presumably #134052) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Dec 9, 2024
@GrigorenkoPV
Copy link
Contributor Author

#133567 changed a place in rustc_interface where this method could be used and now it no longer fits, oh well.

@Dylan-DPC Dylan-DPC removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 30, 2025
@GrigorenkoPV
Copy link
Contributor Author

rust-lang/libs-team#341 has gone completely unnoticed for a year and a half now, but a slightly more general version of the same API got proposed in rust-lang/libs-team#586 and accepted.

Let me adjust the PR accordingly.

@rustbot author
@rustbot label -S-waiting-on-ACP

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 11, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot removed the S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. label Jul 11, 2025
@GrigorenkoPV
Copy link
Contributor Author

@rustbot ready

I've went with the design that does not use Try for two reasons:

  • Try probably won't be stabilized any time soon
  • It seems to be impossible to return the original Ref{,Mut} on failure when using Try

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 11, 2025
@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 14, 2025

📌 Commit f6bdffd has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 14, 2025
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 14, 2025
…rk-Simulacrum

Add Ref/RefMut try_map method

Tracking issue:  rust-lang#143801

A more generalized version of [`filter_map`](https://doc.rust-lang.org/stable/core/cell/struct.Ref.html#method.filter_map), which allows to return some data on failure.

## Safety

As far as I can tell, `E` cannot contain any `'b` data, so it is impossible to duplicate the `&'b [mut]` reference into the `RefCell`'s  data.

Other than this `E`, everything is analogous to the already stable `filter_map`.

## `Try` / `Residual`

I have considered generalizing this to use the `Try` & `Residual` just like rust-lang#79711 does for `array::try_map`, but it does not seem to be possible: we want to essentially `.map_err(|e| (orig, e))` but this does not seem to be supported with `Try`. (Plus I am not even sure if it is possible to express the fact that `&U` in `Try::Output` would have to have the same lifetime as the `&T` input of `F`.)

## ACP

~As far as I can tell, this [is not mandatory](https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html#suitability-for-the-standard-library), and the implementation is small enough to probably be smaller than the doc I would have to write.~

~rust-lang/libs-team#341

rust-lang/libs-team#586
bors added a commit that referenced this pull request Aug 14, 2025
Rollup of 9 pull requests

Successful merges:

 - #118087 (Add Ref/RefMut try_map method)
 - #140794 (Add information about group a lint belongs to)
 - #144947 (Fix description of unsigned `checked_exact_div`)
 - #145005 (strip prefix of temporary file names when it exceeds filesystem name length limit)
 - #145233 (cfg_select: Support unbraced expressions)
 - #145243 (take attr style into account in diagnostics)
 - #145353 (bootstrap: Fix jemalloc 64K page support for aarch64 tools)
 - #145379 (bootstrap: Support passing `--timings` to cargo)
 - #145389 ([rustdoc] Revert "rustdoc search: prefer stable items in search results")

Failed merges:

 - #144983 (Rehome 37 `tests/ui/issues/` tests to other subdirectories under `tests/ui/`)
 - #145065 (resolve: Introduce `RibKind::Block`)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 15, 2025
…rk-Simulacrum

Add Ref/RefMut try_map method

Tracking issue:  rust-lang#143801

A more generalized version of [`filter_map`](https://doc.rust-lang.org/stable/core/cell/struct.Ref.html#method.filter_map), which allows to return some data on failure.

## Safety

As far as I can tell, `E` cannot contain any `'b` data, so it is impossible to duplicate the `&'b [mut]` reference into the `RefCell`'s  data.

Other than this `E`, everything is analogous to the already stable `filter_map`.

## `Try` / `Residual`

I have considered generalizing this to use the `Try` & `Residual` just like rust-lang#79711 does for `array::try_map`, but it does not seem to be possible: we want to essentially `.map_err(|e| (orig, e))` but this does not seem to be supported with `Try`. (Plus I am not even sure if it is possible to express the fact that `&U` in `Try::Output` would have to have the same lifetime as the `&T` input of `F`.)

## ACP

~As far as I can tell, this [is not mandatory](https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html#suitability-for-the-standard-library), and the implementation is small enough to probably be smaller than the doc I would have to write.~

~rust-lang/libs-team#341

rust-lang/libs-team#586
bors added a commit that referenced this pull request Aug 15, 2025
Rollup of 22 pull requests

Successful merges:

 - #118087 (Add Ref/RefMut try_map method)
 - #122661 (Change the desugaring of `assert!` for better error output)
 - #140740 (Add `-Zindirect-branch-cs-prefix`)
 - #142640 (Implement autodiff using intrinsics)
 - #143075 (compiler: Allow `extern "interrupt" fn() -> !`)
 - #144865 (Fix tail calls to `#[track_caller]` functions)
 - #144944 (E0793: Clarify that it applies to unions as well)
 - #144947 (Fix description of unsigned `checked_exact_div`)
 - #145004 (Couple of minor cleanups)
 - #145005 (strip prefix of temporary file names when it exceeds filesystem name length limit)
 - #145012 (Tail call diagnostics to include lifetime info)
 - #145065 (resolve: Introduce `RibKind::Block`)
 - #145120 (llvm: Accept new LLVM lifetime format)
 - #145189 (Weekly `cargo update`)
 - #145235 (Minor `[const]` tweaks)
 - #145275 (fix(compiler/rustc_codegen_llvm): apply `target-cpu` attribute)
 - #145322 (Resolve the prelude import in `build_reduced_graph`)
 - #145331 (Make std use the edition 2024 prelude)
 - #145369 (Do not ICE on private type in field of unresolved struct)
 - #145378 (Add `FnContext` in parser for diagnostic)
 - #145389 ([rustdoc] Revert "rustdoc search: prefer stable items in search results")
 - #145392 (coverage: Remove intermediate data structures from mapping creation)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 15, 2025
…rk-Simulacrum

Add Ref/RefMut try_map method

Tracking issue:  rust-lang#143801

A more generalized version of [`filter_map`](https://doc.rust-lang.org/stable/core/cell/struct.Ref.html#method.filter_map), which allows to return some data on failure.

## Safety

As far as I can tell, `E` cannot contain any `'b` data, so it is impossible to duplicate the `&'b [mut]` reference into the `RefCell`'s  data.

Other than this `E`, everything is analogous to the already stable `filter_map`.

## `Try` / `Residual`

I have considered generalizing this to use the `Try` & `Residual` just like rust-lang#79711 does for `array::try_map`, but it does not seem to be possible: we want to essentially `.map_err(|e| (orig, e))` but this does not seem to be supported with `Try`. (Plus I am not even sure if it is possible to express the fact that `&U` in `Try::Output` would have to have the same lifetime as the `&T` input of `F`.)

## ACP

~As far as I can tell, this [is not mandatory](https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html#suitability-for-the-standard-library), and the implementation is small enough to probably be smaller than the doc I would have to write.~

~rust-lang/libs-team#341

rust-lang/libs-team#586
bors added a commit that referenced this pull request Aug 15, 2025
Rollup of 21 pull requests

Successful merges:

 - #118087 (Add Ref/RefMut try_map method)
 - #122661 (Change the desugaring of `assert!` for better error output)
 - #142640 (Implement autodiff using intrinsics)
 - #143075 (compiler: Allow `extern "interrupt" fn() -> !`)
 - #144865 (Fix tail calls to `#[track_caller]` functions)
 - #144944 (E0793: Clarify that it applies to unions as well)
 - #144947 (Fix description of unsigned `checked_exact_div`)
 - #145004 (Couple of minor cleanups)
 - #145005 (strip prefix of temporary file names when it exceeds filesystem name length limit)
 - #145012 (Tail call diagnostics to include lifetime info)
 - #145065 (resolve: Introduce `RibKind::Block`)
 - #145120 (llvm: Accept new LLVM lifetime format)
 - #145189 (Weekly `cargo update`)
 - #145235 (Minor `[const]` tweaks)
 - #145275 (fix(compiler/rustc_codegen_llvm): apply `target-cpu` attribute)
 - #145322 (Resolve the prelude import in `build_reduced_graph`)
 - #145331 (Make std use the edition 2024 prelude)
 - #145369 (Do not ICE on private type in field of unresolved struct)
 - #145378 (Add `FnContext` in parser for diagnostic)
 - #145389 ([rustdoc] Revert "rustdoc search: prefer stable items in search results")
 - #145392 (coverage: Remove intermediate data structures from mapping creation)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 324bf2b into rust-lang:master Aug 15, 2025
11 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Aug 15, 2025
rust-timer added a commit that referenced this pull request Aug 15, 2025
Rollup merge of #118087 - GrigorenkoPV:refcell_try_map, r=Mark-Simulacrum

Add Ref/RefMut try_map method

Tracking issue:  #143801

A more generalized version of [`filter_map`](https://doc.rust-lang.org/stable/core/cell/struct.Ref.html#method.filter_map), which allows to return some data on failure.

## Safety

As far as I can tell, `E` cannot contain any `'b` data, so it is impossible to duplicate the `&'b [mut]` reference into the `RefCell`'s  data.

Other than this `E`, everything is analogous to the already stable `filter_map`.

## `Try` / `Residual`

I have considered generalizing this to use the `Try` & `Residual` just like #79711 does for `array::try_map`, but it does not seem to be possible: we want to essentially `.map_err(|e| (orig, e))` but this does not seem to be supported with `Try`. (Plus I am not even sure if it is possible to express the fact that `&U` in `Try::Output` would have to have the same lifetime as the `&T` input of `F`.)

## ACP

~As far as I can tell, this [is not mandatory](https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html#suitability-for-the-standard-library), and the implementation is small enough to probably be smaller than the doc I would have to write.~

~rust-lang/libs-team#341

rust-lang/libs-team#586
@GrigorenkoPV GrigorenkoPV deleted the refcell_try_map branch August 15, 2025 18:51
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Aug 18, 2025
Rollup of 21 pull requests

Successful merges:

 - rust-lang/rust#118087 (Add Ref/RefMut try_map method)
 - rust-lang/rust#122661 (Change the desugaring of `assert!` for better error output)
 - rust-lang/rust#142640 (Implement autodiff using intrinsics)
 - rust-lang/rust#143075 (compiler: Allow `extern "interrupt" fn() -> !`)
 - rust-lang/rust#144865 (Fix tail calls to `#[track_caller]` functions)
 - rust-lang/rust#144944 (E0793: Clarify that it applies to unions as well)
 - rust-lang/rust#144947 (Fix description of unsigned `checked_exact_div`)
 - rust-lang/rust#145004 (Couple of minor cleanups)
 - rust-lang/rust#145005 (strip prefix of temporary file names when it exceeds filesystem name length limit)
 - rust-lang/rust#145012 (Tail call diagnostics to include lifetime info)
 - rust-lang/rust#145065 (resolve: Introduce `RibKind::Block`)
 - rust-lang/rust#145120 (llvm: Accept new LLVM lifetime format)
 - rust-lang/rust#145189 (Weekly `cargo update`)
 - rust-lang/rust#145235 (Minor `[const]` tweaks)
 - rust-lang/rust#145275 (fix(compiler/rustc_codegen_llvm): apply `target-cpu` attribute)
 - rust-lang/rust#145322 (Resolve the prelude import in `build_reduced_graph`)
 - rust-lang/rust#145331 (Make std use the edition 2024 prelude)
 - rust-lang/rust#145369 (Do not ICE on private type in field of unresolved struct)
 - rust-lang/rust#145378 (Add `FnContext` in parser for diagnostic)
 - rust-lang/rust#145389 ([rustdoc] Revert "rustdoc search: prefer stable items in search results")
 - rust-lang/rust#145392 (coverage: Remove intermediate data structures from mapping creation)

r? `@ghost`
`@rustbot` modify labels: rollup
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Aug 18, 2025
…rk-Simulacrum

Add Ref/RefMut try_map method

Tracking issue:  rust-lang#143801

A more generalized version of [`filter_map`](https://doc.rust-lang.org/stable/core/cell/struct.Ref.html#method.filter_map), which allows to return some data on failure.

## Safety

As far as I can tell, `E` cannot contain any `'b` data, so it is impossible to duplicate the `&'b [mut]` reference into the `RefCell`'s  data.

Other than this `E`, everything is analogous to the already stable `filter_map`.

## `Try` / `Residual`

I have considered generalizing this to use the `Try` & `Residual` just like rust-lang#79711 does for `array::try_map`, but it does not seem to be possible: we want to essentially `.map_err(|e| (orig, e))` but this does not seem to be supported with `Try`. (Plus I am not even sure if it is possible to express the fact that `&U` in `Try::Output` would have to have the same lifetime as the `&T` input of `F`.)

## ACP

~As far as I can tell, this [is not mandatory](https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html#suitability-for-the-standard-library), and the implementation is small enough to probably be smaller than the doc I would have to write.~

~rust-lang/libs-team#341

rust-lang/libs-team#586
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants