Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 55 additions & 24 deletions arrow-data/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use std::mem;
use std::ops::Range;
use std::sync::Arc;

use crate::data::private::UnsafeFlag;
use crate::{equal, validate_binary_view, validate_string_view};

#[inline]
Expand Down Expand Up @@ -1783,33 +1782,55 @@ impl PartialEq for ArrayData {
}
}

mod private {
/// A boolean flag that cannot be mutated outside of unsafe code.
///
/// Defaults to a value of false.
/// A boolean flag that cannot be mutated outside of unsafe code.
///
/// Defaults to a value of false.
///
/// This structure is used to enforce safety in the [`ArrayDataBuilder`]
///
/// [`ArrayDataBuilder`]: super::ArrayDataBuilder
///
/// # Example
/// ```rust
/// use arrow_data::UnsafeFlag;
/// assert!(!UnsafeFlag::default().get()); // default is false
/// let mut flag = UnsafeFlag::new();
/// assert!(!flag.get()); // defaults to false
/// // can only set it to true in unsafe code
/// unsafe { flag.set(true) };
/// assert!(flag.get()); // now true
/// ```
#[derive(Debug, Copy, Clone)]
pub struct UnsafeFlag(bool);

impl UnsafeFlag {
/// Creates a new `UnsafeFlag` with the value set to `false`
///
/// This structure is used to enforce safety in the [`ArrayDataBuilder`]
/// See examples on [`Self::new`]
#[inline]
pub const fn new() -> Self {
Self(false)
}

/// Sets the value of the flag to the given value
///
/// [`ArrayDataBuilder`]: super::ArrayDataBuilder
#[derive(Debug)]
pub struct UnsafeFlag(bool);

impl UnsafeFlag {
/// Creates a new `UnsafeFlag` with the value set to `false`
#[inline]
pub const fn new() -> Self {
Self(false)
}
/// Note this can only be done in `unsafe` code
#[inline]
pub unsafe fn set(&mut self, val: bool) {
self.0 = val;
}

#[inline]
pub unsafe fn set(&mut self, val: bool) {
self.0 = val;
}
/// Returns the value of the flag
#[inline]
pub fn get(&self) -> bool {
self.0
}
}

#[inline]
pub fn get(&self) -> bool {
self.0
}
// Manual impl to make it clear you can not construct unsafe with true
impl Default for UnsafeFlag {
fn default() -> Self {
Self::new()
}
}

Expand Down Expand Up @@ -2040,6 +2061,16 @@ impl ArrayDataBuilder {
self.skip_validation.set(skip_validation);
self
}

/// Specifies skipping validation of the data based on an [`UnsafeFlag`]
///
/// # Safety
/// While this function is safe, setting the flag to true can only be done
/// in `unsafe` code. See [`Self::skip_validation`] for more details
pub fn with_skip_validation(mut self, skip_validation: UnsafeFlag) -> Self {
self.skip_validation = skip_validation;
self
}
}

impl From<ArrayData> for ArrayDataBuilder {
Expand Down
Loading
Loading