1use crate::{AssetName, RawWeakId};
6use message_channel::Sender;
7use std::fmt::{Debug, Display};
8use tracing::info;
9
10#[derive(Debug)]
12pub struct DropMessage {
13 pub asset_id: RawWeakId,
14}
15
16pub struct AssetOwner {
18 id: RawWeakId,
19 #[allow(unused)]
20 asset_name: Option<AssetName>,
21 drop_channel: Sender<DropMessage>,
22}
23
24impl Debug for AssetOwner {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 let asset_name = self
27 .asset_name
28 .map_or("unknown".to_string(), |name| name.to_string());
29 write!(f, "({:?} {})", self.id, asset_name)
30 }
31}
32
33impl Display for AssetOwner {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 write!(f, "{}", self.id)
36 }
37}
38
39impl PartialEq<Self> for AssetOwner {
40 fn eq(&self, other: &Self) -> bool {
41 self.id == other.id && self.asset_name == other.asset_name
42 }
43}
44
45impl PartialOrd<Self> for AssetOwner {
46 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
47 Some(self.id.cmp(&other.id))
48 }
49}
50
51impl Ord for AssetOwner {
52 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
53 self.id.cmp(&other.id)
54 }
55}
56
57impl Eq for AssetOwner {}
58
59impl Drop for AssetOwner {
60 fn drop(&mut self) {
61 info!(id=%self.id, "notice: asset owner is dropped");
62 let _ = self.drop_channel.send(DropMessage { asset_id: self.id });
63 }
64}
65
66impl AssetOwner {
67 #[must_use]
68 pub const fn new(
69 id: RawWeakId,
70 asset_name: Option<AssetName>,
71 drop_channel: Sender<DropMessage>,
72 ) -> Self {
73 Self {
74 id,
75 asset_name,
76 drop_channel,
77 }
78 }
79
80 #[must_use]
81 pub const fn raw_id(&self) -> RawWeakId {
82 self.id
83 }
84
85 #[must_use]
86 pub const fn asset_name(&self) -> Option<AssetName> {
87 self.asset_name
88 }
89}