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

Remove uses of unsafe. #142

Merged
merged 4 commits into from
Apr 4, 2020
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The MSRV (Minimum Supported Rust Version) is 1.22.0, and typenum is tested again
version. Much of typenum should work on as low a version as 1.20.0, but that is not guaranteed.

### Unreleased
- [changed] Added `Copy` bound to `Rhs` of `Mul<Rhs>` impl for `<TArr<V, A>`.
- [changed] Added `Copy` bound to `Rhs` of `Div<Rhs>` impl for `<TArr<V, A>`.
- [changed] Added `Copy` bound to `Rhs` of `PartialDiv<Rhs>` impl for `<TArr<V, A>`.
- [changed] Added `Copy` bound to `Rhs` of `Rem<Rhs>` impl for `<TArr<V, A>`.
- [fixed] Make all functions #[inline].

### 1.11.2 (2019-08-26)
Expand Down
18 changes: 9 additions & 9 deletions build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ pub fn gen_uint(u: u64) -> UIntCode {
}

pub fn gen_int(i: i64) -> IntCode {
if i > 0 {
IntCode::Pos(Box::new(gen_uint(i as u64)))
} else if i < 0 {
IntCode::Neg(Box::new(gen_uint(i.abs() as u64)))
} else {
IntCode::Zero
use std::cmp::Ordering::{Equal, Greater, Less};

match i.cmp(&0) {
Greater => IntCode::Pos(Box::new(gen_uint(i as u64))),
Less => IntCode::Neg(Box::new(gen_uint(i.abs() as u64))),
Equal => IntCode::Zero,
}
}

Expand Down Expand Up @@ -166,12 +166,12 @@ pub mod consts {{
.unwrap();

for u in uints {
write!(f, " pub type U{} = {};\n", u, gen_uint(u)).unwrap();
writeln!(f, " pub type U{} = {};", u, gen_uint(u)).unwrap();
if u <= ::std::i64::MAX as u64 && u != 0 {
let i = u as i64;
write!(
writeln!(
f,
" pub type P{i} = PInt<U{i}>; pub type N{i} = NInt<U{i}>;\n",
" pub type P{i} = PInt<U{i}>; pub type N{i} = NInt<U{i}>;",
i = i
)
.unwrap();
Expand Down
78 changes: 56 additions & 22 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//!
//! It is not very featureful right now, and should be considered a work in progress.

use core::marker::PhantomData;
use core::ops::{Add, Div, Mul, Sub};

use super::*;
Expand All @@ -21,7 +20,8 @@ impl TypeArray for ATerm {}
/// may find it lacking functionality.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug)]
pub struct TArr<V, A> {
_marker: PhantomData<(V, A)>,
first: V,
rest: A,
}
paholg marked this conversation as resolved.
Show resolved Hide resolved

impl<V, A> TypeArray for TArr<V, A> {}
Expand Down Expand Up @@ -69,7 +69,7 @@ where
type Output = Add1<Length<A>>;
#[inline]
fn len(&self) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
self.rest.len() + B1
}
}

Expand All @@ -92,8 +92,11 @@ where
{
type Output = TArr<Sum<Vl, Vr>, Sum<Al, Ar>>;
#[inline]
fn add(self, _: TArr<Vr, Ar>) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn add(self, rhs: TArr<Vr, Ar>) -> Self::Output {
TArr {
first: self.first + rhs.first,
rest: self.rest + rhs.rest,
}
}
}

Expand All @@ -116,8 +119,11 @@ where
{
type Output = TArr<Diff<Vl, Vr>, Diff<Al, Ar>>;
#[inline]
fn sub(self, _: TArr<Vr, Ar>) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn sub(self, rhs: TArr<Vr, Ar>) -> Self::Output {
TArr {
first: self.first - rhs.first,
rest: self.rest - rhs.rest,
}
}
}

Expand All @@ -136,11 +142,15 @@ impl<V, A, Rhs> Mul<Rhs> for TArr<V, A>
where
V: Mul<Rhs>,
A: Mul<Rhs>,
Rhs: Copy,
{
type Output = TArr<Prod<V, Rhs>, Prod<A, Rhs>>;
#[inline]
fn mul(self, _: Rhs) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn mul(self, rhs: Rhs) -> Self::Output {
TArr {
first: self.first * rhs,
rest: self.rest * rhs,
}
}
}
paholg marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -180,8 +190,11 @@ where
{
type Output = TArr<Z0, Prod<Z0, A>>;
#[inline]
fn mul(self, _: TArr<V, A>) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn mul(self, rhs: TArr<V, A>) -> Self::Output {
TArr {
first: Z0,
rest: self * rhs.rest,
}
}
}

Expand All @@ -192,8 +205,11 @@ where
{
type Output = TArr<Prod<PInt<U>, V>, Prod<PInt<U>, A>>;
#[inline]
fn mul(self, _: TArr<V, A>) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn mul(self, rhs: TArr<V, A>) -> Self::Output {
TArr {
first: self * rhs.first,
rest: self * rhs.rest,
}
}
}

Expand All @@ -204,8 +220,11 @@ where
{
type Output = TArr<Prod<NInt<U>, V>, Prod<NInt<U>, A>>;
#[inline]
fn mul(self, _: TArr<V, A>) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn mul(self, rhs: TArr<V, A>) -> Self::Output {
TArr {
first: self * rhs.first,
rest: self * rhs.rest,
}
}
}

Expand All @@ -224,11 +243,15 @@ impl<V, A, Rhs> Div<Rhs> for TArr<V, A>
where
V: Div<Rhs>,
A: Div<Rhs>,
Rhs: Copy,
{
type Output = TArr<Quot<V, Rhs>, Quot<A, Rhs>>;
#[inline]
fn div(self, _: Rhs) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn div(self, rhs: Rhs) -> Self::Output {
TArr {
first: self.first / rhs,
rest: self.rest / rhs,
}
}
}

Expand All @@ -247,11 +270,15 @@ impl<V, A, Rhs> PartialDiv<Rhs> for TArr<V, A>
where
V: PartialDiv<Rhs>,
A: PartialDiv<Rhs>,
Rhs: Copy,
{
type Output = TArr<PartialQuot<V, Rhs>, PartialQuot<A, Rhs>>;
#[inline]
fn partial_div(self, _: Rhs) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn partial_div(self, rhs: Rhs) -> Self::Output {
TArr {
first: self.first.partial_div(rhs),
rest: self.rest.partial_div(rhs),
}
}
}

Expand All @@ -271,11 +298,15 @@ impl<V, A, Rhs> Rem<Rhs> for TArr<V, A>
where
V: Rem<Rhs>,
A: Rem<Rhs>,
Rhs: Copy,
{
type Output = TArr<Mod<V, Rhs>, Mod<A, Rhs>>;
#[inline]
fn rem(self, _: Rhs) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
fn rem(self, rhs: Rhs) -> Self::Output {
TArr {
first: self.first % rhs,
rest: self.rest % rhs,
}
}
}

Expand All @@ -299,6 +330,9 @@ where
type Output = TArr<Negate<V>, Negate<A>>;
#[inline]
fn neg(self) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
TArr {
first: -self.first,
rest: -self.rest,
}
}
}
17 changes: 17 additions & 0 deletions src/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//!

use core::ops::{BitAnd, BitOr, BitXor, Not};
use private::InternalMarker;
use {Cmp, Equal, Greater, Less, NonZero, PowerOfTwo};

pub use marker_traits::Bit;
Expand Down Expand Up @@ -213,18 +214,34 @@ mod tests {

impl Cmp<B0> for B0 {
type Output = Equal;

fn compare<P: InternalMarker>(&self, _: &B0) -> Self::Output {
Equal
}
}

impl Cmp<B1> for B0 {
type Output = Less;

fn compare<P: InternalMarker>(&self, _: &B1) -> Self::Output {
Less
}
}

impl Cmp<B0> for B1 {
type Output = Greater;

fn compare<P: InternalMarker>(&self, _: &B0) -> Self::Output {
Greater
}
}

impl Cmp<B1> for B1 {
type Output = Equal;

fn compare<P: InternalMarker>(&self, _: &B1) -> Self::Output {
Equal
}
}

use Min;
Expand Down
Loading