Skip to content

Commit

Permalink
Implement Support for no_std
Browse files Browse the repository at this point in the history
sid doesn't actually need std. It can just use core + std.
  • Loading branch information
CryZe committed Sep 4, 2019
1 parent 6914af2 commit 0440d6e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ repository = "https://github.com/nical/sid"
name = "sid"

[dependencies]
num-traits = "0.2"
num-traits = { version = "0.2", default-features = false }
8 changes: 4 additions & 4 deletions src/id.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use {IdRange, IntegerHandle, Identifier, FromUsize, ToUsize};
use std::marker::PhantomData;
use std::fmt;
use std::ops::{Add, Sub};
use std::hash::{Hash, Hasher};
use core::marker::PhantomData;
use core::fmt;
use core::ops::{Add, Sub};
use core::hash::{Hash, Hasher};
use num_traits::One;

#[repr(C)]
Expand Down
7 changes: 4 additions & 3 deletions src/id_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{Identifier, FromUsize};
use std::marker::PhantomData;
use std::ops;
use core::marker::PhantomData;
use core::ops;
use alloc::vec::Vec;

/// A trait that defines how to choose the null (or invalid) Id.
pub trait NullId<ID> {
Expand Down Expand Up @@ -237,7 +238,7 @@ struct MagicValue;
#[cfg(test)]
impl NullId<TestId> for MagicValue {
fn null_id() -> TestId {
return FromUsize::from_usize(::std::u32::MAX as usize);
return FromUsize::from_usize(::core::u32::MAX as usize);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/id_range.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use {Id, IntegerHandle, FromUsize};
use std::marker::PhantomData;
use std::fmt;
use std::ops;
use std::cmp;
use core::marker::PhantomData;
use core::fmt;
use core::ops;
use core::cmp;
use num_traits::Zero;

pub struct IdRange<T, H = u32> {
Expand Down
12 changes: 6 additions & 6 deletions src/id_vector.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use {Identifier, FromUsize, ToUsize, IdRange};
use std::default::Default;
use std::slice;
use std::vec;
use std::marker::PhantomData;
use std::ops;
use std::iter::IntoIterator;
use core::default::Default;
use core::slice;
use alloc::{vec, vec::Vec};
use core::marker::PhantomData;
use core::ops;
use core::iter::IntoIterator;
use num_traits::Zero;

/// Similar to Vec except that it is indexed using an Id rather than an usize index.
Expand Down
30 changes: 16 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#![no_std]

extern crate alloc;
extern crate num_traits;

use std::cmp;
use std::ops::{Add, Sub};
use num_traits::{Zero, One};
use num_traits::{One, Zero};
use core::cmp;
use core::ops::{Add, Sub};

mod id;
mod id_list;
mod id_range;
mod id_vector;
mod id_list;

pub use id_vector::{IdSlice, MutIdSlice, IdVec};
pub use id_list::{IdFreeList, NullId, NoneAsNullId};
pub use id::Id;
pub use id_list::{IdFreeList, NoneAsNullId, NullId};
pub use id_range::{IdRange, ReverseIdRange};
pub use id_vector::{IdSlice, IdVec, MutIdSlice};

pub trait FromUsize {
fn from_usize(idx: usize) -> Self;
Expand All @@ -22,8 +25,8 @@ pub trait ToUsize {
fn to_usize(&self) -> usize;
}

pub trait IntegerHandle
: Copy
pub trait IntegerHandle:
Copy
+ Clone
+ Add<Output = Self>
+ Sub<Output = Self>
Expand All @@ -33,7 +36,8 @@ pub trait IntegerHandle
+ FromUsize
+ ToUsize
+ Zero
+ One {
+ One
{
}

pub trait Identifier: Copy + FromUsize + ToUsize + PartialEq {
Expand Down Expand Up @@ -212,7 +216,6 @@ impl IntegerHandle for i32 {}
impl IntegerHandle for i64 {}
impl IntegerHandle for isize {}


/*
// TODO: remove it or implement traits manually
Expand All @@ -226,8 +229,8 @@ pub struct GenId<T, H: Copy, G> {
pub gen: G,
}
impl<T, H: Copy + std::fmt::Display, G: std::fmt::Display> std::fmt::Debug for GenId<T, H, G> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl<T, H: Copy + core::fmt::Display, G: core::fmt::Display> core::fmt::Debug for GenId<T, H, G> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "GenId#{}({})", self.id.handle, self.gen)
}
}
Expand Down Expand Up @@ -272,7 +275,6 @@ impl<T, H: Copy, G: Generation> Generation for GenId<T, H, G> {
}
*/


#[test]
fn test_copy_id() {
#[derive(Debug)]
Expand All @@ -294,7 +296,7 @@ fn test_copy_id() {

#[test]
fn test_reverese_id_range() {
use std::iter::FromIterator;
use core::iter::FromIterator;
fn range(first: u16, count: u16) -> IdRange<u16, u16> {
IdRange::new(first..(first + count))
}
Expand Down

0 comments on commit 0440d6e

Please sign in to comment.