Skip to content
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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ matrix:
script:
- cargo build
- cargo build --features small
- rust: nightly
name: Miri
script:
- rustup component add miri || travis_terminate 0
- cargo miri test
Copy link

@RalfJung RalfJung Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see Miri being added here. :)

cargo miri test will interactively ask for confirmation to install things, and will probably not work. Miri is just not available for the latest nightly so right now we do not enter this code path.
The recommended Miri CI integration is given in our README.

28 changes: 2 additions & 26 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,12 @@ impl Buffer {
#[cfg_attr(feature = "no-panic", no_panic)]
pub fn format_finite<F: Float>(&mut self, f: F) -> &str {
unsafe {
let n = f.write_to_ryu_buffer(self.first_byte_pointer_mut());
let n = f.write_to_ryu_buffer(self.bytes.as_mut_ptr() as *mut u8);
debug_assert!(n <= self.bytes.len());
let slice = slice::from_raw_parts(self.first_byte_pointer(), n);
let slice = slice::from_raw_parts(self.bytes.as_ptr() as *const u8, n);
str::from_utf8_unchecked(slice)
}
}

#[inline]
#[cfg(maybe_uninit)]
fn first_byte_pointer(&self) -> *const u8 {
self.bytes[0].as_ptr()
}

#[inline]
#[cfg(not(maybe_uninit))]
fn first_byte_pointer(&self) -> *const u8 {
&self.bytes[0] as *const u8
}

#[inline]
#[cfg(maybe_uninit)]
fn first_byte_pointer_mut(&mut self) -> *mut u8 {
self.bytes[0].as_mut_ptr()
}

#[inline]
#[cfg(not(maybe_uninit))]
fn first_byte_pointer_mut(&mut self) -> *mut u8 {
&mut self.bytes[0] as *mut u8
}
}

impl Default for Buffer {
Expand Down
4 changes: 3 additions & 1 deletion tests/d2s_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ fn test_ryu() {

#[test]
fn test_random() {
let n = if cfg!(miri) { 100 } else { 1000000 };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I like this style... so far I always used #[cfg(miri)] for this but that means we need 4 lines to define one constant.

let mut buffer = ryu::Buffer::new();
for _ in 0..1000000 {
for _ in 0..n {
let f: f64 = rand::random();
assert_eq!(f, buffer.format_finite(f).parse().unwrap());
}
}

#[cfg(not(miri))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the usual alternative I use these days is #[cfg_attr(miri, ignore)]. Then at least it is clear in the output that we are skipping some tests.

It doesn't really make a difference, though.

#[test]
fn test_non_finite() {
for i in 0u64..1 << 23 {
Expand Down
4 changes: 3 additions & 1 deletion tests/f2s_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ fn test_ryu() {

#[test]
fn test_random() {
let n = if cfg!(miri) { 100 } else { 1000000 };
let mut buffer = ryu::Buffer::new();
for _ in 0..1000000 {
for _ in 0..n {
let f: f32 = rand::random();
assert_eq!(f, buffer.format_finite(f).parse().unwrap());
}
}

#[cfg(not(miri))]
#[test]
fn test_non_finite() {
for i in 0u32..1 << 23 {
Expand Down