Skip to content

Commit

Permalink
No panic
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Jan 20, 2022
1 parent 0fc2b97 commit 352d246
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions provider/core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,14 @@ impl ResourceKey {
/// Useful for reading and writing data to a file system.
#[inline]
pub fn get_path(&self) -> &str {
// Unfortunately string slices are not const, and there doesn't seem to be any work to
// make them (as of 2021-01).
// However, once `const_ptr_offset` and `const_slice_from_raw_parts` are stable, we can
// use this const code:
// unsafe {
// // Safe due to invariant that self.path is tagged correctly
// core::str::from_utf8_unchecked(core::slice::from_raw_parts(
// self.path.as_ptr().add(leading_tag!().len()),
// self.path.len() - trailing_tag!().len() - leading_tag!().len(),
// ))
// }
&self.path[leading_tag!().len()..self.path.len() - trailing_tag!().len()]
// This becomes const with `const_ptr_offset` and `const_slice_from_raw_parts`.
unsafe {
// Safe due to invariant that self.path is tagged correctly
core::str::from_utf8_unchecked(core::slice::from_raw_parts(
self.path.as_ptr().add(leading_tag!().len()),
self.path.len() - trailing_tag!().len() - leading_tag!().len(),
))
}
}

/// Gets a machine-readable representation of a [`ResourceKey`].
Expand Down

0 comments on commit 352d246

Please sign in to comment.