# Please - give me cases that break this generic implementation of map() etc

**URL:** <https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405>\
**Category:** code review\
**Created:** [June 2, 2026, 12:30pm UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405 "2026-06-02T12:30:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![MusicalNinjaDad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/musicalninjadad/32/41229_2.png) [@MusicalNinjaDad](https://users.rust-lang.org/u/MusicalNinjaDad)\
**Post date:** [June 2, 2026, 12:30pm UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/1 "2026-06-02T12:30:57Z")

</div>

Hey folks,

Just crawling out of a rabbit-hole after spending a few hours crafting Try-related trait bounds and generic implementations of `map`, `zip`, `transpose` etc for any TryType

> **[Transform in try\_v2 - Rust](https://docs.rs/try_v2/0.5.1/try_v2/trait.Transform.html)**
>
> Methods for transforming TryTypes. Inspired by the methods provided on \`Option\` & \`Result\`

> <https://github.com/MusicalNinjaDad/try_v2/blob/main/crates/try_v2/src/transform.rs>

I'd be really grateful for ideas of cases where this is broken.

Thanks!

---

<div class="post-metadata">

**Author:** ![Guelakais](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/guelakais/32/34576_2.png) [@Guelakais](https://users.rust-lang.org/u/Guelakais)\
**Post date:** [June 3, 2026, 9:01am UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/2 "2026-06-03T09:01:32Z")

</div>

I'm a bit at a loss here, to be honest. I've had a look at your crate and compared it with the standard library. Everything you've implemented is already available in the standard library. What's more, the standard library explains how to use all these features. Unfortunately, your crate doesn't do that. So what is the purpose of your crate?

---

<div class="post-metadata">

**Author:** ![MusicalNinjaDad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/musicalninjadad/32/41229_2.png) [@MusicalNinjaDad](https://users.rust-lang.org/u/MusicalNinjaDad)\
**Post date:** [June 3, 2026, 12:28pm UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/3 "2026-06-03T12:28:41Z")

</div>

> [@Guelakais](#):
>
> So what is the purpose of your crate?

Great, eye-opening question.Thanks! (I'm not an AI, I really did learn to start a response like that when I grew up)

Right now `Option` & `Result` are the only\[1\] cases where you can apply `?`. There is an experimental feature on nightly that makes the underlying traits (`Try` etc.) available for anyone to implement on their own types. When you do that 2 things happen:

1. You write a load of boilerplate & risk a couple of footguns to implement the basic case (the crate provides a Derive macro for that)
2. You end up with a type that is missing fundamental ergonomic functions, which you only notice once some time later when you (and potentially others) are already using it and get stuck. **The trait above aims to resolve this**.

> [@Guelakais](#):
>
> Everything you've implemented is already available in the standard library.

That was the aim - and is good confirmation for me. (`Option` & `Result` both hand-roll these functions as direct methods) although I think there are a couple which are missing on one or the other (e.g. `Result` doesn't have `.zip()` IIRC)

> [@Guelakais](#):
>
> What's more, the standard library explains how to use all these features. Unfortunately, your crate doesn't

I hear you, documentation needs examples. Thanks - I would probably have skipped them in most cases for just a little more text\[2\].

* * *

1. `Poll` is a whole other discussion, so simplifying it away for this answer. 

2. My personal dev cycle usually starts with "Note to self" doc comments and ends with "full code & API review by writing docs, examples and polishing the code as I go" - I've not done that final step on this one as I'm not at the point I feel comfortable enough to say "time to polish this"

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [June 3, 2026, 4:28pm UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/4 "2026-06-03T16:28:03Z")

</div>

I think what this is really making me think is about this old thread:

> **[Something for coalescing; aka generalized/improved \`or\_else\`](https://internals.rust-lang.org/t/something-for-coalescing-aka-generalized-improved-or-else/9295?u=scottmcm)**
>
> It was noted in the await syntax thread that Option::or\_else mixes poorly with await (and ?): https://github.com/rust-lang/rust/issues/57640#issuecomment-457518407 Since one can think of ? as and\_then, would it make sense for there to be some...

Lots of those implementations you have are easy one-liners with `try`, like `map` is just `try { f(x?) }`, `flatten` is `try { a?? }`, and `zip` is `try { (a?, b?) }`.

The places that _aren't_ are things like `map_or`. So I'd love to have a generalized construct covering those cases too. Like if `unwrap_or_else` is `coalesce![a?, 0]` and `map_or_else` is `coalesce![f1(a?), f2()]` (or something).

---

<div class="post-metadata">

**Author:** ![MusicalNinjaDad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/musicalninjadad/32/41229_2.png) [@MusicalNinjaDad](https://users.rust-lang.org/u/MusicalNinjaDad)\
**Post date:** [June 4, 2026, 6:41am UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/5 "2026-06-04T06:41:34Z")

</div>

> [@scottmcm](#):
>
> Lots of those implementations you have are easy one-liners with `try`, like `map` is just `try { f(x?) }`, `flatten` is `try { a?? }`, and `zip` is `try { (a?, b?) }`.

yeah, I also prefer `try` blocks or bare `?`. I only missed the functions when I got into the middle of a functional chain, and was amazed just how simple some of them are when I started implementing them. And `flatten` only needs to be there to allow for custom implementations if needed\[1\].

> [@scottmcm](#):
>
> I think what this is really making me think is about this old thread:  
> ...  
> The places that _aren't_ are things like `map_or`. So I'd love to have a generalized construct covering those cases too. Like if `unwrap_or_else` is `coalesce![a?, 0]` and `map_or_else` is `coalesce![f1(a?), f2()]` (or something).

If spanish keyboards were standard we could have `¿` - so `unwrap_or_else` would be `¿a?,0¿` ... now I've written that ... maybe not ... but I do like the coalesce idea. Maybe an "inverted try" block:

```rust
yrt {
  a?; // desugars to return on Continue; continue on Break(Residual)
  0
}

```

which could even allow

```rust
yrt {
  let err = a?; // desugars to return on Continue; continue on Break(Residual)
  len(format!("{err}"))
}

```

although it probably adds too much cognitive complexity to remember that ? does something different, so we'd still need an alternative operator, and that seems to be where the old thread ran aground.

* * *

1. e.g. ... `Poll` 🍺

---

<div class="post-metadata">

**Author:** ![MusicalNinjaDad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/musicalninjadad/32/41229_2.png) [@MusicalNinjaDad](https://users.rust-lang.org/u/MusicalNinjaDad)\
**Post date:** [June 4, 2026, 6:53am UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/6 "2026-06-04T06:53:24Z")

</div>

I've also recently added `and`, `or` etc. and got stuck deciding whether to allow  
`ok(5).or(None) == Some(5)` or forbid `Result<_,E>::Ok(5).or(Result<_,F>::Ok(6))`

Any thoughts on how to add a restriction that forbids `Result.or(Option)` but allows `Result<_,E>.or(Result<_,F>)`\[1\]:

```rust
/// This will convert types! - so `Ok(5).or(None) = Some(5)`
    fn or<Y>(self, other: Y) -> Y
    where
        Y: Try<Output = Self::Output>,
    {
        match self.branch() {
            ControlFlow::Continue(v) => Try::from_output(v),
            ControlFlow::Break(_) => other,
        }
    }

```

* * *

1. They don't share a residual `Result<!,E>`vs `Result<!,F>`, or a canonical `TryType` 😢

---

<div class="post-metadata">

**Author:** ![ProgramCrafter](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/programcrafter/32/45916_2.png) [@ProgramCrafter](https://users.rust-lang.org/u/ProgramCrafter)\
**Post date:** [June 4, 2026, 7:58am UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/7 "2026-06-04T07:58:12Z")

</div>

`flip(): Result<T,E> -> Result<E,T>` would be simpler in that regard.

---

<div class="post-metadata">

**Author:** ![MusicalNinjaDad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/musicalninjadad/32/41229_2.png) [@MusicalNinjaDad](https://users.rust-lang.org/u/MusicalNinjaDad)\
**Post date:** [June 4, 2026, 9:12am UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/8 "2026-06-04T09:12:01Z")

</div>

but wouldn't work for `Option`, any TryType that has more than one residual variant, or where the residual and output variants have differently structured fields (which is why Option fails)

---

<div class="post-metadata">

**Author:** ![robofinch](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/robofinch/32/49287_2.png) [@robofinch](https://users.rust-lang.org/u/robofinch)\
**Post date:** [June 4, 2026, 11:56am UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/9 "2026-06-04T11:56:53Z")

</div>

I like the “inverted try” idea. And how about `try_not`? Would make for a good joke, too: “`try`, or `try_not`. There is no `do`.”

I’m not sure that there’s a good replacement for `?`, but maybe the “not” in `try_not` is a good enough hint?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [June 4, 2026, 6:05pm UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/10 "2026-06-04T18:05:49Z")

</div>

> [@MusicalNinjaDad](#):
>
> so we'd still need an alternative operator

TBH, I think that a coalesce like this is better _not_ just being a subtle operator the way `?` is.

The point of `?` is that you can mostly ignore it to read what the function does, and there's likely to be a whole bunch even in a single expression.

When instead you're doing the "try this, then that, then some other thing", I think expressing that in a more match-like structure that highlights the different cases is actually better.

`foo()¿.bar()¿.quz()¿` flowing data from one to the next makes much less sense for coalescing things than it does for bubbling with `?`.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 2, 2026, 6:06pm UTC](https://users.rust-lang.org/t/please-give-me-cases-that-break-this-generic-implementation-of-map-etc/140405/11 "2026-09-02T18:06:23Z")

</div>

Closed on September 2, 2026, 6:06pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
