Implementation:Pola rs Polars Polars Buffer Lib
| Knowledge Sources | |
|---|---|
| Domains | Memory_Management, Data_Structures |
| Last Updated | 2026-02-09 09:00 GMT |
Overview
Concrete tool for the polars-buffer library root that re-exports Buffer and SharedStorage types and provides range-checking utilities.
Description
The polars-buffer/src/lib.rs module serves as the crate root for the polars-buffer library. It re-exports the two primary types (Buffer and SharedStorage) and defines two public utility functions: check_range (a bounds-checked range decoder that panics on invalid ranges, used by Buffer::slice_in_place) and decode_range_unchecked (an unchecked variant for performance-critical paths). Both functions convert any RangeBounds<usize> into a concrete Range<usize> relative to a given upper bound, handling all six range bound variants (Included, Excluded, Unbounded for both start and end).
Usage
Import from polars_buffer to access Buffer<T>, SharedStorage<T>, check_range, and decode_range_unchecked. The check_range function is useful when implementing custom sliceable data structures that need bounds validation matching the standard library's behavior.
Code Reference
Source Location
- Repository: Pola_rs_Polars
- File: crates/polars-buffer/src/lib.rs
- Lines: 1-77
Signature
pub mod buffer;
pub mod storage;
pub use buffer::Buffer;
pub use storage::SharedStorage;
pub fn check_range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>
where
R: ops::RangeBounds<usize>;
pub fn decode_range_unchecked<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>
where
R: ops::RangeBounds<usize>;
Import
use polars_buffer::{Buffer, SharedStorage, check_range, decode_range_unchecked};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| range | R: RangeBounds<usize> | Yes | Any range type (e.g., 1..5, ..3, 2..) |
| bounds | RangeTo<usize> | Yes | Upper bound for validation (e.g., ..len) |
Outputs
| Name | Type | Description |
|---|---|---|
| check_range returns | Range<usize> | Concrete start..end range (panics if invalid) |
| decode_range_unchecked returns | Range<usize> | Concrete start..end range (undefined if invalid) |
Usage Examples
Bounds-Checked Range Decoding
use polars_buffer::check_range;
// Decode a partial range against a length of 10
let range = check_range(2..7, ..10);
assert_eq!(range, 2..7);
// Unbounded start defaults to 0
let range = check_range(..5, ..10);
assert_eq!(range, 0..5);
// Unbounded end defaults to the length
let range = check_range(3.., ..10);
assert_eq!(range, 3..10);