Rust: core::marker::๋ณด๋‚ด๊ธฐ ๋ฐ ์›์‹œ ํฌ์ธํ„ฐ

์— ๋งŒ๋“  2015๋…„ 01์›” 28์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: rust-lang/rust

Rust ์ฑ… ์—์„œ๋Š” ์›์‹œ ํฌ์ธํ„ฐ๊ฐ€ "์ „์†ก ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค(๋‚ด์šฉ์ด ์ „์†ก ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ๋˜๋Š” ๊ฒฝ์šฐ)"๋ผ๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋‚ด๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฐ”์— ๋”ฐ๋ฅด๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” Sendable ์œ ํ˜•์— ๋Œ€ํ•œ ์›์‹œ ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋‚˜๋ฉด the trait core::marker::Send is not implemented for the type ์˜ค๋ฅ˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ ๋ญ”๊ฐ€ ์ž˜๋ชปํ•˜๊ณ  ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ , ๋ฌธ์„œ๊ฐ€ ๋ถˆ๋ถ„๋ช…ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์ด ์ฝ”๋“œ์˜ ์‹ค์ œ ๋ฌธ์ œ๊ฐ€ ๋ฌด์—‡์ด๋“  ๊ฐ„์— ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋„์›€์ด ๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

extern crate core;
use std::thread::Thread;

struct ShouldBeSendable {
    x: i32
}
unsafe impl core::marker::Send for ShouldBeSendable { }


fn main() {
    let sendablePtr : *const ShouldBeSendable = &ShouldBeSendable {x: 5};
            let closure = move |:| {
                *sendablePtr;
            };
    let something = Thread::spawn(closure);
}

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

test.rs:15:21: 15:34 error: the trait `core::marker::Send` is not implemented for the type `*const ShouldBeSendable` [E0277]
test.rs:15     let something = Thread::spawn(closure);
                               ^~~~~~~~~~~~~
test.rs:15:21: 15:34 note: `*const ShouldBeSendable` cannot be sent between threads safely
test.rs:15     let something = Thread::spawn(closure);
                               ^~~~~~~~~~~~~
error: aborting due to previous error
$ rustc --version
rustc 1.0.0-dev (d15192317 2015-01-25 16:09:48 +0000)

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

NonNull ์‚ฌ์šฉํ•˜๋Š” Rust 1.25์˜ @japaric ์˜ˆ์ œ.

https://play.rust-lang.org/?gist=1ce2532a0eefc60695663c26faddebe1&version=stable

๋ชจ๋“  3 ๋Œ“๊ธ€

์ด๊ฒƒ์€ ์ตœ๊ทผ์— ๋ณ€๊ฒฝ๋˜์–ด ๋ฌธ์„œ๊ฐ€ ์ตœ์‹  ์ƒํƒœ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๋ฒ„๊ทธ๋ฅผ ์•Œ์•„์ฐจ๋ฆฌ๊ณ  ์‹ ๊ณ ํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

@drewcrawford Unique ๋ž˜ํผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›์‹œ ํฌ์ธํ„ฐ๋ฅผ Send ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ˆ˜์ •๋œ ๋ฒ„์ „์˜ ์ฝ”๋“œ๊ฐ€ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

use std::ptr::Unique;
use std::thread::Thread;

#[derive(Copy)]
struct ShouldBeSendable {
    x: i32
}

unsafe impl std::marker::Send for ShouldBeSendable { }

fn main() {
    let ptr : *mut ShouldBeSendable = &mut ShouldBeSendable {x: 5};  // this is not `Send`
    let sendablePtr = Unique(ptr);  // but this is!

    let closure = move |:| {
        // `sendablePtr` con be moved inside this closure
        let ptr = sendablePtr.0;  // unpack the raw pointer
        println!("{}", unsafe { (*ptr).x })
    };
    let something = Thread::scoped(closure).join();
}

NonNull ์‚ฌ์šฉํ•˜๋Š” Rust 1.25์˜ @japaric ์˜ˆ์ œ.

https://play.rust-lang.org/?gist=1ce2532a0eefc60695663c26faddebe1&version=stable

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