use std::num::{NonZeroU32, NonZeroU64, NonZeroUsize};
use crate::{ContextId, sys};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct TextureId(u64);
impl TextureId {
#[inline]
pub const fn new(id: u64) -> Self {
Self(id)
}
#[inline]
pub const fn id(self) -> u64 {
self.0
}
#[inline]
pub const fn null() -> Self {
Self(0)
}
#[inline]
pub const fn is_null(self) -> bool {
self.0 == 0
}
pub fn try_as_usize(self) -> Option<usize> {
usize::try_from(self).ok()
}
pub fn try_as_u32(self) -> Option<u32> {
u32::try_from(self).ok()
}
pub fn try_as_ptr<T>(self) -> Option<*const T> {
self.try_as_usize().map(|value| value as *const T)
}
pub fn try_as_mut_ptr<T>(self) -> Option<*mut T> {
self.try_as_usize().map(|value| value as *mut T)
}
}
impl From<u64> for TextureId {
#[inline]
fn from(id: u64) -> Self {
TextureId(id)
}
}
impl From<u32> for TextureId {
#[inline]
fn from(id: u32) -> Self {
Self(u64::from(id))
}
}
impl From<NonZeroU32> for TextureId {
#[inline]
fn from(id: NonZeroU32) -> Self {
Self(u64::from(id.get()))
}
}
impl From<NonZeroU64> for TextureId {
#[inline]
fn from(id: NonZeroU64) -> Self {
Self(id.get())
}
}
impl From<NonZeroUsize> for TextureId {
#[inline]
fn from(id: NonZeroUsize) -> Self {
Self(id.get() as u64)
}
}
impl<T> From<*const T> for TextureId {
#[inline]
fn from(ptr: *const T) -> Self {
TextureId(ptr as usize as u64)
}
}
impl<T> From<*mut T> for TextureId {
#[inline]
fn from(ptr: *mut T) -> Self {
TextureId(ptr as usize as u64)
}
}
impl From<usize> for TextureId {
#[inline]
fn from(id: usize) -> Self {
TextureId(id as u64)
}
}
impl TryFrom<TextureId> for usize {
type Error = std::num::TryFromIntError;
#[inline]
fn try_from(id: TextureId) -> Result<Self, Self::Error> {
Self::try_from(id.0)
}
}
impl TryFrom<TextureId> for u32 {
type Error = std::num::TryFromIntError;
#[inline]
fn try_from(id: TextureId) -> Result<Self, Self::Error> {
Self::try_from(id.0)
}
}
impl Default for TextureId {
#[inline]
fn default() -> Self {
Self::null()
}
}
pub type RawTextureId = sys::ImTextureID;
impl From<TextureId> for RawTextureId {
#[inline]
fn from(id: TextureId) -> Self {
id.id()
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct ManagedTextureId {
context: ContextId,
slot: u32,
generation: NonZeroU64,
}
impl ManagedTextureId {
#[inline]
pub(crate) const fn new(context: ContextId, slot: u32, generation: NonZeroU64) -> Self {
Self {
context,
slot,
generation,
}
}
#[inline]
pub const fn context_id(self) -> ContextId {
self.context
}
#[inline]
pub(crate) const fn slot(self) -> u32 {
self.slot
}
#[inline]
pub(crate) const fn generation(self) -> NonZeroU64 {
self.generation
}
}