From 7dd831c75da2274e3b5bb3c0af6241e1b44e059b Mon Sep 17 00:00:00 2001 From: Lachlan Deakin Date: Wed, 14 Feb 2024 08:19:59 +1100 Subject: [PATCH 1/3] Implement `From` for `DimensionName` --- src/array/dimension_name.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/array/dimension_name.rs b/src/array/dimension_name.rs index c50666ee..32835e0a 100644 --- a/src/array/dimension_name.rs +++ b/src/array/dimension_name.rs @@ -27,8 +27,14 @@ impl DimensionName { } impl From<&str> for DimensionName { - fn from(value: &str) -> Self { - Self(Some(value.into())) + fn from(name: &str) -> Self { + Self(Some(name.into())) + } +} + +impl From for DimensionName { + fn from(name: String) -> Self { + Self(Some(name)) } } From ec0aa01789ff12321c37dc4c7ead5e51a1fb3595 Mon Sep 17 00:00:00 2001 From: Lachlan Deakin Date: Wed, 14 Feb 2024 08:32:17 +1100 Subject: [PATCH 2/3] `DimensionName::new()` now accepts a name implementing `Into` --- CHANGELOG.md | 2 ++ src/array/dimension_name.rs | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85a5e7b5..b17dca21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Breaking** Existing `Array` `_opt` use new encode/decode options insted of `parallel: bool` - Implement `DoubleEndedIterator` for `{Indices,LinearisedIndices,ContiguousIndices,ContiguousLinearisedIndicesIterator}Iterator` - Add `ParIndicesIterator` and `ParChunksIterator` + - Implement `From` for `DimensionName` ### Changed - Dependency bumps @@ -51,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Indices` and `Chunks` implement `IntoParallelIter` - **Breaking**: array subset iterators are moved into public `array_subset::iterators` and no longer in the `array_subset` namespace - Add a fast path to `Array::retrieve_chunk_subset{_opt}` if the entire chunk is requested + - `DimensionName::new()` now accepts anything implementing `Into` ### Removed - **Breaking**: remove `InvalidArraySubsetError` and `ArrayExtractElementsError` diff --git a/src/array/dimension_name.rs b/src/array/dimension_name.rs index 32835e0a..1144967d 100644 --- a/src/array/dimension_name.rs +++ b/src/array/dimension_name.rs @@ -15,7 +15,7 @@ impl Default for DimensionName { impl DimensionName { /// Create a new dimension with `name`. Use [`default`](DimensionName::default) to create a dimension with no name. #[must_use] - pub fn new(name: &str) -> Self { + pub fn new>(name: T) -> Self { Self(Some(name.into())) } @@ -28,13 +28,13 @@ impl DimensionName { impl From<&str> for DimensionName { fn from(name: &str) -> Self { - Self(Some(name.into())) + Self::new(name) } } impl From for DimensionName { fn from(name: String) -> Self { - Self(Some(name)) + Self::new(name) } } From 6df10dbb3958d6966ad9d7663b45726d8070101a Mon Sep 17 00:00:00 2001 From: Lachlan Deakin Date: Wed, 14 Feb 2024 08:47:48 +1100 Subject: [PATCH 3/3] Make `ArrayBuilder::dimension_names()` generic --- CHANGELOG.md | 5 ++++- examples/array_write_read.rs | 2 +- examples/async_array_write_read.rs | 2 +- examples/rectangular_array_write_read.rs | 2 +- examples/sharded_array_write_read.rs | 2 +- examples/zip_array_write_read.rs | 2 +- src/array/array_builder.rs | 23 ++++++++++++++++++----- 7 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b17dca21..2b5a3df6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Indices` and `Chunks` implement `IntoParallelIter` - **Breaking**: array subset iterators are moved into public `array_subset::iterators` and no longer in the `array_subset` namespace - Add a fast path to `Array::retrieve_chunk_subset{_opt}` if the entire chunk is requested - - `DimensionName::new()` now accepts anything implementing `Into` + - `DimensionName::new()` generalised to accept a name implementing `Into` + - **Breaking**: `ArrayBuilder::dimension_names()` generalised to accept `Option` where `I: IntoIterator` and `D: Into` + - Can now write +`builder.dimension_names(["y", "x"].into())` instead of `builder.dimension_names(vec!["y".into(), "x".into()].into())` ### Removed - **Breaking**: remove `InvalidArraySubsetError` and `ArrayExtractElementsError` diff --git a/examples/array_write_read.rs b/examples/array_write_read.rs index 5dd7d68a..1d45cf9e 100644 --- a/examples/array_write_read.rs +++ b/examples/array_write_read.rs @@ -43,7 +43,7 @@ fn array_write_read() -> Result<(), Box> { FillValue::from(ZARR_NAN_F32), ) // .bytes_to_bytes_codecs(vec![]) // uncompressed - .dimension_names(Some(vec!["y".into(), "x".into()])) + .dimension_names(["y", "x"].into()) // .storage_transformers(vec![].into()) .build(store.clone(), array_path)?; diff --git a/examples/async_array_write_read.rs b/examples/async_array_write_read.rs index 0cbfc92f..56ace1c0 100644 --- a/examples/async_array_write_read.rs +++ b/examples/async_array_write_read.rs @@ -44,7 +44,7 @@ async fn async_array_write_read() -> Result<(), Box> { FillValue::from(ZARR_NAN_F32), ) // .bytes_to_bytes_codecs(vec![]) // uncompressed - .dimension_names(Some(vec!["y".into(), "x".into()])) + .dimension_names(["y", "x"].into()) // .storage_transformers(vec![].into()) .build(store.clone(), array_path)?; diff --git a/examples/rectangular_array_write_read.rs b/examples/rectangular_array_write_read.rs index 25b26e72..9c13ae3a 100644 --- a/examples/rectangular_array_write_read.rs +++ b/examples/rectangular_array_write_read.rs @@ -48,7 +48,7 @@ fn rectangular_array_write_read() -> Result<(), Box> { #[cfg(feature = "gzip")] Box::new(codec::GzipCodec::new(5)?), ]) - .dimension_names(Some(vec!["y".into(), "x".into()])) + .dimension_names(["y", "x"].into()) // .storage_transformers(vec![].into()) .build(store.clone(), array_path)?; diff --git a/examples/sharded_array_write_read.rs b/examples/sharded_array_write_read.rs index fb966189..4740d692 100644 --- a/examples/sharded_array_write_read.rs +++ b/examples/sharded_array_write_read.rs @@ -66,7 +66,7 @@ fn sharded_array_write_read() -> Result<(), Box> { FillValue::from(0u16), ) .array_to_bytes_codec(Box::new(sharding_codec_builder.build())) - .dimension_names(Some(vec!["y".into(), "x".into()])) + .dimension_names(["y", "x"].into()) // .storage_transformers(vec![].into()) .build(store.clone(), array_path)?; diff --git a/examples/zip_array_write_read.rs b/examples/zip_array_write_read.rs index a94c7cb9..bc1f1627 100644 --- a/examples/zip_array_write_read.rs +++ b/examples/zip_array_write_read.rs @@ -30,7 +30,7 @@ fn write_array_to_storage( #[cfg(feature = "gzip")] Box::new(codec::GzipCodec::new(5)?), ]) - .dimension_names(Some(vec!["y".into(), "x".into()])) + .dimension_names(["y", "x"].into()) // .storage_transformers(vec![].into()) .build(storage, ARRAY_PATH)?; diff --git a/src/array/array_builder.rs b/src/array/array_builder.rs index 360d216a..c9a9984d 100644 --- a/src/array/array_builder.rs +++ b/src/array/array_builder.rs @@ -39,7 +39,7 @@ use super::{ /// #[cfg(feature = "gzip")] /// Box::new(zarrs::array::codec::GzipCodec::new(5)?), /// ]) -/// .dimension_names(Some(vec!["y".into(), "x".into()])) +/// .dimension_names(["y", "x"].into()) /// .build(store.clone(), "/group/array")?; /// array.store_metadata()?; // write metadata to the store /// @@ -231,8 +231,21 @@ impl ArrayBuilder { /// Set the dimension names. /// /// If left unmodified, all dimension names are "unnamed". - pub fn dimension_names(&mut self, dimension_names: Option>) -> &mut Self { - self.dimension_names = dimension_names; + pub fn dimension_names(&mut self, dimension_names: Option) -> &mut Self + where + I: IntoIterator, + D: Into, + { + if let Some(dimension_names) = dimension_names { + self.dimension_names = Some( + dimension_names + .into_iter() + .map(Into::::into) + .collect(), + ); + } else { + self.dimension_names = None; + } self } @@ -332,7 +345,7 @@ mod tests { ))); builder.fill_value(FillValue::from(0i8)); - builder.dimension_names(Some(vec!["y".into(), "x".into()])); + builder.dimension_names(["y", "x"].into()); let mut attributes = serde_json::Map::new(); attributes.insert("key".to_string(), "value".into()); @@ -400,7 +413,7 @@ mod tests { vec![2, 2].try_into().unwrap(), FillValue::from(0i8), ); - builder.dimension_names(Some(vec!["z".into(), "y".into(), "x".into()])); + builder.dimension_names(["z", "y", "x"].into()); assert!(builder.build(storage.clone(), "/").is_err()); } }