Skip to content

Commit 2e39fe4

Browse files
committed
Make UnsafeFlag pub
1 parent 7c663ac commit 2e39fe4

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

arrow-data/src/data.rs

+50-23
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use std::mem;
2828
use std::ops::Range;
2929
use std::sync::Arc;
3030

31-
use crate::data::private::UnsafeFlag;
3231
use crate::{equal, validate_binary_view, validate_string_view};
3332

3433
#[inline]
@@ -1781,33 +1780,61 @@ impl PartialEq for ArrayData {
17811780
}
17821781
}
17831782

1784-
mod private {
1785-
/// A boolean flag that cannot be mutated outside of unsafe code.
1783+
/// A boolean flag that cannot be mutated outside of unsafe code.
1784+
///
1785+
/// Defaults to a value of false.
1786+
///
1787+
/// This structure is used to enforce safety in the [`ArrayDataBuilder`]
1788+
///
1789+
/// [`ArrayDataBuilder`]: super::ArrayDataBuilder
1790+
///
1791+
/// # Example
1792+
/// ```rust
1793+
/// use arrow_data::UnsafeFlag;
1794+
/// assert!(!UnsafeFlag::default().get()); // default is false
1795+
/// let mut flag = UnsafeFlag::new();
1796+
/// assert!(!flag.get()); // defaults to false
1797+
/// // can only set it to true in unsafe code
1798+
/// unsafe { flag.set(true) };
1799+
/// assert!(flag.get()); // now true
1800+
/// ```
1801+
#[derive(Debug, Clone)]
1802+
pub struct UnsafeFlag(bool);
1803+
1804+
impl UnsafeFlag {
1805+
/// Creates a new `UnsafeFlag` with the value set to `false`.
1806+
///
1807+
/// See examples on [`Self::new`]
1808+
#[inline]
1809+
pub const fn new() -> Self {
1810+
Self(false)
1811+
}
1812+
1813+
/// Sets the value of the flag to the given value
17861814
///
1787-
/// Defaults to a value of false.
1815+
/// Note this can purposely only be done in `unsafe` code
17881816
///
1789-
/// This structure is used to enforce safety in the [`ArrayDataBuilder`]
1817+
/// # Safety
17901818
///
1791-
/// [`ArrayDataBuilder`]: super::ArrayDataBuilder
1792-
#[derive(Debug)]
1793-
pub struct UnsafeFlag(bool);
1794-
1795-
impl UnsafeFlag {
1796-
/// Creates a new `UnsafeFlag` with the value set to `false`
1797-
#[inline]
1798-
pub const fn new() -> Self {
1799-
Self(false)
1800-
}
1819+
/// If set, the flag will be set to the given value. There is nothing
1820+
/// immediately unsafe about doing so, however, the flag can be used to
1821+
/// subsequently bypass safety checks in the [`ArrayDataBuilder`].
1822+
#[inline]
1823+
pub unsafe fn set(&mut self, val: bool) {
1824+
self.0 = val;
1825+
}
18011826

1802-
#[inline]
1803-
pub unsafe fn set(&mut self, val: bool) {
1804-
self.0 = val;
1805-
}
1827+
/// Returns the value of the flag
1828+
#[inline]
1829+
pub fn get(&self) -> bool {
1830+
self.0
1831+
}
1832+
}
18061833

1807-
#[inline]
1808-
pub fn get(&self) -> bool {
1809-
self.0
1810-
}
1834+
// Manual impl to make it clear you can not construct unsafe with true
1835+
impl Default for UnsafeFlag {
1836+
fn default() -> Self {
1837+
Self::new()
18111838
}
18121839
}
18131840

0 commit comments

Comments
 (0)