algebra_sparse/traits.rs
1// Copyright (C) 2020-2025 algebra-sparse authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/// Trait for converting types to immutable views.
16///
17/// This trait provides a unified way to create efficient read-only views of various
18/// sparse matrix and vector types without allocation. Views are useful for:
19/// - Passing data to functions without transferring ownership
20/// - Creating temporary references for computation
21/// - Implementing generic algorithms that work with multiple matrix types
22///
23/// # Examples
24///
25/// ```rust
26/// use algebra_sparse::{CsrMatrix, traits::IntoView};
27/// use nalgebra::DMatrix;
28///
29/// let dense = DMatrix::from_row_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
30/// let csr = CsrMatrix::from_dense(dense.as_view());
31///
32/// // Create view for read-only operations
33/// let view = csr.as_view(); // or csr.into_view()
34/// println!("Shape: {:?}", view.shape());
35/// ```
36pub trait IntoView {
37 /// The view type produced by this trait.
38 type View;
39
40 /// Converts the type into an immutable view.
41 ///
42 /// # Returns
43 /// An immutable view of the data
44 fn into_view(self) -> Self::View;
45}