Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add miri to CI and rework Label internals #204

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/sval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,24 @@ jobs:

- name: Powerset
run: cargo hack check --each-feature --exclude-features std,alloc -Z avoid-dev-deps --target thumbv6m-none-eabi

miri:
name: Test (Miri)
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab

- name: Install Miri
run: |
rustup toolchain install nightly --component miri
cargo +nightly miri setup

- name: Default features
run: cargo +nightly miri test --lib

- name: No features
run: cargo +nightly miri test --lib --no-default-features

- name: All features
run: cargo +nightly miri test --all-features --lib
9 changes: 6 additions & 3 deletions flatten/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,18 @@ impl<'sval> LabelBuf<'sval> {
LabelBuf::Text(text) => f(&Label::new_computed(text.as_str())),
LabelBuf::I128(v) => {
let mut buf = itoa::Buffer::new();
f(&Label::new_computed(buf.format(*v)))
let r= f(&Label::new_computed(buf.format(*v)));
r
}
LabelBuf::U128(v) => {
let mut buf = itoa::Buffer::new();
f(&Label::new_computed(buf.format(*v)))
let r = f(&Label::new_computed(buf.format(*v)));
r
}
LabelBuf::F64(v) => {
let mut buf = ryu::Buffer::new();
f(&Label::new_computed(buf.format(*v)))
let r =f(&Label::new_computed(buf.format(*v)));
r
}
}
}
Expand Down
30 changes: 23 additions & 7 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,24 @@ pub struct Label<'computed> {
// Only one `backing_field_*` may be `Some`
backing_field_static: Option<&'static str>,
#[cfg(feature = "alloc")]
backing_field_owned: Option<Box<str>>,
// Owned is a `Box<str>`
backing_field_owned: Option<*mut str>,
tag: Option<Tag>,
_marker: PhantomData<&'computed str>,
}

impl<'computed> Drop for Label<'computed> {
fn drop(&mut self) {
#[cfg(feature = "alloc")]
{
if let Some(owned) = self.backing_field_owned {
// SAFETY: We're dropping the value
drop(unsafe { Box::from_raw(owned) });
}
}
}
}

// SAFETY: Label doesn't mutate or synchronize: it acts just like a `&str`
unsafe impl<'computed> Send for Label<'computed> {}
// SAFETY: Label doesn't mutate or synchronize: it acts just like a `&str`
Expand Down Expand Up @@ -555,8 +568,9 @@ mod alloc_support {

impl<'computed> Clone for Label<'computed> {
fn clone(&self) -> Self {
if let Some(ref owned) = self.backing_field_owned {
Label::new_owned((**owned).to_owned())
if let Some(owned) = self.backing_field_owned {
// SAFETY: `owned` lives as long as `Label`
Label::new_owned(unsafe { &*owned }.to_owned())
} else {
Label {
value_computed: self.value_computed,
Expand Down Expand Up @@ -589,10 +603,12 @@ mod alloc_support {
Create a new label from an owned string value.
*/
pub fn new_owned(label: String) -> Self {
let owned = Box::into_raw(label.into_boxed_str());

Label {
value_computed: label.as_str() as *const str,
value_computed: owned as *const str,
backing_field_static: None,
backing_field_owned: Some(label.into_boxed_str()),
backing_field_owned: Some(owned),
tag: None,
_marker: PhantomData,
}
Expand Down Expand Up @@ -754,8 +770,8 @@ mod tests {
let b = a.clone();

assert_ne!(
a.backing_field_owned.as_ref().unwrap().as_ptr(),
b.backing_field_owned.as_ref().unwrap().as_ptr()
a.backing_field_owned.as_ref().unwrap(),
b.backing_field_owned.as_ref().unwrap(),
);
}
}
Expand Down
Loading