Return an empty slice on safe slice casting error#462
Conversation
The cargo `byte-slice-cast` crate supports safe `u8 -> f32` slice casting through the `as_slice_of` trait function. However, for dynamic linked crate from rust toolchain (`byte-slice-cast-0.2.0`) in case if source `u8` slice is empty, the `WrongAlignment` error will be returned, so need to handle this edge case properly and return the empty `f32` slice on error. Please notice that the newest version (`byte-slice-cast-1.2.3`) is handling this case without alignment error (return an empty slice). https://docs.rs/byte-slice-cast/0.2.0/src/byte_slice_cast/lib.rs.html#206 https://docs.rs/byte-slice-cast/1.2.3/src/byte_slice_cast/lib.rs.html#263 Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
|
There are observable |
|
Why not update from byte-slice-cast 0.2 to 1? |
|
The So I decided don't upgrade crate and make it statically linked without significant reason. |
Right, but updating seems like a good idea regardless :) There were a couple of bugs fixed after the last couple of years between these two versions. |
| impl AsRef<[f32]> for GStreamerAudioChunk { | ||
| fn as_ref(&self) -> &[f32] { | ||
| self.0.as_ref().as_slice_of::<f32>().unwrap() | ||
| self.0.as_ref().as_slice_of::<f32>().unwrap_or_default() |
There was a problem hiding this comment.
I'm not sure this is the right way of handling this. You're going to silently ignore bugs here (it should always be aligned correctly, etc) which are going to be very annoying to debug if you just make this an empty slice.
Also note that backends/gstreamer/audio_decoder.rs has basically the same code but still unwrap()s, so at the very least this is inconsistent.
There was a problem hiding this comment.
And backends/gstreamer/audio_stream_reader.rs converts it into an Err but as above, this should never really happen and if it happens then there is a bug elsewhere that needs to be fixed.
At the very least these cases should be logged.
|
byte-slice-cast update is in #469 |
The cargo
byte-slice-castcrate supports safeu8 -> f32slice casting through theas_slice_oftrait function. However, for dynamic linked crate from rust toolchain (byte-slice-cast-0.2.0) in case if sourceu8slice is empty, theWrongAlignmenterror will be returned, so need to handle this edge case properly and return the emptyf32slice on error.Please notice that the newest version (
byte-slice-cast-1.2.3) is handling this case without alignment error (return an empty slice).https://docs.rs/byte-slice-cast/0.2.0/src/byte_slice_cast/lib.rs.html#206
https://docs.rs/byte-slice-cast/1.2.3/src/byte_slice_cast/lib.rs.html#263