# Constness when interfacing with foreign code

**URL:** <https://users.rust-lang.org/t/constness-when-interfacing-with-foreign-code/140275>\
**Category:** help\
**Created:** [May 26, 2026, 1:15pm UTC](https://users.rust-lang.org/t/constness-when-interfacing-with-foreign-code/140275 "2026-05-26T13:15:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eclipse-impl](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/eclipse-impl/32/52682_2.png) [@eclipse-impl](https://users.rust-lang.org/u/eclipse-impl)\
**Post date:** [May 26, 2026, 1:15pm UTC](https://users.rust-lang.org/t/constness-when-interfacing-with-foreign-code/140275/1 "2026-05-26T13:15:23Z")

</div>

I'm interfacing Rust with C++ (using cxx) and faced the following situation:

The Rust code calls an adapter class, and this adapter class holds a (non-const) `std::unique_ptr` to the object implementing the functionality.

As the adapter class has only this pointer as state, and it's never mutated, all adapter methods are marked as `const` (which is correct by C++'s understanding of constness: no member variable is changed.)

Therefore the interface to call the adapter methods is made using only `self: &T`, and this compiles correctly.

I know that the pontee of the `std::unique_ptr` has state and mutates it when the adapter calls some of its methods (it does so, however, using a mutex, so concurrent access would not be a problem.)

That is, we have something like:

```rust
Rust Code -- self: &T --> Adapter C++ Class (const) -- std::unique_ptr --> C++ business logic (mutating)

```

My question is: From Rust's perspective is it fine to ignore that there're state changes happening on the C++ side? Or could it be that the compiler could perform some optimization based on the expectation of constness, that could trigger some UB?

In other words, as long as the Adapter is const, that's all that Rust needs to know about the C++ interface, or if there's mutability down the road then I should let Rust know about it, say, by wrapping the Adapter into an `UnsafeCell`?

---

<div class="post-metadata">

**Author:** ![giocri](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/giocri/32/49821_2.png) [@giocri](https://users.rust-lang.org/u/giocri)\
**Post date:** [May 26, 2026, 1:32pm UTC](https://users.rust-lang.org/t/constness-when-interfacing-with-foreign-code/140275/2 "2026-05-26T13:32:50Z")

</div>

i am fairly confident rust does not assume calls through ffi to be guaranteed to return the same results each time so if rust only ever interacts with the adapter and the adapter itself does not change then rust is fine to have it behind a & because that is the extent to witch it expects the invariant to be upheld.

just know that you cannot convert the unique\_ptr itself into a & since the data pointed by it does indeed change

---

<div class="post-metadata">

**Author:** ![ProgramCrafter](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/programcrafter/32/45916_2.png) [@ProgramCrafter](https://users.rust-lang.org/u/ProgramCrafter)\
**Post date:** [May 26, 2026, 1:40pm UTC](https://users.rust-lang.org/t/constness-when-interfacing-with-foreign-code/140275/3 "2026-05-26T13:40:07Z")

</div>

If it's fine to call `self.foo` several times, and if it's not going to cause UB if you call it reentrantly (i.e. from a previous `self.foo()` call), then just a shared reference should be OK. Otherwise, use exclusive one which is `&mut`.

---

<div class="post-metadata">

**Author:** ![jonh](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@jonh](https://users.rust-lang.org/u/jonh)\
**Post date:** [May 26, 2026, 2:42pm UTC](https://users.rust-lang.org/t/constness-when-interfacing-with-foreign-code/140275/4 "2026-05-26T14:42:43Z")

</div>

If your not dereferencing in Rust it will have no exposure to any internal mutation. Using `UnsafeCell`would be unnecessary, similar to how `Pin` can be unnecessary when you full control heap content (eg from raw `Box`) that your internal code depends on being in a fixed location.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kpreid/32/22420_2.png) [@kpreid](https://users.rust-lang.org/u/kpreid)\
**Post date:** [May 26, 2026, 2:53pm UTC](https://users.rust-lang.org/t/constness-when-interfacing-with-foreign-code/140275/5 "2026-05-26T14:53:03Z")

</div>

In general, there are three ways to have mutable state behind an `&` reference:

1. Hold the data inline in `UnsafeCell`.
2. Hold the data behind a pointer, such that no pointer to it has aliasing rules (no `&`, `&mut`, `Box`, etc.).
3. Hold the data outside of memory entirely (e.g. a file or pipe).

Your case is the second. From what you’ve written, your C++ code is roughly equivalent to this Rust code:

```rust
struct Adapter {
    data: *mut Implementation,
}
struct Implementation {
    value: u32,
}

impl Adapter {
    pub fn new() -> Self {
        Adapter {
            data: Box::into_raw(Box::new(Implementation { value: 0 })),
        }
    }
    pub fn increment(&self) {
        unsafe { (*self.data).value += 1 }
    }
    pub fn get(&self) -> u32 {
        unsafe { (*self.data).value }
    }
}

impl Drop for Adapter {
    fn drop(&mut self) {
        drop(unsafe { Box::from_raw(self.data) });
    }
}

fn main() {
    let adapter = Adapter::new();
    adapter.increment();
    assert_eq!(adapter.get(), 1);
}

```

You can run this Rust code under Miri in the playground and see that it has no objection. (More care would be needed if `Adapter: Sync` were wanted, but it is automatically `!Sync` by default.)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 24, 2026, 2:54pm UTC](https://users.rust-lang.org/t/constness-when-interfacing-with-foreign-code/140275/6 "2026-08-24T14:54:00Z")

</div>

Closed on August 24, 2026, 2:54pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
