Implement image loding test

This commit is contained in:
Dennis
2022-08-13 19:15:48 +02:00
committed by Keavon Chambers
parent 7eb050092c
commit fa0b896825
9 changed files with 665 additions and 30 deletions

View File

@@ -1,9 +1,10 @@
use core::marker::PhantomData;
use core::ops::Add;
use crate::Node;
pub struct AddNode;
impl<'n, L: Add<R>, R> Node<'n, (L, R)> for AddNode {
impl<'n, L: Add<R, Output = O> + 'n, R, O: 'n> Node<'n, (L, R)> for AddNode {
type Output = <L as Add<R>>::Output;
fn eval(&'n self, input: (L, R)) -> Self::Output {
input.0 + input.1
@@ -52,9 +53,27 @@ impl<'n, T, U: 'n> Node<'n, &'n (T, U)> for SndNode {
}
}
/// Destructures a Tuple of two values and returns them in reverse order
pub struct SwapNode;
impl<'n, T: 'n, U: 'n> Node<'n, (T, U)> for SwapNode {
type Output = (U, T);
fn eval(&'n self, input: (T, U)) -> Self::Output {
let (a, b) = input;
(b, a)
}
}
impl<'n, T, U: 'n> Node<'n, &'n (T, U)> for SwapNode {
type Output = (&'n U, &'n T);
fn eval(&'n self, input: &'n (T, U)) -> Self::Output {
let (a, b) = input;
(b, a)
}
}
/// Return a tuple with two instances of the input argument
pub struct DupNode;
impl<'n, T: Clone> Node<'n, T> for DupNode {
impl<'n, T: Clone + 'n> Node<'n, T> for DupNode {
type Output = (T, T);
fn eval(&'n self, input: T) -> Self::Output {
(input.clone(), input) //TODO: use Copy/Clone implementation
@@ -63,13 +82,46 @@ impl<'n, T: Clone> Node<'n, T> for DupNode {
/// Return the Input Argument
pub struct IdNode;
impl<'n, T> Node<'n, T> for IdNode {
impl<'n, T: 'n> Node<'n, T> for IdNode {
type Output = T;
fn eval(&'n self, input: T) -> Self::Output {
input
}
}
pub struct MapResultNode<'n, MN: Node<'n, I>, I, E>(pub MN, pub PhantomData<&'n (I, E)>);
impl<'n, MN: Node<'n, I>, I, E> Node<'n, Result<I, E>> for MapResultNode<'n, MN, I, E> {
type Output = Result<MN::Output, E>;
fn eval(&'n self, input: Result<I, E>) -> Self::Output {
input.map(|x| self.0.eval(x))
}
}
impl<'n, MN: Node<'n, I>, I, E> MapResultNode<'n, MN, I, E> {
pub const fn new(mn: MN) -> Self {
Self(mn, PhantomData)
}
}
pub struct FlatMapResultNode<'n, MN: Node<'n, I>, I, E>(pub MN, pub PhantomData<&'n (I, E)>);
impl<'n, MN: Node<'n, I, Output = Result<O, E>>, I, O: 'n, E: 'n> Node<'n, Result<I, E>> for FlatMapResultNode<'n, MN, I, E> {
type Output = Result<O, E>;
fn eval(&'n self, input: Result<I, E>) -> Self::Output {
match input.map(|x| self.0.eval(x)) {
Ok(Ok(x)) => Ok(x),
Ok(Err(e)) => Err(e),
Err(e) => Err(e),
}
}
}
impl<'n, MN: Node<'n, I>, I, E> FlatMapResultNode<'n, MN, I, E> {
pub const fn new(mn: MN) -> Self {
Self(mn, PhantomData)
}
}
#[cfg(test)]
mod test {
use super::*;

View File

@@ -134,6 +134,18 @@ impl Color {
(self.red, self.green, self.blue, self.alpha)
}
/// Return the all components as a u8 slice, first component is red, followed by green, followed by blue, followed by alpha.
///
/// # Examples
/// ```
/// use graphene_core::raster::color::Color;
/// let color = Color::from_rgbaf32(0.114, 0.103, 0.98, 0.97).unwrap();
/// //TODO: Add test
/// ```
pub fn to_rgba8(&self) -> [u8; 4] {
[(self.red * 255.) as u8, (self.green * 255.) as u8, (self.blue * 255.) as u8, (self.alpha * 255.) as u8]
}
// TODO: Readd formatting
/// Creates a color from a 8-character RGBA hex string (without a # prefix).

View File

@@ -58,3 +58,17 @@ where
(input, arg)
}
}
pub struct ConsPassInputNode<Root>(pub Root);
impl<'n, Root, L, R> Node<'n, (L, R)> for ConsPassInputNode<Root>
where
Root: Node<'n, R>,
{
type Output = (L, <Root as Node<'n, R>>::Output);
fn eval(&'n self, input: (L, R)) -> Self::Output {
let arg = self.0.eval(input.1);
(input.0, arg)
}
}