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

Rewrite builders to take Cows #2688

Merged
merged 1 commit into from
Jan 6, 2024
Merged

Conversation

GnomedDev
Copy link
Member

String -> Cow<'a, str>
Vec<T> -> Cow<'a, [T]>
impl IntoIterator<...> -> Cow<'a, [T]>

@github-actions github-actions bot added framework Related to the `framework` and `framework::standard` modules and/or the `command_attr` crate model Related to the `model` module. builder Related to the `builder` module. http Related to the `http` module. utils Related to the `utils` module. examples Related to Serenity's examples. labels Jan 5, 2024
@GnomedDev GnomedDev force-pushed the better-builders branch 4 times, most recently from 0d87060 to 4c73b15 Compare January 5, 2024 09:31
@arqunis arqunis added enhancement An improvement to Serenity. breaking change The public API is changed, resulting in miscompilations or unexpected new behaviour for users labels Jan 6, 2024
@arqunis arqunis merged commit 590cc89 into serenity-rs:next Jan 6, 2024
22 checks passed
Comment on lines -106 to +113
pub fn users(mut self, users: impl IntoIterator<Item = impl Into<UserId>>) -> Self {
self.users = users.into_iter().map(Into::into).collect();
pub fn users(mut self, users: impl Into<Cow<'a, [UserId]>>) -> Self {
self.users = users.into();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry for the late review on this. I feel like this is an ergonomics hit, since the Into<UserId> is gone.

Copy link
Member Author

Choose a reason for hiding this comment

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

I dislike the Into Id signatures personally, as it ruins the strict typing of the ID types. Since this is a performance hit for a signature style I don't like, I removed it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There's only a From<u64> impl on IDs. Since there's no way to implicitly cast an Id of one kind to a different one, I don't see how the strict typing is hurt. Being able to just pass in literals is a boon to ergonomics, and still guarantees that a list of multiple will all be converted into the same kind of Id.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not saying it allows for converting one type of ID to another, I believe that Id::new should be a boundary that needs explicitly typing to allow for the second of "wait, is this actually a user id".

Copy link
Collaborator

Choose a reason for hiding this comment

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

As opposed to what? Not many constants will look like IDs, unless maybe we're talking about UNIX timestamps. Plus, the builder methods are named well enough that they clue you in already.

What looks better to you?

m.allowed_mentions(Am::new().users([UserId::new(110372470472613888), UserId::new(1103724704727656385), UserId::new(110372470472918472)].as_slice()));

vs

m.allowed_mentions(Am::new().users(vec![110372470472613888, 1103724704727656385, 110372470472918472]));

Another thing: calling .into on each ID is equivalent to the first example but is no longer explicit about the Id kind:

m.allowed_mentions(Am::new().users([110372470472613888.into(), 1103724704727656385.into(), 110372470472918472.into()].as_slice()));

Mapping over the entire array is technically possible, but is still longer than passing in just the array:

m.allowed_mentions(Am::new().users([110372470472613888, 1103724704727656385 110372470472918472].map(UserId::from).as_slice()));

So, both sides of the Into<Cow> are resultantly longer/noisier. Besides, aren't builders relatively short-lived objects? They aren't cached in any way, so the only real performance hit is the cost of creating a few allocations. These aren't large objects either, so those allocator calls are not going to be expensive.

Copy link
Member Author

Choose a reason for hiding this comment

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

Small allocations still cause fragmentation and are just absolutely not needed. I agree this is a hit for passing u64s in, but in real bots these Ids should be in consts or config files anyway, I just believe this example shows the worst case scenario, which isn't even too bad.

@GnomedDev GnomedDev deleted the better-builders branch January 6, 2024 11:09
arqunis pushed a commit to arqunis/serenity that referenced this pull request Jan 16, 2024
arqunis pushed a commit to arqunis/serenity that referenced this pull request Jan 22, 2024
GnomedDev added a commit that referenced this pull request Feb 9, 2024
arqunis pushed a commit to arqunis/serenity that referenced this pull request Mar 1, 2024
GnomedDev added a commit that referenced this pull request Mar 10, 2024
GnomedDev added a commit that referenced this pull request Mar 11, 2024
GnomedDev added a commit that referenced this pull request Mar 11, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Mar 13, 2024
GnomedDev added a commit that referenced this pull request Mar 13, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Mar 19, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Mar 19, 2024
GnomedDev added a commit that referenced this pull request Mar 21, 2024
GnomedDev added a commit that referenced this pull request Mar 25, 2024
GnomedDev added a commit that referenced this pull request Mar 29, 2024
GnomedDev added a commit that referenced this pull request Mar 31, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Mar 31, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Apr 1, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request May 14, 2024
GnomedDev added a commit that referenced this pull request May 14, 2024
GnomedDev added a commit that referenced this pull request May 23, 2024
GnomedDev added a commit that referenced this pull request May 28, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Jun 9, 2024
GnomedDev added a commit that referenced this pull request Jun 22, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Jun 22, 2024
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Jul 29, 2024
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Jul 30, 2024
GnomedDev added a commit that referenced this pull request Aug 16, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Oct 7, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Oct 20, 2024
GnomedDev added a commit that referenced this pull request Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change The public API is changed, resulting in miscompilations or unexpected new behaviour for users builder Related to the `builder` module. enhancement An improvement to Serenity. examples Related to Serenity's examples. framework Related to the `framework` and `framework::standard` modules and/or the `command_attr` crate http Related to the `http` module. model Related to the `model` module. utils Related to the `utils` module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants