@@ -76,7 +76,7 @@ impl Blob {
76
76
/// # Panics
77
77
///
78
78
/// Panics if the returned OpenVINO size will not fit in `usize`.
79
- pub fn len ( & mut self ) -> Result < usize > {
79
+ pub fn len ( & self ) -> Result < usize > {
80
80
let mut size = 0 ;
81
81
try_unsafe ! ( ie_blob_size( self . instance, std:: ptr:: addr_of_mut!( size) ) ) ?;
82
82
Ok ( usize:: try_from ( size) . unwrap ( ) )
@@ -87,7 +87,7 @@ impl Blob {
87
87
/// # Panics
88
88
///
89
89
/// Panics if the returned OpenVINO size will not fit in `usize`.
90
- pub fn byte_len ( & mut self ) -> Result < usize > {
90
+ pub fn byte_len ( & self ) -> Result < usize > {
91
91
let mut size = 0 ;
92
92
try_unsafe ! ( ie_blob_byte_size(
93
93
self . instance,
@@ -97,7 +97,7 @@ impl Blob {
97
97
}
98
98
99
99
/// Retrieve the [`Blob`]'s data as an immutable slice of bytes.
100
- pub fn buffer ( & mut self ) -> Result < & [ u8 ] > {
100
+ pub fn buffer ( & self ) -> Result < & [ u8 ] > {
101
101
let mut buffer = Blob :: empty_buffer ( ) ;
102
102
try_unsafe ! ( ie_blob_get_buffer(
103
103
self . instance,
@@ -124,6 +124,28 @@ impl Blob {
124
124
Ok ( slice)
125
125
}
126
126
127
+ /// Retrieve the [`Blob`]'s data as an immutable slice of type `T`.
128
+ ///
129
+ /// # Safety
130
+ ///
131
+ /// This function is `unsafe`, since the values of `T` may not have been properly initialized;
132
+ /// however, this functionality is provided as an equivalent of what C/C++ users of OpenVINO
133
+ /// currently do to access [`Blob`]s with, e.g., floating point values:
134
+ /// `results.buffer_as_type::<f32>()`.
135
+ pub unsafe fn buffer_as_type < T > ( & self ) -> Result < & [ T ] > {
136
+ let mut buffer = Blob :: empty_buffer ( ) ;
137
+ InferenceError :: from ( ie_blob_get_buffer (
138
+ self . instance ,
139
+ std:: ptr:: addr_of_mut!( buffer) ,
140
+ ) ) ?;
141
+ // This is very unsafe, but very convenient: by allowing users to specify T, they can
142
+ // retrieve the buffer in whatever shape they prefer. But we must ensure that they cannot
143
+ // read too many bytes, so we manually calculate the resulting slice `size`.
144
+ let size = self . byte_len ( ) ? / std:: mem:: size_of :: < T > ( ) ;
145
+ let slice = std:: slice:: from_raw_parts ( buffer. __bindgen_anon_1 . buffer . cast :: < T > ( ) , size) ;
146
+ Ok ( slice)
147
+ }
148
+
127
149
/// Retrieve the [`Blob`]'s data as a mutable slice of type `T`.
128
150
///
129
151
/// # Safety
0 commit comments