Skip to content

Conversation

torsteingrindvik
Copy link
Contributor

@torsteingrindvik torsteingrindvik commented Nov 21, 2022

Objective

add_node_edge and add_slot_edge are fallible methods, but are always used with .unwrap().
input_node is often unwrapped as well.
This points to having an infallible behaviour as default, with an alternative fallible variant if needed.

Improves readability and ergonomics.

Solution

  • Change add_node_edge and add_slot_edge to panic on error.
  • Change input_node to panic on None.
  • Add try_add_node_edge and try_add_slot_edge in case fallible methods are needed.
  • Add get_input_node to still be able to get an Option.

Changelog

Added

  • try_add_node_edge
  • try_add_slot_edge
  • get_input_node

Changed

  • add_node_edge is now infallible (panics on error)
  • add_slot_edge is now infallible (panics on error)
  • input_node now panics on None

Migration Guide

Remove .unwrap() from add_node_edge and add_slot_edge.
For cases where the error was handled, use try_add_node_edge and try_add_slot_edge instead.

Remove .unwrap() from input_node.
For cases where the option was handled, use get_input_node instead.

Signed-off-by: Torstein Grindvik <torstein.grindvik@nordicsemi.no>
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Nov 21, 2022
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On board with this direction: I like this as an API pattern a lot.

I'm noticing the same pattern node_id().unwrap() while reviewing. Should we change that too? Should it be in the same PR?

Copy link
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, such a simple change but it makes the render graph a lot nicer!

@alice-i-cecile alice-i-cecile added this to the 0.10 milestone Nov 21, 2022
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Nov 21, 2022
@torsteingrindvik
Copy link
Contributor Author

@alice-i-cecile About input_node(), I agree.
I think try_input_node() would be misleading since the return is Option and not Result though.

I saw in rustlang that there is precedence for having a checked_foo for Option<T> when foo returns T.

So should I make the change for input_node() -> infallible, and add input_node_checked()?

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Nov 21, 2022

I would use input_node and get_input_node personally :) Remember to update the PR description!

@torsteingrindvik
Copy link
Contributor Author

I'll use that then, it's unsurprising which is good.

I was a bit thrown off by this: https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter

But I'm not sure what the justification is there and anyway I see Bevy uses a lot of get_foo.

I'll update the description.

@IceSentry
Copy link
Contributor

Yeah, it's pretty much a bevy convention that when you want a fallible getter you add the get_ otherwise you just name it without the prefix.

Signed-off-by: Torstein Grindvik <torstein.grindvik@nordicsemi.no>
@cart
Copy link
Member

cart commented Nov 21, 2022

bors r+

@torsteingrindvik
Copy link
Contributor Author

Ok, that's good to know.
Description updated.

Hopefully CI passes, there was a strange error last run.

bors bot pushed a commit that referenced this pull request Nov 21, 2022
# Objective

`add_node_edge` and `add_slot_edge` are fallible methods, but are always used with `.unwrap()`.
`input_node` is often unwrapped as well.
This points to having an infallible behaviour as default, with an alternative fallible variant if needed.

Improves readability and ergonomics.

## Solution

- Change `add_node_edge` and `add_slot_edge` to panic on error.
- Change `input_node` to panic on `None`.
- Add `try_add_node_edge` and `try_add_slot_edge` in case fallible methods are needed.
- Add `get_input_node` to still be able to get an `Option`.
---

## Changelog

### Added

- `try_add_node_edge`
- `try_add_slot_edge`
- `get_input_node`

### Changed

- `add_node_edge` is now infallible (panics on error)
- `add_slot_edge` is now infallible (panics on error)
- `input_node` now panics on `None`

## Migration Guide

Remove `.unwrap()` from `add_node_edge` and `add_slot_edge`.
For cases where the error was handled, use `try_add_node_edge` and `try_add_slot_edge` instead.

Remove `.unwrap()` from `input_node`.
For cases where the option was handled, use `get_input_node` instead.


