Skip to content

Commit 6d07b68

Browse files
committed
Fix problems with f64 and DirEntry on Illumos
1 parent ebab240 commit 6d07b68

File tree

2 files changed

+37
-67
lines changed

2 files changed

+37
-67
lines changed

src/libstd/num/f64.rs

Lines changed: 28 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -511,28 +511,7 @@ impl f64 {
511511
#[stable(feature = "rust1", since = "1.0.0")]
512512
#[inline]
513513
pub fn ln(self) -> f64 {
514-
if !cfg!(target_os = "sunos") {
515-
unsafe { intrinsics::logf64(self) }
516-
} else {
517-
// Illumos requires a wrapper around log, log2, and log10 functions
518-
// because of their non-standard behavior (e.g. log(-n) returns -Inf instead
519-
// of expected NaN).
520-
if self.is_finite() {
521-
if self > 0.0 {
522-
unsafe { intrinsics::logf64(self) }
523-
} else if self == 0.0 {
524-
NEG_INFINITY // log(0) = -Inf
525-
} else {
526-
NAN // log(-n) = NaN
527-
}
528-
} else if self.is_nan() {
529-
self // log(NaN) = NaN
530-
} else if self > 0.0 {
531-
self // log(Inf) = Inf
532-
} else {
533-
NAN // log(-Inf) = NaN
534-
}
535-
}
514+
self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
536515
}
537516

538517
/// Returns the logarithm of the number with respect to an arbitrary base.
@@ -567,27 +546,7 @@ impl f64 {
567546
#[stable(feature = "rust1", since = "1.0.0")]
568547
#[inline]
569548
pub fn log2(self) -> f64 {
570-
if !cfg!(target_os = "sunos") {
571-
unsafe { intrinsics::log2f64(self) }
572-
} else {
573-
// Illumos requires a wrapper around the log2 function because of
574-
// its non-standard behavior
575-
if self.is_finite() {
576-
if self > 0.0 {
577-
unsafe { intrinsics::log2f64(self) }
578-
} else if self == 0.0 {
579-
NEG_INFINITY // log2(0) = -Inf
580-
} else {
581-
NAN // log2(-n) = NaN
582-
}
583-
} else if self.is_nan() {
584-
self // log2(NaN) = NaN
585-
} else if self > 0.0 {
586-
self // log2(Inf) = Inf
587-
} else {
588-
NAN // log2(-Inf) = NaN
589-
}
590-
}
549+
self.log_wrapper(|n| { unsafe { intrinsics::log2f64(n) } })
591550
}
592551

593552
/// Returns the base 10 logarithm of the number.
@@ -603,27 +562,7 @@ impl f64 {
603562
#[stable(feature = "rust1", since = "1.0.0")]
604563
#[inline]
605564
pub fn log10(self) -> f64 {
606-
if !cfg!(target_os = "sunos") {
607-
unsafe { intrinsics::log10f64(self) }
608-
} else {
609-
// Illumos requires a wrapper around the log10 function because of
610-
// its non-standard behavior.
611-
if self.is_finite() {
612-
if self > 0.0 {
613-
unsafe { intrinsics::log10f64(self) }
614-
} else if self == 0.0 {
615-
NEG_INFINITY // log10(0) = -Inf
616-
} else {
617-
NAN // log10(-n) = NaN
618-
}
619-
} else if self.is_nan() {
620-
self // log10(NaN) = NaN
621-
} else if self > 0.0 {
622-
self // log10(Inf) = Inf
623-
} else {
624-
NAN // log10(-Inf) = NaN
625-
}
626-
}
565+
self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
627566
}
628567

629568
/// Converts radians to degrees.
@@ -1126,6 +1065,31 @@ impl f64 {
11261065
pub fn atanh(self) -> f64 {
11271066
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
11281067
}
1068+
1069+
// Illumos requires a wrapper around log, log2, and log10 functions
1070+
// because of their non-standard behavior (e.g. log(-n) returns -Inf instead
1071+
// of expected NaN).
1072+
fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
1073+
if !cfg!(target_os = "sunos") {
1074+
log_fn(self)
1075+
} else {
1076+
if self.is_finite() {
1077+
if self > 0.0 {
1078+
log_fn(self)
1079+
} else if self == 0.0 {
1080+
NEG_INFINITY // log(0) = -Inf
1081+
} else {
1082+
NAN // log(-n) = NaN
1083+
}
1084+
} else if self.is_nan() {
1085+
self // log(NaN) = NaN
1086+
} else if self > 0.0 {
1087+
self // log(Inf) = Inf
1088+
} else {
1089+
NAN // log(-Inf) = NaN
1090+
}
1091+
}
1092+
}
11291093
}
11301094

11311095
#[cfg(test)]

src/libstd/sys/unix/fs.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use sys_common::{AsInner, FromInner};
2626
use vec::Vec;
2727
#[cfg(target_os = "sunos")]
2828
use core_collections::borrow::ToOwned;
29+
#[cfg(target_os = "sunos")]
30+
use boxed::Box;
2931

3032
pub struct File(FileDesc);
3133

@@ -52,7 +54,7 @@ pub struct DirEntry {
5254
// store the name, b) its lifetime between readdir calls
5355
// is not guaranteed.
5456
#[cfg(target_os = "sunos")]
55-
name: Arc<Vec<u8>>
57+
name: Box<[u8]>
5658
}
5759

5860
#[derive(Clone)]
@@ -143,6 +145,10 @@ impl Iterator for ReadDir {
143145
fn next(&mut self) -> Option<io::Result<DirEntry>> {
144146
unsafe {
145147
loop {
148+
// Although readdir_r(3) would be a correct function to use here because
149+
// of the thread safety, on Illumos the readdir(3C) function is safe to use
150+
// in threaded applications and it is generally preferred over the
151+
// readdir_r(3C) function.
146152
let entry_ptr = libc::readdir(self.dirp.0);
147153
if entry_ptr.is_null() {
148154
return None
@@ -153,8 +159,8 @@ impl Iterator for ReadDir {
153159

154160
let ret = DirEntry {
155161
entry: *entry_ptr,
156-
name: Arc::new(::slice::from_raw_parts(name as *const u8,
157-
namelen as usize).to_owned()),
162+
name: ::slice::from_raw_parts(name as *const u8,
163+
namelen as usize).to_owned().into_boxed_slice(),
158164
root: self.root.clone()
159165
};
160166
if ret.name_bytes() != b"." && ret.name_bytes() != b".." {

0 commit comments

Comments
 (0)