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

Minor: Allow Field::new and Field::new_with_dict to take existing String as well as &str #3288

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions arrow-schema/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ impl Hash for Field {

impl Field {
/// Creates a new field
pub fn new(name: &str, data_type: DataType, nullable: bool) -> Self {
pub fn new(name: impl Into<String>, data_type: DataType, nullable: bool) -> Self {
Field {
name: name.to_string(),
name: name.into(),
data_type,
nullable,
dict_id: 0,
Expand All @@ -125,14 +125,14 @@ impl Field {

/// Creates a new field that has additional dictionary information
pub fn new_dict(
name: &str,
name: impl Into<String>,
data_type: DataType,
nullable: bool,
dict_id: i64,
dict_is_ordered: bool,
) -> Self {
Field {
name: name.to_string(),
name: name.into(),
data_type,
nullable,
dict_id,
Expand Down Expand Up @@ -485,6 +485,20 @@ mod test {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

#[test]
fn test_new_with_string() {
// Fields should allow owned Strings to support reuse
let s = String::from("c1");
Field::new(s, DataType::Int64, false);
}
Comment on lines +489 to +493
Copy link
Member

@viirya viirya Dec 7, 2022

Choose a reason for hiding this comment

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

Not sure if I understand the purpose clearly. Doesn't it work for now:

let s = String::from("c1");
Field::new(&s, DataType::Int64, false);

Copy link
Contributor

Choose a reason for hiding this comment

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

Internally that will create a new memory allocation and copy the string data across, effectively this saves a Clone

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation!


#[test]
fn test_new_dict_with_string() {
// Fields should allow owned Strings to support reuse
let s = String::from("c1");
Field::new_dict(s, DataType::Int64, false, 4, false);
}

#[test]
fn test_merge_incompatible_types() {
let mut field = Field::new("c1", DataType::Int64, false);
Expand Down