-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
use Into<String> as argument type wherever applicable #615
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the value in a few of these changes, but otherwise things look good to me. Thank you @houqp
options: CsvReadOptions, | ||
projection: Option<Vec<usize>>, | ||
) -> Result<Self> { | ||
let provider = Arc::new(CsvFile::try_new(path, options)?); | ||
let path = path.into(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, this pattern just doesn't make a lot of sense to me -- if someone passed in a &str
then this code will now create a copy (a String
) and then simply use a reference to that copy (rather than the path itself)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the owned value is passed along to Self::scan
at the end of the function.
provider: Arc<dyn TableProvider>, | ||
projection: Option<Vec<usize>>, | ||
) -> Result<Self> { | ||
let table_name = table_name.into(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change makes sense to me as this function previously was making an explicit copy and no can reuse the copy the caller passed in, if any 👍
table_name: table_name.to_string(),
@@ -44,10 +44,10 @@ pub struct Column { | |||
|
|||
impl Column { | |||
/// Create Column from unqualified name. | |||
pub fn from_name(name: String) -> Self { | |||
pub fn from_name(name: impl Into<String>) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
I think this one is ready - it just needs to be rebased |
Rationale for this change
Following the article shared by @jorgecarleitao: https://phaazon.net/blog/on-owning-borrowing-pub-interface. Using
impl Into<T>
as argument type makes it possible for function to take ownership of an argument while providing the flexibility to the callers to decide whether they want to pass argument by reference or by value.@Dandandan also proposed this pattern in #55 for the builder interface: https://github.com/apache/arrow-datafusion/blob/d55a10569b3a24195bed2d67cc6414c63b6b2336/datafusion/src/logical_plan/builder.rs#L264
I just never thought of extending it to other existing functions and types until I saw that article. I think we can apply this same pattern to other types as well like
LogicalPlanBuilder::from
, which takes a reference of LogicalPlan as argument and clones it internally.What changes are included in this PR?
Change
&str
toimpl Into<String>
for functions that does internal clone of the string.Are there any user-facing changes?
No breaking API change.