@@ -8,9 +8,6 @@ use crate::archetypes::EncodedImage;
8
8
9
9
use super :: { TensorBuffer , TensorData , TensorDimension } ;
10
10
11
- // Much of the following duplicates code from: `crates/re_components/src/tensor.rs`, which
12
- // will eventually go away as the Tensor migration is completed.
13
-
14
11
// ----------------------------------------------------------------------------
15
12
16
13
impl TensorData {
@@ -169,7 +166,6 @@ macro_rules! tensor_from_ndarray {
169
166
type Error = TensorCastError ;
170
167
171
168
fn try_from( value: ndarray:: Array <$type, D >) -> Result <Self , Self :: Error > {
172
- let value = value. as_standard_layout( ) ;
173
169
let shape = value
174
170
. shape( )
175
171
. iter( )
@@ -178,15 +174,27 @@ macro_rules! tensor_from_ndarray {
178
174
name: None ,
179
175
} )
180
176
. collect( ) ;
181
- value
182
- . is_standard_layout( )
183
- . then( || TensorData {
184
- shape,
185
- buffer: TensorBuffer :: $variant(
186
- value. to_owned( ) . into_raw_vec_and_offset( ) . 0 . into( ) ,
187
- ) ,
188
- } )
189
- . ok_or( TensorCastError :: NotContiguousStdOrder )
177
+
178
+ let vec = if value. is_standard_layout( ) {
179
+ let ( mut vec, offset) = value. into_raw_vec_and_offset( ) ;
180
+ // into_raw_vec_and_offset() guarantees that the logical element order (.iter()) matches the internal
181
+ // storage order in the returned vector. Therefore, since it's in standard layout, it's contiguous in
182
+ // memory and safe to assume all our data is stored starting at the offset returned
183
+ if let Some ( offset) = offset {
184
+ vec. drain( ..offset) ;
185
+ vec
186
+ } else {
187
+ debug_assert!( vec. is_empty( ) ) ;
188
+ vec
189
+ }
190
+ } else {
191
+ value. into_iter( ) . collect:: <Vec <_>>( )
192
+ } ;
193
+
194
+ Ok ( Self {
195
+ shape,
196
+ buffer: TensorBuffer :: $variant( vec. into( ) ) ,
197
+ } )
190
198
}
191
199
}
192
200
@@ -313,13 +321,19 @@ impl<D: ::ndarray::Dimension> TryFrom<::ndarray::Array<half::f16, D>> for Tensor
313
321
} )
314
322
. collect ( ) ;
315
323
if value. is_standard_layout ( ) {
324
+ let ( vec, offset) = value. into_raw_vec_and_offset ( ) ;
325
+ // into_raw_vec_and_offset() guarantees that the logical element order (.iter()) matches the internal
326
+ // storage order in the returned vector. Therefore, since it's in standard layout, it's contiguous in
327
+ // memory and it's safe to assume all our data is stored starting at the offset returned
328
+ let vec_slice = if let Some ( offset) = offset {
329
+ & vec[ offset..]
330
+ } else {
331
+ debug_assert ! ( vec. is_empty( ) ) ;
332
+ & vec
333
+ } ;
316
334
Ok ( Self {
317
335
shape,
318
- buffer : TensorBuffer :: F16 (
319
- bytemuck:: cast_slice ( value. into_raw_vec_and_offset ( ) . 0 . as_slice ( ) )
320
- . to_vec ( )
321
- . into ( ) ,
322
- ) ,
336
+ buffer : TensorBuffer :: F16 ( Vec :: from ( bytemuck:: cast_slice ( vec_slice) ) . into ( ) ) ,
323
337
} )
324
338
} else {
325
339
Ok ( Self {
0 commit comments