use crate::os;
use crate::weak;
use std::io;
use std::iter::FusedIterator;
use std::path;
use std::vec;
#[cfg(unix)]
use os::unix as imp;
#[cfg(windows)]
use os::windows as imp;
#[derive(Debug, Clone)]
pub struct Images {
inner: vec::IntoIter<weak::Weak>,
}
impl Images {
pub fn now() -> io::Result<Self> {
let inner = unsafe { imp::load_objects()?.into_iter() };
Ok(Self { inner })
}
}
impl From<Vec<weak::Weak>> for Images {
fn from(value: Vec<weak::Weak>) -> Self {
Self {
inner: value.into_iter(),
}
}
}
impl Iterator for Images {
type Item = weak::Weak;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.inner.count()
}
}
impl DoubleEndedIterator for Images {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back()
}
}
impl ExactSizeIterator for Images {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl FusedIterator for Images {}
#[repr(C)]
pub struct Image {
_data: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}
impl Image {
pub const fn magic(&self) -> *const [u8] {
let hdr = self as *const Image;
let len: usize = if cfg!(windows) { 2 } else { 4 };
std::ptr::slice_from_raw_parts(hdr.cast::<u8>(), len)
}
pub fn path(&self) -> io::Result<path::PathBuf> {
unsafe { imp::hdr_path(self as *const Image) }
}
pub fn to_bytes(&self) -> io::Result<&[u8]> {
let len = unsafe { imp::hdr_size(self)? };
let data = self as *const Image as *const u8;
let slice = unsafe { std::slice::from_raw_parts::<u8>(data, len) };
Ok(slice)
}
}