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

Convert std:: to core::, add alloc to lib.rs for no_std, add #2

Closed
wants to merge 6 commits into from
Closed
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
33 changes: 32 additions & 1 deletion no_std_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
#![feature(lang_items, start)]
#![feature(lang_items, start, alloc_error_handler)]
#![no_std]

extern crate libc;
extern crate alloc;

extern crate palette;

use core::panic::PanicInfo;

//from https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html
use core::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut;

struct MyAllocator;

unsafe impl GlobalAlloc for MyAllocator {
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { null_mut() }
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}

#[global_allocator]
static A: MyAllocator = MyAllocator;

//from https://github.com/rust-lang/rust/issues/51540
#[alloc_error_handler]
fn handle_alloc_error(_layout: Layout) -> ! {
// example implementation based on libc
extern "C" { fn abort() -> !; }
unsafe { abort() }
}

#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
let _magenta = palette::Srgb::new(255u8, 0, 255);
let mut v = alloc::vec::Vec::with_capacity(2);
v.push(palette::LinSrgb::new(1.0, 0.1, 0.1));
v.push(palette::LinSrgb::new(0.1, 1.0, 1.0));
let _grad = palette::Gradient::new(v);

0
}

#[lang = "eh_personality"]
extern fn eh_personality() {}

#[lang = "eh_unwind_resume"]
#[no_mangle]
pub extern fn rust_eh_unwind_resume() {}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
Expand Down
19 changes: 10 additions & 9 deletions palette/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

use num_traits::{One, Zero};
use float::Float;
use std::cmp::max;
use core::cmp::max;
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use std_alloc::Vec;

use cast;

Expand Down Expand Up @@ -307,35 +308,35 @@ impl<T: Float> Range<T> {
}
}

impl<T: Float> From<::std::ops::Range<T>> for Range<T> {
fn from(range: ::std::ops::Range<T>) -> Range<T> {
impl<T: Float> From<::core::ops::Range<T>> for Range<T> {
fn from(range: ::core::ops::Range<T>) -> Range<T> {
Range {
from: Some(range.start),
to: Some(range.end),
}
}
}

impl<T: Float> From<::std::ops::RangeFrom<T>> for Range<T> {
fn from(range: ::std::ops::RangeFrom<T>) -> Range<T> {
impl<T: Float> From<::core::ops::RangeFrom<T>> for Range<T> {
fn from(range: ::core::ops::RangeFrom<T>) -> Range<T> {
Range {
from: Some(range.start),
to: None,
}
}
}

impl<T: Float> From<::std::ops::RangeTo<T>> for Range<T> {
fn from(range: ::std::ops::RangeTo<T>) -> Range<T> {
impl<T: Float> From<::core::ops::RangeTo<T>> for Range<T> {
fn from(range: ::core::ops::RangeTo<T>) -> Range<T> {
Range {
from: None,
to: Some(range.end),
}
}
}

impl<T: Float> From<::std::ops::RangeFull> for Range<T> {
fn from(_range: ::std::ops::RangeFull) -> Range<T> {
impl<T: Float> From<::core::ops::RangeFull> for Range<T> {
fn from(_range: ::core::ops::RangeFull) -> Range<T> {
Range {
from: None,
to: None,
Expand Down
15 changes: 14 additions & 1 deletion palette/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,20 @@ pub use palette_derive::*;

pub use alpha::Alpha;
pub use blend::Blend;

#[cfg(feature = "std")]
mod std_alloc {
pub use std::vec::Vec;
}

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
mod std_alloc {
pub use alloc::vec::Vec;
}

pub use gradient::Gradient;

pub use hsl::{Hsl, Hsla};
Expand Down Expand Up @@ -346,7 +359,7 @@ macro_rules! assert_ranges {
mod macros;

pub mod blend;
#[cfg(feature = "std")]

pub mod gradient;

#[cfg(feature = "named")]
Expand Down