diff --git a/no_std_test/src/main.rs b/no_std_test/src/main.rs index eeab90ce6..5081185d2 100644 --- a/no_std_test/src/main.rs +++ b/no_std_test/src/main.rs @@ -1,15 +1,42 @@ -#![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 } @@ -17,6 +44,10 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize { #[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 {} diff --git a/palette/src/gradient.rs b/palette/src/gradient.rs index 76c57dd98..dee6e1e07 100644 --- a/palette/src/gradient.rs +++ b/palette/src/gradient.rs @@ -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; @@ -307,8 +308,8 @@ impl Range { } } -impl From<::std::ops::Range> for Range { - fn from(range: ::std::ops::Range) -> Range { +impl From<::core::ops::Range> for Range { + fn from(range: ::core::ops::Range) -> Range { Range { from: Some(range.start), to: Some(range.end), @@ -316,8 +317,8 @@ impl From<::std::ops::Range> for Range { } } -impl From<::std::ops::RangeFrom> for Range { - fn from(range: ::std::ops::RangeFrom) -> Range { +impl From<::core::ops::RangeFrom> for Range { + fn from(range: ::core::ops::RangeFrom) -> Range { Range { from: Some(range.start), to: None, @@ -325,8 +326,8 @@ impl From<::std::ops::RangeFrom> for Range { } } -impl From<::std::ops::RangeTo> for Range { - fn from(range: ::std::ops::RangeTo) -> Range { +impl From<::core::ops::RangeTo> for Range { + fn from(range: ::core::ops::RangeTo) -> Range { Range { from: None, to: Some(range.end), @@ -334,8 +335,8 @@ impl From<::std::ops::RangeTo> for Range { } } -impl From<::std::ops::RangeFull> for Range { - fn from(_range: ::std::ops::RangeFull) -> Range { +impl From<::core::ops::RangeFull> for Range { + fn from(_range: ::core::ops::RangeFull) -> Range { Range { from: None, to: None, diff --git a/palette/src/lib.rs b/palette/src/lib.rs index d78d493db..ae2b1debc 100644 --- a/palette/src/lib.rs +++ b/palette/src/lib.rs @@ -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}; @@ -346,7 +359,7 @@ macro_rules! assert_ranges { mod macros; pub mod blend; -#[cfg(feature = "std")] + pub mod gradient; #[cfg(feature = "named")]