# Sealed inheritance

**URL:** <https://internals.rust-lang.org/t/sealed-inheritance/9197>\
**Category:** language design\
**Created:** [January 12, 2019, 8:15pm UTC](https://internals.rust-lang.org/t/sealed-inheritance/9197 "2019-01-12T20:15:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hydroper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hydroper/32/5003_2.png) [@hydroper](https://internals.rust-lang.org/u/hydroper)\
**Post date:** [January 12, 2019, 8:15pm UTC](https://internals.rust-lang.org/t/sealed-inheritance/9197/1 "2019-01-12T20:15:32Z")

</div>

Regarding [cheap field access](https://stackoverflow.com/questions/51698722/inline-a-common-method/51704002?r=SearchResults#51704002) I’m actively performing internal struct inheritance in Rust and this of course uses more general pointers (`Rc`). One issue is that from the beginning I had to implement methods like `as_element()`, `as_canvas()` etc. to perform cast and call them miscellanous times.

Traits and enums aren’t the best solution. For retrieving coordinates from my base `DisplayElement` I get trait dispatch for each or, for enums, all elements have the same fixed size (i.e., `sizeof::<DisplayElement::Sprite>() == sizeof::<DisplayElement::Canvas>()`).

Besides variant size being an issue, `enum`s aren’t flexible. Sealed struct inheritance is flexible and _fast_ (allows direct access to _x_ and _y_ in general cases; you could even store offset-bounds like l and h for finally performing partial collision-check w/ display elements and then perform exact collision check using runtime dispatches for the actual element type).

First, of course inheritance is possible in Rust, though the language is missing flexibility with pointers such as `Rc`; they leak `AsRc` for implicit convertion. For inheritance a unsafe function such as `cast` is needed:

```
unsafe fn cast<'a, Out, In>(r: &'a Rc<In>) -> Rc<Out> {
    Rc::from_raw(Rc::into_raw(Rc::clone(r)) as
        *const () as *const Out)
}

```

Then here’s a base class sealed for our crate or module:

#[repr©] pub struct DisplayElement { #[doc(hidden)] pub \_kind: ElKind, pub x: Cell, }

`Fro

---

<div class="post-metadata">

**Author:** ![hydroper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hydroper/32/5003_2.png) [@hydroper](https://internals.rust-lang.org/u/hydroper)\
**Post date:** [January 12, 2019, 8:21pm UTC](https://internals.rust-lang.org/t/sealed-inheritance/9197/2 "2019-01-12T20:21:21Z")

</div>

My phone shut down like a donkey and submitted this thread accidentally. I was still writting it 😕

---

<div class="post-metadata">

**Author:** ![skysch](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@skysch](https://internals.rust-lang.org/u/skysch)\
**Post date:** [January 17, 2019, 4:00pm UTC](https://internals.rust-lang.org/t/sealed-inheritance/9197/3 "2019-01-17T16:00:47Z")

</div>

Given what you wrote, I’m wondering if you have tried using a trait hierarchy with specialization? I’ve been able emulate inheritance fairly well with it, so it is worth giving it a shot. It can be verbose, but probably some careful macro use could fix that.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 17, 2019, 4:08pm UTC](https://internals.rust-lang.org/t/sealed-inheritance/9197/4 "2019-04-17T16:08:52Z")

</div>

Closed on April 17, 2019, 4:08pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
