Skip to content

Commit 2d4c666

Browse files
authored
Merge pull request #1005 from MathieuDuponchelle/downcast-docs
object: improve downcasting docs
2 parents 1fddbfd + aac536d commit 2d4c666

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

glib/src/object.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,9 @@ pub trait Cast: ObjectType {
122122
/// Returns `Ok(T)` if the object is an instance of `T` and `Err(self)`
123123
/// otherwise.
124124
///
125-
/// *NOTE*: This statically checks at compile-time if casting is possible. It is not always
126-
/// known at compile-time, whether a specific object implements an interface or not, in which case
127-
/// `upcast` would fail to compile. `dynamic_cast` can be used in these circumstances, which
128-
/// is checking the types at runtime.
125+
/// *NOTE*: This will check at compile-time if `T` is lower down the
126+
/// inheritance tree of `Self`, but also check at runtime if downcasting
127+
/// is indeed possible.
129128
///
130129
/// # Example
131130
///
@@ -137,7 +136,7 @@ pub trait Cast: ObjectType {
137136
#[inline]
138137
fn downcast<T: ObjectType>(self) -> Result<T, Self>
139138
where
140-
Self: CanDowncast<T>,
139+
Self: MayDowncastTo<T>,
141140
{
142141
if self.is::<T>() {
143142
Ok(unsafe { self.unsafe_cast() })
@@ -152,10 +151,9 @@ pub trait Cast: ObjectType {
152151
/// Returns `Some(T)` if the object is an instance of `T` and `None`
153152
/// otherwise.
154153
///
155-
/// *NOTE*: This statically checks at compile-time if casting is possible. It is not always
156-
/// known at compile-time, whether a specific object implements an interface or not, in which case
157-
/// `upcast` would fail to compile. `dynamic_cast` can be used in these circumstances, which
158-
/// is checking the types at runtime.
154+
/// *NOTE*: This will check at compile-time if `T` is lower down the
155+
/// inheritance tree of `Self`, but also check at runtime if downcasting
156+
/// is indeed possible.
159157
///
160158
/// # Example
161159
///
@@ -167,7 +165,7 @@ pub trait Cast: ObjectType {
167165
#[inline]
168166
fn downcast_ref<T: ObjectType>(&self) -> Option<&T>
169167
where
170-
Self: CanDowncast<T>,
168+
Self: MayDowncastTo<T>,
171169
{
172170
if self.is::<T>() {
173171
Some(unsafe { self.unsafe_cast_ref() })
@@ -179,7 +177,9 @@ pub trait Cast: ObjectType {
179177
// rustdoc-stripper-ignore-next
180178
/// Tries to cast to an object of type `T`. This handles upcasting, downcasting
181179
/// and casting between interface and interface implementors. All checks are performed at
182-
/// runtime, while `downcast` and `upcast` will do many checks at compile-time already.
180+
/// runtime, while `upcast` will do many checks at compile-time already. `downcast` will
181+
/// perform the same checks at runtime as `dynamic_cast`, but will also ensure some amount of
182+
/// compile-time safety.
183183
///
184184
/// It is not always known at compile-time, whether a specific object implements an interface or
185185
/// not, and checking has to be performed at runtime.
@@ -303,10 +303,10 @@ pub trait CastNone: Sized {
303303
type Inner;
304304
fn and_downcast<T: ObjectType>(self) -> Option<T>
305305
where
306-
Self::Inner: CanDowncast<T>;
306+
Self::Inner: MayDowncastTo<T>;
307307
fn and_downcast_ref<T: ObjectType>(&self) -> Option<&T>
308308
where
309-
Self::Inner: CanDowncast<T>;
309+
Self::Inner: MayDowncastTo<T>;
310310
fn and_upcast<T: ObjectType>(self) -> Option<T>
311311
where
312312
Self::Inner: IsA<T>;
@@ -322,15 +322,15 @@ impl<I: ObjectType + Sized> CastNone for Option<I> {
322322
#[inline]
323323
fn and_downcast<T: ObjectType>(self) -> Option<T>
324324
where
325-
Self::Inner: CanDowncast<T>,
325+
Self::Inner: MayDowncastTo<T>,
326326
{
327327
self.and_then(|i| i.downcast().ok())
328328
}
329329

330330
#[inline]
331331
fn and_downcast_ref<T: ObjectType>(&self) -> Option<&T>
332332
where
333-
Self::Inner: CanDowncast<T>,
333+
Self::Inner: MayDowncastTo<T>,
334334
{
335335
self.as_ref().and_then(|i| i.downcast_ref())
336336
}
@@ -365,9 +365,9 @@ impl<I: ObjectType + Sized> CastNone for Option<I> {
365365

366366
// rustdoc-stripper-ignore-next
367367
/// Marker trait for the statically known possibility of downcasting from `Self` to `T`.
368-
pub trait CanDowncast<T> {}
368+
pub trait MayDowncastTo<T> {}
369369

370-
impl<Super: IsA<Super>, Sub: IsA<Super>> CanDowncast<Sub> for Super {}
370+
impl<Super: IsA<Super>, Sub: IsA<Super>> MayDowncastTo<Sub> for Super {}
371371

372372
// Manual implementation of glib_shared_wrapper! because of special cases
373373
#[repr(transparent)]

0 commit comments

Comments
 (0)