Skip to content

Commit

Permalink
Add docstring and example to Scalar (#4793)
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb authored Sep 8, 2023
1 parent 1e46f8f commit 878217b
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 878217b

Please sign in to comment.