Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Comments

feat: relax Sized bound on Mutex#202

Merged
tropicaaal merged 2 commits intomainfrom
feat/mutex-unsized
Oct 31, 2024
Merged

feat: relax Sized bound on Mutex#202
tropicaaal merged 2 commits intomainfrom
feat/mutex-unsized

Conversation

@tropicaaal
Copy link
Member

Describe the changes this PR makes. Why should it be merged?

Makes T on Mutex<T> ?Sized, relaxing the requirement that T have a known size at compile time. This matches the behavior of the standard library.

I think this is a breaking change? (although it shouldn't affect the vast majority of people)

@tropicaaal tropicaaal added scope: core Relates to vexide-core or Rust language support scope: async Relates to the vexide async runtime. semver: major Backwards-incompatible API change or suggestion labels Oct 29, 2024
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}

impl<T> Mutex<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you cannot use Mutex::new if T is unsized, how is this relaxed Size bound useful?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

github kinda threw, the line im talking about is 93

Copy link
Member Author

@tropicaaal tropicaaal Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutexes can be coerced into an unsized type after creation, delegating the bound on Sized to a wrapper type like Arc. This is useful for trait objects - for example:

use std::sync::*;
use std::any::Any;

fn main() {
    let x: Arc<Mutex<dyn Any>> = Arc::new(Mutex::new(3));
}

Would avoid the additional heap allocation of an Arc<Mutex<Box<dyn Any>>>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@Gavin-Niederman Gavin-Niederman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tropicaaal tropicaaal merged commit 9a56cda into main Oct 31, 2024
@tropicaaal tropicaaal deleted the feat/mutex-unsized branch October 31, 2024 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: async Relates to the vexide async runtime. scope: core Relates to vexide-core or Rust language support semver: major Backwards-incompatible API change or suggestion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants