Skip to content

Commit a7ccd9a

Browse files
committed
Add RawArrayView and RawArrayViewMut
1 parent 497fd4d commit a7ccd9a

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

src/data_traits.rs

Lines changed: 30 additions & 0 deletions
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

Lines changed: 69 additions & 0 deletions
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
};
@@ -1119,6 +1120,57 @@ pub type ArrayView<'a, A, D> = ArrayBase<ViewRepr<&'a A>, D>;
11191120
/// [ab]: struct.ArrayBase.html
11201121
pub type ArrayViewMut<'a, A, D> = ArrayBase<ViewRepr<&'a mut A>, D>;
11211122

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

1201+
/// Array pointer’s representation.
1202+
///
1203+
/// *Don’t use this type directly—use the type aliases
1204+
/// [`RawArrayView`](type.RawArrayView.html) /
1205+
/// [`RawArrayViewMut`](type.RawArrayViewMut.html) for the array type!*
1206+
#[derive(Copy, Clone)]
1207+
// This is just a marker type, to carry the mutability and element type.
1208+
pub struct RawViewRepr<A> {
1209+
ptr: PhantomData<A>,
1210+
}
1211+
1212+
impl<A> RawViewRepr<A> {
1213+
#[inline(always)]
1214+
fn new() -> Self {
1215+
RawViewRepr { ptr: PhantomData }
1216+
}
1217+
}
11491218

11501219
/// Array view’s representation.
11511220
///

src/prelude.rs

Lines changed: 2 additions & 0 deletions
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)