Skip to content

Commit

Permalink
Add docstring and example to Scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Sep 7, 2023
1 parent dd0c4ab commit 8af24e3
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions arrow-array/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,32 @@ impl Datum for &dyn Array {
}
}

/// A wrapper around a single value [`Array`] indicating kernels should treat it as a scalar value
/// A wrapper around a single value [`Array`] that implements
/// [`Datum`] and indicates [compute] kernels should treat this array
/// as a scalar value (a single value).
///
/// See [`Datum`] for more information
/// Using a [`Scalar`] is often much more efficient than creating an
/// [`Array`] with the same (repeated) value.
///
/// See [`Datum`] for more information.
///
/// # Example
///
/// ```rust
/// # use arrow_array::{Scalar, Int32Array, ArrayRef};
/// # fn get_array() -> ArrayRef { std::sync::Arc::new(Int32Array::from(vec![42])) }
/// // Create a (typed) scalar for Int32Array for the value 42
/// let scalar = Scalar::new(Int32Array::from(vec![42]));
///
/// // Create a scalar using PrimtiveArray::scalar
/// let scalar = Int32Array::new_scalar(42);
///
/// // create a scalar from an ArrayRef (for dynamic typed Arrays)
/// let array: ArrayRef = get_array();
/// let scalar = Scalar::new(array);
/// ```
///
/// [compute]: https://docs.rs/arrow/latest/arrow/compute/index.html
#[derive(Debug, Copy, Clone)]
pub struct Scalar<T: Array>(T);

Expand Down

0 comments on commit 8af24e3

Please sign in to comment.