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

refactor: use id::new_checked #1403

Closed
wants to merge 6 commits into from
Closed

refactor: use id::new_checked #1403

wants to merge 6 commits into from

Conversation

zeylahellyer
Copy link
Member

@zeylahellyer zeylahellyer commented Dec 30, 2021

Depends on #1402.

Use the new twilight_model::id::Id::new_checked method over the Id::new method, which creates a new ID from a value or otherwise panics. This is equivalent behavior to nearly all uses of Id::new in our codebase, and is primarily for tests.

To accomplish this two regexes were used with some manual cleaning up of others not included due to formatting not being taken into account (i.e. between the Id::new call and the Option::expect call).

Regex 1 input: Id::new\(([0-9_]*)\).expect\((.+?)\)
Regex 1 substitution: Id::new_checked($1)
Example:

-Some(test::user(Id::new(1).expect("non zero")))
+Some(test::user(Id::new_checked(1)))

Regex 2 input: Id(::<(.+?)Marker>)?::new\(([0-9_]*)\).expect\((.+?)\)
Regex 2 substitution: Id::<$1Marker>::new_checked($1)
Example:

-Id::<ChannelMarker>::new(123).expect("non zero")
+Id::<ChannelMarker>::new_checked(123)

Add a const method to IDs named `new_checked` that takes a value and
creates a non-zero ID, panicking if the value is 0. This is useful when
you have a hardcoded value, such as in tests or in const contexts.

Code such as:
```
let guild_id = GuildId::new(1).expect("non zero id");
```

can now become:

```
let guild_id = GuildId::new_checked(1);

// or
const GUILD_ID: GuildId = GuildId::new_checked(1);
```

Signed-off-by: Zeyla Hellyer <[email protected]>
Signed-off-by: Zeyla Hellyer <[email protected]>
Use the new `twilight_model::id::Id::new_checked` method over the
`Id::new` method, which creates a new ID from a value or otherwise
panics. This is equivalent behavior to nearly all uses of `Id::new` in
our codebase, and is primarily for tests.

To accomplish this two regexes were used with some manual cleaning up of
others not included due to formatting not being taken into account (i.e.
between the `Id::new` call and the `Option::expect` call).

Regex 1 input: `Id::new\(([0-9_]*)\).expect\((.+?)\)`
Regex 1 substitution: `Id::new_checked($1)`
Example:
```diff
-Some(test::user(Id::new(1).expect("non zero")))
+Some(test::user(Id::new_checked(1)))
```

Regex 2 input: `Id(::<(.+?)Marker>)?::new\(([0-9_]*)\).expect\((.+?)\)`
Regex 2 substitution: `Id::<$1Marker>::new_checked($1)`
Example:
```diff
-Id::<ChannelMarker>::new(123).expect("non zero")
+Id::<ChannelMarker>::new_checked(123)
```

Signed-off-by: Zeyla Hellyer <[email protected]>
@zeylahellyer zeylahellyer added c-all Affects all crates or the project as a whole m-breaking change Breaks the public API. t-refactor Refactors APIs or code. labels Dec 30, 2021
@zeylahellyer
Copy link
Member Author

Closing in favor of only having #1402.

@zeylahellyer zeylahellyer deleted the refactor-use-id-new-checked branch December 30, 2021 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-all Affects all crates or the project as a whole m-breaking change Breaks the public API. t-refactor Refactors APIs or code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant