Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Boxed owned query parameters w/ impl for From<T>#859

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

elpiel
Copy link
Contributor

Extension of#853 that allows creation of aBox<dyn ToSql + Send + Sync> fromT: ToSql

@elpielelpielforce-pushed thefrom-T-for-boxed-dyn-ToSql branch from2c9b9a6 tof9fd5ceCompareDecember 31, 2021 15:39
fnfrom(to_sql:T) ->Self{
Box::new(to_sql)
}
}
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

We could also add impl for Sync only, although I'm not sure if/when it would be needed:

Suggested change
}
}
impl<'a,T:ToSql +Sync +'a>From<T>forBox<dynToSql +Sync +'a>{
fn from(to_sql:T) ->Self{
Box::new(to_sql)
}
}

@elpielelpiel mentioned this pull requestDec 31, 2021
@sfackler
Copy link
Collaborator

I'm not sure I've ever seen this conversion before for other traits. What's the rationale for it?

@elpiel
Copy link
ContributorAuthor

elpiel commentedJan 1, 2022
edited
Loading

I'm not sure I've ever seen this conversion before for other traits. What's the rationale for it?

As mentioned in the comment, this allows you to build aBox<dyn ToSql> from any type and can be used to create aVec of these values that can be passed to thequery_raw().

This is necessary in order to build these values e.g. dynamically and I couldn't find any other way to do so.
The example ofquery_raw uses onlyStrings (1 type) so this issue is not present.

Example:

// These parameters can now come from e.g. another function call and can be mixed typeslet params: Vec<Box<dyn ToSql + Sync + Send>> = vec![    "first param".into(),    42.into(),];let mut it = client.query_raw(    "SELECT foo FROM bar WHERE biz = $1 AND baz = $2",    params,).await?;

@sfackler
Copy link
Collaborator

Can't you just call Box::new yourself?

@elpiel
Copy link
ContributorAuthor

Can't you just call Box::new yourself?

Maybe I just don't know how to do the casting since I get a type error:

17 |         pub fn get_key(&self, key: AllowedKey) -> Option<Box<dyn ToSql + Sync + Send>> {   |                                                   ------------------------------------ expected `std::option::Option<Box<(dyn ToSql + Send + Sync + 'static)>>` because of return type18 |             match key {19 |                 AllowedKey::CampaignId => self.campaign_id.map(Box::new),   |                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn ToSql`, found struct `CampaignId`   |   = note: expected enum `std::option::Option<Box<(dyn ToSql + Send + Sync + 'static)>>`              found enum `std::option::Option<Box<CampaignId>>`

I've also tried the following but then I get an error because ofSized requirements forBox::new

self.campaign_id.map(|s|Box::new(sas_)),

@sfackler
Copy link
Collaborator

Without seeing the rest of the code I can't be 100% sure, but one of these should work:self.campaign_id.map(|s| Box::new(s)) orself.campaign_id.map(|s| Box::new(s) as _).

@elpiel
Copy link
ContributorAuthor

elpiel commentedJan 3, 2022
edited
Loading

Without seeing the rest of the code I can't be 100% sure, but one of these should work:self.campaign_id.map(|s| Box::new(s)) orself.campaign_id.map(|s| Box::new(s) as _).

Here are links to the playground with 3 non-working examples:

@sfackler
Copy link
Collaborator

sfackler commentedJan 3, 2022
edited
Loading

@elpiel
Copy link
ContributorAuthor

@elpielelpiel closed thisJan 4, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@elpiel@sfackler

[8]ページ先頭

©2009-2025 Movatter.jp