Co-authored-by: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com>
@bors bors bot changed the title Add try_* to add_slot_edge, add_node_edge [Merged by Bors] - Add try_* to add_slot_edge, add_node_edge Nov 21, 2022
@bors bors bot closed this Nov 21, 2022
@torsteingrindvik torsteingrindvik deleted the nodes-try-vs-unwrap branch November 21, 2022 22:28
taiyoungjang pushed a commit to taiyoungjang/bevy that referenced this pull request Dec 15, 2022
# Objective

`add_node_edge` and `add_slot_edge` are fallible methods, but are always used with `.unwrap()`.
`input_node` is often unwrapped as well.
This points to having an infallible behaviour as default, with an alternative fallible variant if needed.

Improves readability and ergonomics.

## Solution

- Change `add_node_edge` and `add_slot_edge` to panic on error.
- Change `input_node` to panic on `None`.
- Add `try_add_node_edge` and `try_add_slot_edge` in case fallible methods are needed.
- Add `get_input_node` to still be able to get an `Option`.
---

## Changelog

### Added

- `try_add_node_edge`
- `try_add_slot_edge`
- `get_input_node`

### Changed

- `add_node_edge` is now infallible (panics on error)
- `add_slot_edge` is now infallible (panics on error)
- `input_node` now panics on `None`

## Migration Guide

Remove `.unwrap()` from `add_node_edge` and `add_slot_edge`.
For cases where the error was handled, use `try_add_node_edge` and `try_add_slot_edge` instead.

Remove `.unwrap()` from `input_node`.
For cases where the option was handled, use `get_input_node` instead.


Co-authored-by: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com>
alradish pushed a commit to alradish/bevy that referenced this pull request Jan 22, 2023
# Objective

`add_node_edge` and `add_slot_edge` are fallible methods, but are always used with `.unwrap()`.
`input_node` is often unwrapped as well.
This points to having an infallible behaviour as default, with an alternative fallible variant if needed.

Improves readability and ergonomics.

## Solution

- Change `add_node_edge` and `add_slot_edge` to panic on error.
- Change `input_node` to panic on `None`.
- Add `try_add_node_edge` and `try_add_slot_edge` in case fallible methods are needed.
- Add `get_input_node` to still be able to get an `Option`.
---

## Changelog

### Added

- `try_add_node_edge`
- `try_add_slot_edge`
- `get_input_node`

### Changed

- `add_node_edge` is now infallible (panics on error)
- `add_slot_edge` is now infallible (panics on error)
- `input_node` now panics on `None`

## Migration Guide

Remove `.unwrap()` from `add_node_edge` and `add_slot_edge`.
For cases where the error was handled, use `try_add_node_edge` and `try_add_slot_edge` instead.

Remove `.unwrap()` from `input_node`.
For cases where the option was handled, use `get_input_node` instead.


Co-authored-by: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

`add_node_edge` and `add_slot_edge` are fallible methods, but are always used with `.unwrap()`.
`input_node` is often unwrapped as well.
This points to having an infallible behaviour as default, with an alternative fallible variant if needed.

Improves readability and ergonomics.

## Solution

- Change `add_node_edge` and `add_slot_edge` to panic on error.
- Change `input_node` to panic on `None`.
- Add `try_add_node_edge` and `try_add_slot_edge` in case fallible methods are needed.
- Add `get_input_node` to still be able to get an `Option`.
---

## Changelog

### Added

- `try_add_node_edge`
- `try_add_slot_edge`
- `get_input_node`

### Changed

- `add_node_edge` is now infallible (panics on error)
- `add_slot_edge` is now infallible (panics on error)
- `input_node` now panics on `None`

## Migration Guide

Remove `.unwrap()` from `add_node_edge` and `add_slot_edge`.
For cases where the error was handled, use `try_add_node_edge` and `try_add_slot_edge` instead.

Remove `.unwrap()` from `input_node`.
For cases where the option was handled, use `get_input_node` instead.


Co-authored-by: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants