Skip to content

Commit f15b0aa

Browse files
quark-zjufacebook-github-bot
authored andcommitted
minibytes: make from_static const fn
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
1 parent e427765 commit f15b0aa

File tree

3 files changed

+19
-62
lines changed

3 files changed

+19
-62
lines changed

eden/scm/lib/configparser/Cargo.toml

Lines changed: 0 additions & 50 deletions
This file was deleted.

eden/scm/lib/minibytes/src/bytes.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct AbstractBytes<T: ?Sized> {
1919
pub(crate) len: usize,
2020

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

2525
/// The actual storage owning the bytes.
@@ -126,17 +126,6 @@ where
126126
}
127127
}
128128

129-
/// Creates `Bytes` from a static slice.
130-
#[inline]
131-
pub fn from_static(value: &'static T) -> Self {
132-
let slice: &[u8] = value.as_bytes();
133-
Self {
134-
ptr: slice.as_ptr(),
135-
len: slice.len(),
136-
owner: None,
137-
}
138-
}
139-
140129
/// Creates `Bytes` from a [`BytesOwner`] (for example, `Vec<u8>`).
141130
pub fn from_owner(value: impl AbstractOwner<T>) -> Self {
142131
let slice: &T = value.as_ref();
@@ -182,6 +171,15 @@ impl Bytes {
182171
self.as_bytes()
183172
}
184173

174+
/// Creates `Bytes` from a static slice.
175+
pub const fn from_static(slice: &'static [u8]) -> Self {
176+
Self {
177+
ptr: slice.as_ptr(),
178+
len: slice.len(),
179+
owner: None,
180+
}
181+
}
182+
185183
/// Convert to `Vec<u8>`, in a zero-copy way if possible.
186184
pub fn into_vec(mut self) -> Vec<u8> {
187185
let len = self.len();

eden/scm/lib/minibytes/src/text.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ impl<T: TextOwner> AbstractOwner<str> for T {
2121
}
2222

2323
impl Text {
24+
/// Creates `Text` from a static str.
25+
pub const fn from_static(slice: &'static str) -> Self {
26+
Self {
27+
ptr: slice.as_ptr(),
28+
len: slice.len(),
29+
owner: None,
30+
}
31+
}
32+
2433
#[inline]
2534
pub(crate) fn as_slice(&self) -> &str {
2635
let bytes = self.as_bytes();

0 commit comments

Comments
 (0)