diff --git a/src/impl_views/methods.rs b/src/impl_views/methods.rs new file mode 100644 index 000000000..a34247165 --- /dev/null +++ b/src/impl_views/methods.rs @@ -0,0 +1,34 @@ +// Copyright 2014-2016 bluss and ndarray developers. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use crate::imp_prelude::*; +use crate::dimension::IntoDimension; +use crate::dimension::broadcast::upcast; + +impl<'a, A, D> ArrayView<'a, A, D> +where + D: Dimension, +{ + /// Broadcasts an `ArrayView`. See [`ArrayBase::broadcast`]. + /// + /// This is a specialized version of [`ArrayBase::broadcast`] that transfers + /// the view's lifetime to the output. + pub fn broadcast_ref(&self, dim: E) -> Option> + where + E: IntoDimension, + { + let dim = dim.into_dimension(); + + // Note: zero strides are safe precisely because we return an read-only view + let broadcast_strides = match upcast(&dim, &self.dim, &self.strides) { + Some(st) => st, + None => return None, + }; + unsafe { Some(ArrayView::new(self.ptr, dim, broadcast_strides)) } + } +} diff --git a/src/impl_views/mod.rs b/src/impl_views/mod.rs index 487cc3cb2..fda58242a 100644 --- a/src/impl_views/mod.rs +++ b/src/impl_views/mod.rs @@ -1,6 +1,7 @@ mod constructors; mod conversions; mod indexing; +mod methods; mod splitting; pub use constructors::*;