use ordered_float::NotNan;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_derive::*;
use std::sync::Arc;
fn deserialize_notnan<'de, D>(deserializer: D) -> Result<NotNan<f32>, D::Error>
where
D: Deserializer<'de>,
{
let value = f32::deserialize(deserializer)?;
NotNan::new(value).map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
}
#[cfg_attr(feature = "cargo-clippy", allow(clippy::trivially_copy_pass_by_ref))]
fn serialize_notnan<S>(value: &NotNan<f32>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
value.into_inner().serialize(serializer)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TextureCoordinate {
#[serde(
deserialize_with = "deserialize_notnan",
serialize_with = "serialize_notnan"
)]
pub x: NotNan<f32>,
#[serde(
deserialize_with = "deserialize_notnan",
serialize_with = "serialize_notnan"
)]
pub y: NotNan<f32>,
}
impl TextureCoordinate {
pub fn new(x: NotNan<f32>, y: NotNan<f32>) -> Self {
Self { x, y }
}
pub fn new_f32(x: f32, y: f32) -> Self {
let x = NotNan::new(x).unwrap();
let y = NotNan::new(y).unwrap();
Self::new(x, y)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImageCell {
top_left: TextureCoordinate,
bottom_right: TextureCoordinate,
data: Arc<ImageData>,
}
impl ImageCell {
pub fn new(
top_left: TextureCoordinate,
bottom_right: TextureCoordinate,
data: Arc<ImageData>,
) -> Self {
Self {
top_left,
bottom_right,
data,
}
}
}
static IMAGE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImageData {
id: usize,
data: Vec<u8>,
}
impl ImageData {
pub fn with_raw_data(data: Vec<u8>) -> Self {
let id = IMAGE_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed);
Self { id, data }
}
#[inline]
pub fn data(&self) -> &[u8] {
&self.data
}
#[inline]
pub fn id(&self) -> usize {
self.id
}
}