[][src]Macro vulkayes_core::impl_common_handle_traits

macro_rules! impl_common_handle_traits {
    (
		impl $([ $($impl_gen: tt)+ ])? HasHandle<$target: ty>, Deref, Borrow, Eq, Hash, Ord for $tp: ty {
			target = { $($target_code: tt)+ }
		} $(+ $deref: ident)?
	) => { ... };
    (
		impl $([ $($impl_gen: tt)+ ])? HasHandle<$target: ty>, Borrow, Eq, Hash, Ord for $tp: ty {
			target = { $($target_code: tt)+ }
		}
	) => { ... };
    (
		impl $([ $($impl_gen: tt)+ ])? HasSynchronizedHandle<$target: ty>, Deref, Borrow, Eq, Hash, Ord for $tp: ty {
			target = { $($target_code: tt)+ }
		}
	) => { ... };
    (
		impl $([ $($impl_gen: tt)+ ])? HasSynchronizedHandle<$target: ty>, Borrow, Eq, Hash, Ord for $tp: ty {
			target = { $($target_code: tt)+ }
		}
	) => { ... };
    (
		impl $([ $($impl_gen: tt)+ ])? Deref<$target: ty>, Borrow, Eq, Hash, Ord for $tp: ty {
			target = { $($target_code: tt)+ }

			$(
				to_handle { $($to_handle_code: tt)+ }
			)?
		}
	) => { ... };
    (
		impl $([ $($impl_gen: tt)+ ])? Borrow<$target: ty>, Eq, Hash, Ord for $tp: ty {
			target = { $($target_code: tt)+ }

			$(
				to_handle { $($to_handle_code: tt)+ }
			)?
		}
	) => { ... };
}

Implements Borrow, Deref, PartialEq, Eq, Hash, PartialOrd and Ord for a type based on its Borrow implementation.

This macro is closely tied to the HasHandle and HasSynchronizedHandle traits.

There are three variants of this macro:


struct MyType<A> {
	field_on_self: Target,
	other_field: A
}

// Base variant
// Deref is optional. If it is not desired, the `<Target>` token is appended to `Borrow` instead.
impl_common_handle_traits! {
	impl [A: Debug] Deref<Target>, Borrow, Eq, Hash, Ord for MyType<A> {
		target = { field_on_self } // Borrows and Derefs to `Target` by invoking `&self.field_on_self`

		to_handle { .handle() } // Gets a handle from `Target` by invoking `self.field_on_self.handle()`
	}
}

// HasHandle variant
// struct MyType<A> {
// 	field_on_self: vk::Image,
// 	other_field: A
// }
// impl_common_handle_traits! {
// 	impl [A: Debug] HasHandle<Target>, Deref, Borrow, Eq, Hash, Ord for MyType<A> {
// 		target = { field_on_self }
// 	}
// }

// HasSynchronizedHandle variant
// struct MyType<A> {
// 	field_on_self: Vutex<vk::Image>,
// 	other_field: A
// }
// impl_common_handle_traits! {
// 	impl [A: Debug] HasSynchronizedHandle<Target>, Deref, Borrow, Eq, Hash, Ord for MyType<A> {
// 		target = { field_on_self }
// 	}
// }

expands to:

// ...

// Base variant
// Deref is optional
impl<A: Debug> std::ops::Deref for MyType<A> {
	type Target = Target;

	fn deref(&self) -> &Self::Target {
		&self.field_on_self
	}
}
impl<A: Debug> std::borrow::Borrow<Target> for MyType<A> {
	fn borrow(&self) -> &Target {
		&self.field_on_self
	}
}

impl<A: Debug> PartialEq for MyType<A> {
	fn eq(&self, other: &Self) -> bool {
		self.field_on_self.handle() == other.field_on_self.handle()
	}
}
impl<A: Debug> Eq for MyType<A> {}
impl<A: Debug> std::hash::Hash for MyType<A> {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		self.field_on_self.handle().hash(state)
	}
}

impl<A: Debug> std::cmp::PartialOrd for MyType<A> {
	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
		self.field_on_self.handle().partial_cmp(&other.field_on_self.handle())
	}
}
impl<A: Debug> std::cmp::Ord for MyType<A> {
	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
		self.field_on_self.handle().cmp(&other.field_on_self.handle())
	}
}

// HasHandle variant adds to the previous implementations also:
// impl<A: Debug> vulkayes_core::util::handle::HasHandle<vk::Image> for MyType<A> {}

// While HasSynchronizedHandle adds:
// impl<A: Debug> vulkayes_core::util::handle::HasSynchronizedHandle<vk::Image> for MyType<A> {}