Skip to content

Commit

Permalink
Add as_slice method for HistogramCounts
Browse files Browse the repository at this point in the history
  • Loading branch information
spadarian committed Dec 2, 2023
1 parent 1392a9d commit d2960a2
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,7 @@ impl Histogram {

/// Histogram values for each bucket
pub fn counts(&self) -> &[u64] {
match &self.counts {
HistogramCounts::GdalAllocated(p, n) => unsafe { std::slice::from_raw_parts(*p, *n) },
HistogramCounts::RustAllocated(v) => v.as_slice(),
}
self.counts.as_slice()
}

/// Number of buckets in histogram
Expand All @@ -993,23 +990,26 @@ impl Histogram {
/// * `GDALGetDefaultHistogram`: returns a pointer (via an 'out' parameter) to a GDAL allocated array,
/// stored in `HistogramCounts::GdalAllocated`, to be deallocated with [`VSIFree`][gdal_sys::VSIFree].
enum HistogramCounts {
/// Pointer to GDAL allocated array and lenght of histogram counts.
/// Pointer to GDAL allocated array and length of histogram counts.
///
/// Requires freeing with [`VSIFree`][gdal_sys::VSIFree].
GdalAllocated(*mut u64, usize),
/// Rust-allocated vector into which GDAL stores histogram counts.
RustAllocated(Vec<u64>),
}

impl HistogramCounts {
fn as_slice(&self) -> &[u64] {
match &self {
Self::GdalAllocated(p, n) => unsafe { std::slice::from_raw_parts(*p, *n) },
Self::RustAllocated(v) => v.as_slice(),
}
}
}

impl Debug for HistogramCounts {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
HistogramCounts::GdalAllocated(p, n) => {
let s = unsafe { std::slice::from_raw_parts(*p, *n) };
s.fmt(f)
}
HistogramCounts::RustAllocated(v) => v.fmt(f),
}
self.as_slice().fmt(f)
}
}

Expand Down

0 comments on commit d2960a2

Please sign in to comment.