1use crate::{Object, ObjectId};
2use std::marker::PhantomData;
3use ulid::Ulid;
4
5#[derive(Clone, Eq, PartialEq, educe::Educe, serde::Deserialize, serde::Serialize)]
6#[educe(Debug(named_field = false), Ord, PartialOrd)]
7pub struct DbPtr<T: Object> {
8 #[educe(Debug(method = std::fmt::Display::fmt))]
9 pub id: Ulid,
10 #[educe(Debug(ignore))]
11 _phantom: PhantomData<T>,
12}
13
14impl<T: Object> deepsize::DeepSizeOf for DbPtr<T> {
15 fn deep_size_of_children(&self, _context: &mut deepsize::Context) -> usize {
16 0
17 }
18}
19
20impl<T: Object> Copy for DbPtr<T> {}
21
22impl<T: Object> DbPtr<T> {
23 pub fn from(id: ObjectId) -> DbPtr<T> {
24 DbPtr {
25 id: id.0,
26 _phantom: PhantomData,
27 }
28 }
29
30 pub fn to_object_id(&self) -> ObjectId {
31 ObjectId(self.id)
32 }
33
34 pub fn from_string(s: &str) -> anyhow::Result<DbPtr<T>> {
35 Ok(DbPtr {
36 id: Ulid::from_string(s)?,
37 _phantom: PhantomData,
38 })
39 }
40}
41
42#[cfg(feature = "arbitrary")]
43impl<'a, T: Object> arbitrary::Arbitrary<'a> for DbPtr<T> {
44 fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
45 Ok(Self {
46 id: Ulid::from_bytes(u.arbitrary()?),
47 _phantom: PhantomData,
48 })
49 }
50}