extern crate std;
use crate::{IInspectable, IUnknown, Interface};
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use core::panic::Location;
use std::sync::Mutex;
#[derive(Clone, Debug)]
pub struct CastCount {
pub location: &'static Location<'static>,
pub source: &'static str,
pub target: &'static str,
pub count: u64,
}
type Key = (&'static str, u32, u32, &'static str, &'static str);
struct Site {
location: &'static Location<'static>,
count: u64,
avoidable: bool,
hinted: bool,
}
static SITES: Mutex<BTreeMap<Key, Site>> = Mutex::new(BTreeMap::new());
pub fn take_cast_counts() -> Vec<CastCount> {
let mut sites = SITES.lock().unwrap();
let counts = sites
.iter()
.filter(|(_, site)| site.count != 0)
.map(|((.., source, target), site)| CastCount {
location: site.location,
source,
target,
count: site.count,
})
.collect();
sites.values_mut().for_each(|site| site.count = 0);
counts
}
#[cold]
#[inline(never)]
pub(crate) fn record_cast<S: Interface, T: Interface>(
location: &'static Location<'static>,
same_pointer: bool,
) {
let source = core::any::type_name::<S>();
let target = core::any::type_name::<T>();
let key = (
location.file(),
location.line(),
location.column(),
source,
target,
);
let mut sites = SITES.lock().unwrap();
let site = sites.entry(key).or_insert(Site {
location,
count: 0,
avoidable: false,
hinted: false,
});
site.count += 1;
let source_size = size_of::<S::Vtable>();
let target_size = size_of::<T::Vtable>();
let unknown_size = size_of::<<IUnknown as Interface>::Vtable>();
let instead = if S::IID == T::IID && T::IID != IUnknown::IID {
Some("as_interface")
} else if S::INSPECTABLE && T::IID == IInspectable::IID {
Some(".into()")
} else {
None
};
if site.hinted {
return;
}
if let Some(instead) = instead {
site.avoidable = true;
site.hinted = true;
drop(sites);
std::eprintln!(
"windows-core: cast::<{target}> from {source} at {location} is avoidable; use \
{instead} instead",
);
} else if same_pointer && target_size > unknown_size && target_size < source_size {
site.hinted = true;
drop(sites);
std::eprintln!(
"windows-core: cast::<{target}> from {source} at {location} returned the same \
interface pointer; if {target} is a base of {source}, use Deref or .into() instead",
);
}
}
pub fn avoidable_casts() -> Vec<CastCount> {
SITES
.lock()
.unwrap()
.iter()
.filter(|(_, site)| site.avoidable)
.map(|((.., source, target), site)| CastCount {
location: site.location,
source,
target,
count: site.count,
})
.collect()
}