Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Cleaned docs for BinaryArray #1047

Merged
merged 1 commit into from
Jun 4, 2022
Merged
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
78 changes: 1 addition & 77 deletions src/array/binary/from.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,12 @@
use std::iter::FromIterator;

use crate::{array::Offset, trusted_len::TrustedLen};
use crate::array::Offset;

use super::{BinaryArray, MutableBinaryArray};

impl<O: Offset> BinaryArray<O> {
/// Creates a new [`BinaryArray`] from slices of `&[u8]`.
pub fn from_slice<T: AsRef<[u8]>, P: AsRef<[T]>>(slice: P) -> Self {
Self::from_trusted_len_values_iter(slice.as_ref().iter())
}

/// Creates a new [`BinaryArray`] from a slice of optional `&[u8]`.
// Note: this can't be `impl From` because Rust does not allow double `AsRef` on it.
pub fn from<T: AsRef<[u8]>, P: AsRef<[Option<T>]>>(slice: P) -> Self {
Self::from_trusted_len_iter(slice.as_ref().iter().map(|x| x.as_ref()))
}

/// Creates a [`BinaryArray`] from an iterator of trusted length.
#[inline]
pub fn from_trusted_len_values_iter<T: AsRef<[u8]>, I: TrustedLen<Item = T>>(
iterator: I,
) -> Self {
MutableBinaryArray::<O>::from_trusted_len_values_iter(iterator).into()
}

/// Creates a new [`BinaryArray`] from a [`Iterator`] of `&str`.
pub fn from_iter_values<T: AsRef<[u8]>, I: Iterator<Item = T>>(iterator: I) -> Self {
MutableBinaryArray::<O>::from_iter_values(iterator).into()
}
}

impl<O: Offset, P: AsRef<[u8]>> FromIterator<Option<P>> for BinaryArray<O> {
#[inline]
fn from_iter<I: IntoIterator<Item = Option<P>>>(iter: I) -> Self {
MutableBinaryArray::<O>::from_iter(iter).into()
}
}

impl<O: Offset> BinaryArray<O> {
/// Creates a [`BinaryArray`] from an iterator of trusted length.
/// # Safety
/// The iterator must be [`TrustedLen`](https://doc.rust-lang.org/std/iter/trait.TrustedLen.html).
/// I.e. that `size_hint().1` correctly reports its length.
#[inline]
pub unsafe fn from_trusted_len_iter_unchecked<I, P>(iterator: I) -> Self
where
P: AsRef<[u8]>,
I: Iterator<Item = Option<P>>,
{
MutableBinaryArray::<O>::from_trusted_len_iter_unchecked(iterator).into()
}

/// Creates a [`BinaryArray`] from an iterator of trusted length.
#[inline]
pub fn from_trusted_len_iter<I, P>(iterator: I) -> Self
where
P: AsRef<[u8]>,
I: TrustedLen<Item = Option<P>>,
{
// soundness: I is `TrustedLen`
unsafe { Self::from_trusted_len_iter_unchecked(iterator) }
}

/// Creates a [`BinaryArray`] from an falible iterator of trusted length.
/// # Safety
/// The iterator must be [`TrustedLen`](https://doc.rust-lang.org/std/iter/trait.TrustedLen.html).
/// I.e. that `size_hint().1` correctly reports its length.
#[inline]
pub unsafe fn try_from_trusted_len_iter_unchecked<E, I, P>(iterator: I) -> Result<Self, E>
where
P: AsRef<[u8]>,
I: IntoIterator<Item = Result<Option<P>, E>>,
{
MutableBinaryArray::<O>::try_from_trusted_len_iter_unchecked(iterator).map(|x| x.into())
}

/// Creates a [`BinaryArray`] from an fallible iterator of trusted length.
#[inline]
pub fn try_from_trusted_len_iter<E, I, P>(iter: I) -> Result<Self, E>
where
P: AsRef<[u8]>,
I: TrustedLen<Item = Result<Option<P>, E>>,
{
// soundness: I: TrustedLen
unsafe { Self::try_from_trusted_len_iter_unchecked(iter) }
}
}
21 changes: 1 addition & 20 deletions src/array/binary/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::{
array::Offset,
bitmap::utils::{zip_validity, ZipValidity},
trusted_len::TrustedLen,
};
use crate::{array::Offset, bitmap::utils::ZipValidity, trusted_len::TrustedLen};

use super::BinaryArray;

Expand Down Expand Up @@ -50,19 +46,4 @@ impl<'a, O: Offset> IntoIterator for &'a BinaryArray<O> {
}
}

impl<'a, O: Offset> BinaryArray<O> {
/// Returns an iterator of `Option<&[u8]>`
pub fn iter(&'a self) -> ZipValidity<'a, &'a [u8], BinaryValueIter<'a, O>> {
zip_validity(
BinaryValueIter::new(self),
self.validity.as_ref().map(|x| x.iter()),
)
}

/// Returns an iterator of `&[u8]`
pub fn values_iter(&'a self) -> BinaryValueIter<'a, O> {
BinaryValueIter::new(self)
}
}

unsafe impl<O: Offset> TrustedLen for BinaryValueIter<'_, O> {}
Loading