Skip to content

Commit

Permalink
minibytes: make from_static const fn
Browse files Browse the repository at this point in the history
Summary:
This allows constructing Bytes or Text statically. Unfortunately traits cannot
have const_fn for now: rust-lang/rust#71971
so we manually write out `from_static` for both types.

Differential Revision: D39041222

fbshipit-source-id: 9b2312cfd415c1276485a4c2b4a4216db9c02a64
  • Loading branch information
quark-zju authored and facebook-github-bot committed Sep 2, 2022
1 parent e427765 commit f15b0aa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 62 deletions.
50 changes: 0 additions & 50 deletions eden/scm/lib/configparser/Cargo.toml

This file was deleted.

22 changes: 10 additions & 12 deletions eden/scm/lib/minibytes/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct AbstractBytes<T: ?Sized> {
pub(crate) len: usize,

// Actual owner of the bytes. None for static buffers.
owner: Option<Arc<dyn AbstractOwner<T>>>,
pub(crate) owner: Option<Arc<dyn AbstractOwner<T>>>,
}

/// The actual storage owning the bytes.
Expand Down Expand Up @@ -126,17 +126,6 @@ where
}
}

/// Creates `Bytes` from a static slice.
#[inline]
pub fn from_static(value: &'static T) -> Self {
let slice: &[u8] = value.as_bytes();
Self {
ptr: slice.as_ptr(),
len: slice.len(),
owner: None,
}
}

/// Creates `Bytes` from a [`BytesOwner`] (for example, `Vec<u8>`).
pub fn from_owner(value: impl AbstractOwner<T>) -> Self {
let slice: &T = value.as_ref();
Expand Down Expand Up @@ -182,6 +171,15 @@ impl Bytes {
self.as_bytes()
}

/// Creates `Bytes` from a static slice.
pub const fn from_static(slice: &'static [u8]) -> Self {
Self {
ptr: slice.as_ptr(),
len: slice.len(),
owner: None,
}
}

/// Convert to `Vec<u8>`, in a zero-copy way if possible.
pub fn into_vec(mut self) -> Vec<u8> {
let len = self.len();
Expand Down
9 changes: 9 additions & 0 deletions eden/scm/lib/minibytes/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ impl<T: TextOwner> AbstractOwner<str> for T {
}

impl Text {
/// Creates `Text` from a static str.
pub const fn from_static(slice: &'static str) -> Self {
Self {
ptr: slice.as_ptr(),
len: slice.len(),
owner: None,
}
}

#[inline]
pub(crate) fn as_slice(&self) -> &str {
let bytes = self.as_bytes();
Expand Down

0 comments on commit f15b0aa

Please sign in to comment.