Skip to content

Commit 619f737

Browse files
committed
Add RawArrayView and RawArrayViewMut
1 parent 40723be commit 619f737

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

src/data_traits.rs

+30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::sync::Arc;
1414
use {
1515
ArrayBase,
1616
Dimension,
17+
RawViewRepr,
1718
ViewRepr,
1819
OwnedRepr,
1920
OwnedRcRepr,
@@ -126,6 +127,35 @@ pub unsafe trait DataClone : Data {
126127
}
127128
}
128129

130+
unsafe impl<A> DataRaw for RawViewRepr<*const A> {
131+
type Elem = A;
132+
fn _data_slice(&self) -> Option<&[A]> {
133+
None
134+
}
135+
private_impl!{}
136+
}
137+
138+
unsafe impl<A> DataRaw for RawViewRepr<*mut A> {
139+
type Elem = A;
140+
fn _data_slice(&self) -> Option<&[A]> {
141+
None
142+
}
143+
private_impl!{}
144+
}
145+
146+
unsafe impl<A> DataRawMut for RawViewRepr<*mut A> {
147+
#[inline]
148+
fn try_ensure_unique<D>(_: &mut ArrayBase<Self, D>)
149+
where Self: Sized,
150+
D: Dimension
151+
{}
152+
153+
#[inline]
154+
fn try_is_unique(&mut self) -> Option<bool> {
155+
None
156+
}
157+
}
158+
129159
unsafe impl<A> DataRaw for OwnedArcRepr<A> {
130160
type Elem = A;
131161
fn _data_slice(&self) -> Option<&[A]> {

src/lib.rs

+69
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ mod imp_prelude {
201201
DataMut,
202202
DataOwned,
203203
DataShared,
204+
RawViewRepr,
204205
ViewRepr,
205206
Ix, Ixs,
206207
};
@@ -1127,6 +1128,57 @@ pub type ArrayView<'a, A, D> = ArrayBase<ViewRepr<&'a A>, D>;
11271128
/// [ab]: struct.ArrayBase.html
11281129
pub type ArrayViewMut<'a, A, D> = ArrayBase<ViewRepr<&'a mut A>, D>;
11291130

1131+
/// A read-only array view without a lifetime.
1132+
///
1133+
/// This is similar to [`ArrayView`] but does not carry any lifetime or
1134+
/// ownership information, and its data cannot be read without an unsafe
1135+
/// conversion into an [`ArrayView`]. The relationship between `RawArrayView`
1136+
/// and [`ArrayView`] is somewhat analogous to the relationship between `*const
1137+
/// T` and `&T`, but `RawArrayView` has additional requirements that `*const T`
1138+
/// does not, such as alignment and non-nullness.
1139+
///
1140+
/// [`ArrayView`]: type.ArrayView.html
1141+
///
1142+
/// The `RawArrayView<A, D>` is parameterized by `A` for the element type and
1143+
/// `D` for the dimensionality.
1144+
///
1145+
/// Raw array views have all the methods of an array (see
1146+
/// [`ArrayBase`](struct.ArrayBase.html)).
1147+
///
1148+
/// See also [`RawArrayViewMut`](type.RawArrayViewMut.html).
1149+
///
1150+
/// # Warning
1151+
///
1152+
/// You can't use this type wih an arbitrary raw pointer; see
1153+
/// [`from_shape_ptr`](#method.from_shape_ptr) for details.
1154+
pub type RawArrayView<A, D> = ArrayBase<RawViewRepr<*const A>, D>;
1155+
1156+
/// A mutable array view without a lifetime.
1157+
///
1158+
/// This is similar to [`ArrayViewMut`] but does not carry any lifetime or
1159+
/// ownership information, and its data cannot be read/written without an
1160+
/// unsafe conversion into an [`ArrayViewMut`]. The relationship between
1161+
/// `RawArrayViewMut` and [`ArrayViewMut`] is somewhat analogous to the
1162+
/// relationship between `*mut T` and `&mut T`, but `RawArrayViewMut` has
1163+
/// additional requirements that `*mut T` does not, such as alignment and
1164+
/// non-nullness.
1165+
///
1166+
/// [`ArrayViewMut`]: type.ArrayViewMut.html
1167+
///
1168+
/// The `RawArrayViewMut<A, D>` is parameterized by `A` for the element type
1169+
/// and `D` for the dimensionality.
1170+
///
1171+
/// Raw array views have all the methods of an array (see
1172+
/// [`ArrayBase`](struct.ArrayBase.html)).
1173+
///
1174+
/// See also [`RawArrayView`](type.RawArrayView.html).
1175+
///
1176+
/// # Warning
1177+
///
1178+
/// You can't use this type wih an arbitrary raw pointer; see
1179+
/// [`from_shape_ptr`](#method.from_shape_ptr) for details.
1180+
pub type RawArrayViewMut<A, D> = ArrayBase<RawViewRepr<*mut A>, D>;
1181+
11301182
/// Array's representation.
11311183
///
11321184
/// *Don’t use this type directly—use the type alias
@@ -1154,6 +1206,23 @@ impl<A> Clone for OwnedArcRepr<A> {
11541206
}
11551207
}
11561208

1209+
/// Array pointer’s representation.
1210+
///
1211+
/// *Don’t use this type directly—use the type aliases
1212+
/// [`RawArrayView`](type.RawArrayView.html) /
1213+
/// [`RawArrayViewMut`](type.RawArrayViewMut.html) for the array type!*
1214+
#[derive(Copy, Clone)]
1215+
// This is just a marker type, to carry the mutability and element type.
1216+
pub struct RawViewRepr<A> {
1217+
ptr: PhantomData<A>,
1218+
}
1219+
1220+
impl<A> RawViewRepr<A> {
1221+
#[inline(always)]
1222+
fn new() -> Self {
1223+
RawViewRepr { ptr: PhantomData }
1224+
}
1225+
}
11571226

11581227
/// Array view’s representation.
11591228
///

src/prelude.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub use {
2525
RcArray,
2626
ArrayView,
2727
ArrayViewMut,
28+
RawArrayView,
29+
RawArrayViewMut,
2830
};
2931

3032
#[doc(no_inline)]

0 commit comments

Comments
 (0)