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 1 commit
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>`.

### 1.11.2 (2019-08-26)
- [fixed] Cross compilation from Linux to Windows.
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 @@ -67,7 +67,7 @@ where
{
type Output = Add1<Length<A>>;
fn len(&self) -> Self::Output {
unsafe { ::core::mem::uninitialized() }
self.rest.len() + B1
}
}

Expand All @@ -88,8 +88,11 @@ where
Vl: Add<Vr>,
{
type Output = TArr<Sum<Vl, Vr>, Sum<Al, Ar>>;
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 @@ -110,8 +113,11 @@ where
Al: Sub<Ar>,
{
type Output = TArr<Diff<Vl, Vr>, Diff<Al, Ar>>;
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 @@ -129,10 +135,14 @@ 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>>;
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 @@ -168,8 +178,11 @@ where
Z0: Mul<A>,
{
type Output = TArr<Z0, Prod<Z0, A>>;
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 @@ -179,8 +192,11 @@ where
PInt<U>: Mul<A> + Mul<V>,
{
type Output = TArr<Prod<PInt<U>, V>, Prod<PInt<U>, A>>;
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 @@ -190,8 +206,11 @@ where
NInt<U>: Mul<A> + Mul<V>,
{
type Output = TArr<Prod<NInt<U>, V>, Prod<NInt<U>, A>>;
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 @@ -209,10 +228,14 @@ 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>>;
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 @@ -230,10 +253,14 @@ 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>>;
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 @@ -252,10 +279,14 @@ 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>>;
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 @@ -277,6 +308,9 @@ where
{
type Output = TArr<Negate<V>, Negate<A>>;
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 @@ -12,6 +12,7 @@

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

pub use marker_traits::Bit;

Expand Down Expand Up @@ -201,18 +202,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