You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by tu...@apache.org on 2022/12/08 10:05:09 UTC

[arrow-rs] branch master updated: Minor: Allow Field::new to take existing `String` as well as &str (#3288)

This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 16484a6d8 Minor: Allow Field::new to take existing `String` as well as &str (#3288)
16484a6d8 is described below

commit 16484a6d841ebe2347dffbaa1b14d06392944cbc
Author: Andrew Lamb <an...@nerdnetworks.org>
AuthorDate: Thu Dec 8 05:05:04 2022 -0500

    Minor: Allow Field::new to take existing `String` as well as &str (#3288)
---
 arrow-schema/src/field.rs | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/arrow-schema/src/field.rs b/arrow-schema/src/field.rs
index 5813902dd..a3275dcb3 100644
--- a/arrow-schema/src/field.rs
+++ b/arrow-schema/src/field.rs
@@ -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,
@@ -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,
@@ -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);
+    }
+
+    #[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);