Is it sound to transmute a MaybeUninit to [MaybeUninit; N]?Where is the Rust intrinsic called “transmute” actually implemented?How can you make a safe static singleton in Rust?Temporarily transmute [u8] to [u16]Transmuting `bool` to `u8`Rust, FFI, and lifetime for string transmutationSafe way to convert array dimensions in RustTransmuting u8 buffer to struct in Rusttransmute called with types of different sizesWhy am I not allowed to transmute a value containing a trait's associated type?Is transmuting PhantomData markers safe?

How did Thanos not realise this had happened at the end of Endgame?

Create a list of all possible Boolean configurations of three constraints

How to compact two the parabol commands in the following example?

Why was the Ancient One so hesitant to teach Dr. Strange the art of sorcery?

How does noise-cancellation work in Mac laptops?

Does the 500 feet falling cap apply per fall, or per turn?

What does the expression "right on the tip of my tongue" mean?

What kind of SATA connector is this?

How to cope with regret and shame about not fully utilizing opportunities during PhD?

What food production methods would allow a metropolis like New York to become self sufficient

Front derailleur hard to move due to gear cable angle

Should these notes be played as a chord or one after another?

Can a tourist shoot a gun in the USA?

Plastic-on-plastic lubricant that wont leave a residue?

Does Lawful Interception of 4G / the proposed 5G provide a back door for hackers as well?

Extrude the faces of a cube symmetrically along XYZ

Size of a folder with du

Is the schwa sound consistent?

What happens if a creature that would fight isn't on the battlefield anymore?

Why does the Earth follow an elliptical trajectory rather than a parabolic one?

What is the significance of 4200 BCE in context of farming replacing foraging in Europe?

Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?

Why doesn't Rocket Lab use a solid stage?

Exception propagation: When should I catch exceptions?



Is it sound to transmute a MaybeUninit to [MaybeUninit; N]?


Where is the Rust intrinsic called “transmute” actually implemented?How can you make a safe static singleton in Rust?Temporarily transmute [u8] to [u16]Transmuting `bool` to `u8`Rust, FFI, and lifetime for string transmutationSafe way to convert array dimensions in RustTransmuting u8 buffer to struct in Rusttransmute called with types of different sizesWhy am I not allowed to transmute a value containing a trait's associated type?Is transmuting PhantomData markers safe?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








7















Is the following code sound?



#![feature(maybe_uninit)]
use std::mem;
const N: usize = 2; // or another number
type T = String; // or any other type

fn main()
unsafe
// create an uninitialized array
let t: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninitialized();
// convert it to an array of uninitialized values
let mut t: [mem::MaybeUninit<T>; N] = mem::transmute(t);
// initialize the values
t[0].set("Hi".to_string());
t[1].set("there".to_string());
// use the values
println!(" ", t[0].get_ref(), t[1].get_ref());
// drop the values
mem::replace(&mut t[0], mem::MaybeUninit::uninitialized()).into_initialized();
mem::replace(&mut t[1], mem::MaybeUninit::uninitialized()).into_initialized();




I should note that miri runs it without problems.










share|improve this question




























    7















    Is the following code sound?



    #![feature(maybe_uninit)]
    use std::mem;
    const N: usize = 2; // or another number
    type T = String; // or any other type

    fn main()
    unsafe
    // create an uninitialized array
    let t: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninitialized();
    // convert it to an array of uninitialized values
    let mut t: [mem::MaybeUninit<T>; N] = mem::transmute(t);
    // initialize the values
    t[0].set("Hi".to_string());
    t[1].set("there".to_string());
    // use the values
    println!(" ", t[0].get_ref(), t[1].get_ref());
    // drop the values
    mem::replace(&mut t[0], mem::MaybeUninit::uninitialized()).into_initialized();
    mem::replace(&mut t[1], mem::MaybeUninit::uninitialized()).into_initialized();




    I should note that miri runs it without problems.










    share|improve this question
























      7












      7








      7


      1






      Is the following code sound?



      #![feature(maybe_uninit)]
      use std::mem;
      const N: usize = 2; // or another number
      type T = String; // or any other type

      fn main()
      unsafe
      // create an uninitialized array
      let t: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninitialized();
      // convert it to an array of uninitialized values
      let mut t: [mem::MaybeUninit<T>; N] = mem::transmute(t);
      // initialize the values
      t[0].set("Hi".to_string());
      t[1].set("there".to_string());
      // use the values
      println!(" ", t[0].get_ref(), t[1].get_ref());
      // drop the values
      mem::replace(&mut t[0], mem::MaybeUninit::uninitialized()).into_initialized();
      mem::replace(&mut t[1], mem::MaybeUninit::uninitialized()).into_initialized();




      I should note that miri runs it without problems.










      share|improve this question














      Is the following code sound?



      #![feature(maybe_uninit)]
      use std::mem;
      const N: usize = 2; // or another number
      type T = String; // or any other type

      fn main()
      unsafe
      // create an uninitialized array
      let t: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninitialized();
      // convert it to an array of uninitialized values
      let mut t: [mem::MaybeUninit<T>; N] = mem::transmute(t);
      // initialize the values
      t[0].set("Hi".to_string());
      t[1].set("there".to_string());
      // use the values
      println!(" ", t[0].get_ref(), t[1].get_ref());
      // drop the values
      mem::replace(&mut t[0], mem::MaybeUninit::uninitialized()).into_initialized();
      mem::replace(&mut t[1], mem::MaybeUninit::uninitialized()).into_initialized();




      I should note that miri runs it without problems.







      rust






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 11:54









      llogiqllogiq

      7,34812851




      7,34812851






















          1 Answer
          1






          active

          oldest

          votes


















          5














          From what I can tell, this is one of those things that are likely to work but not guaranteed, and may be subject to compiler-specific or platform-specific behavior.



          MaybeUninit is defined as follows in the current source:



          #[allow(missing_debug_implementations)]
          #[unstable(feature = "maybe_uninit", issue = "53491")]
          pub union MaybeUninit<T>
          uninit: (),
          value: ManuallyDrop<T>,



          Since it's not marked with the #[repr] attribute (as opposed to for instance ManuallyDrop), it's in the default representation, of which the reference says this:




          Nominal types without a repr attribute have the default representation. Informally, this representation is also called the rust representation.



          There are no guarantees of data layout made by this representation.




          In order to transmute from Wrapper<[T]> to [Wrapper<T>], it must be the case that the memory layout of Wrapper<T> is exactly the same as the memory layout of T. This is the case for a number of wrappers, such as the previously mentioned ManuallyDrop, and those will usually be marked with the #[repr(transparent)] attribute.



          But in this case, this is not necessarily true. Since () is a zero-size type, it's likely that the compiler will use the same memory layout for T and MaybeUninit<T> (and this is why it's working for you), but it is also possible
          that the compiler decides to use some other memory layout (e.g. for optimization purposes), in which case transmuting will not work anymore.




          As a specific example, the compiler may chose to use the following memory layout for MaybeUninit<T>:



          +---+---+...+---+
          | T | b | where b is "is initialized" flag
          +---+---+...+---+


          According to the above quote, the compiler is allowed to do this. In this case, [MaybeUninit<T>] and MaybeUninit<[T]> have different memory layouts, since MaybeUninit<[T]> has one b for the entire array, while [MaybeUninit<T>] has one b for each MaybeUninit<T> in the array:



          MaybeUninit<[T]>:
          +---+...+---+---+...+---+...+---+...+---+---+
          | T[0] | T[1] | … | T[n-1] | b |
          +---+...+---+---+...+---+...+---+...+---+---+
          Total size: n * size_of::<T>() + 1

          [MaybeUninit<T>]
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          | T[0] |b[0]| T[1] |b[1]| … | T[n-1] |b[n-1]|
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          Total size: (n + 1) * size_of::<T>()





          share|improve this answer

























          • Does it make a difference if I use MaybeUninit<[MaybeUninit<T>]>?

            – llogiq
            Mar 25 at 19:02






          • 1





            @llogiq No, it shouldn't. I've added a specific example to try to clarify example what I meant here.

            – Frxstrem
            Mar 25 at 19:21











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313460%2fis-it-sound-to-transmute-a-maybeuninitt-n-to-maybeuninitt-n%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          From what I can tell, this is one of those things that are likely to work but not guaranteed, and may be subject to compiler-specific or platform-specific behavior.



          MaybeUninit is defined as follows in the current source:



          #[allow(missing_debug_implementations)]
          #[unstable(feature = "maybe_uninit", issue = "53491")]
          pub union MaybeUninit<T>
          uninit: (),
          value: ManuallyDrop<T>,



          Since it's not marked with the #[repr] attribute (as opposed to for instance ManuallyDrop), it's in the default representation, of which the reference says this:




          Nominal types without a repr attribute have the default representation. Informally, this representation is also called the rust representation.



          There are no guarantees of data layout made by this representation.




          In order to transmute from Wrapper<[T]> to [Wrapper<T>], it must be the case that the memory layout of Wrapper<T> is exactly the same as the memory layout of T. This is the case for a number of wrappers, such as the previously mentioned ManuallyDrop, and those will usually be marked with the #[repr(transparent)] attribute.



          But in this case, this is not necessarily true. Since () is a zero-size type, it's likely that the compiler will use the same memory layout for T and MaybeUninit<T> (and this is why it's working for you), but it is also possible
          that the compiler decides to use some other memory layout (e.g. for optimization purposes), in which case transmuting will not work anymore.




          As a specific example, the compiler may chose to use the following memory layout for MaybeUninit<T>:



          +---+---+...+---+
          | T | b | where b is "is initialized" flag
          +---+---+...+---+


          According to the above quote, the compiler is allowed to do this. In this case, [MaybeUninit<T>] and MaybeUninit<[T]> have different memory layouts, since MaybeUninit<[T]> has one b for the entire array, while [MaybeUninit<T>] has one b for each MaybeUninit<T> in the array:



          MaybeUninit<[T]>:
          +---+...+---+---+...+---+...+---+...+---+---+
          | T[0] | T[1] | … | T[n-1] | b |
          +---+...+---+---+...+---+...+---+...+---+---+
          Total size: n * size_of::<T>() + 1

          [MaybeUninit<T>]
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          | T[0] |b[0]| T[1] |b[1]| … | T[n-1] |b[n-1]|
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          Total size: (n + 1) * size_of::<T>()





          share|improve this answer

























          • Does it make a difference if I use MaybeUninit<[MaybeUninit<T>]>?

            – llogiq
            Mar 25 at 19:02






          • 1





            @llogiq No, it shouldn't. I've added a specific example to try to clarify example what I meant here.

            – Frxstrem
            Mar 25 at 19:21















          5














          From what I can tell, this is one of those things that are likely to work but not guaranteed, and may be subject to compiler-specific or platform-specific behavior.



          MaybeUninit is defined as follows in the current source:



          #[allow(missing_debug_implementations)]
          #[unstable(feature = "maybe_uninit", issue = "53491")]
          pub union MaybeUninit<T>
          uninit: (),
          value: ManuallyDrop<T>,



          Since it's not marked with the #[repr] attribute (as opposed to for instance ManuallyDrop), it's in the default representation, of which the reference says this:




          Nominal types without a repr attribute have the default representation. Informally, this representation is also called the rust representation.



          There are no guarantees of data layout made by this representation.




          In order to transmute from Wrapper<[T]> to [Wrapper<T>], it must be the case that the memory layout of Wrapper<T> is exactly the same as the memory layout of T. This is the case for a number of wrappers, such as the previously mentioned ManuallyDrop, and those will usually be marked with the #[repr(transparent)] attribute.



          But in this case, this is not necessarily true. Since () is a zero-size type, it's likely that the compiler will use the same memory layout for T and MaybeUninit<T> (and this is why it's working for you), but it is also possible
          that the compiler decides to use some other memory layout (e.g. for optimization purposes), in which case transmuting will not work anymore.




          As a specific example, the compiler may chose to use the following memory layout for MaybeUninit<T>:



          +---+---+...+---+
          | T | b | where b is "is initialized" flag
          +---+---+...+---+


          According to the above quote, the compiler is allowed to do this. In this case, [MaybeUninit<T>] and MaybeUninit<[T]> have different memory layouts, since MaybeUninit<[T]> has one b for the entire array, while [MaybeUninit<T>] has one b for each MaybeUninit<T> in the array:



          MaybeUninit<[T]>:
          +---+...+---+---+...+---+...+---+...+---+---+
          | T[0] | T[1] | … | T[n-1] | b |
          +---+...+---+---+...+---+...+---+...+---+---+
          Total size: n * size_of::<T>() + 1

          [MaybeUninit<T>]
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          | T[0] |b[0]| T[1] |b[1]| … | T[n-1] |b[n-1]|
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          Total size: (n + 1) * size_of::<T>()





          share|improve this answer

























          • Does it make a difference if I use MaybeUninit<[MaybeUninit<T>]>?

            – llogiq
            Mar 25 at 19:02






          • 1





            @llogiq No, it shouldn't. I've added a specific example to try to clarify example what I meant here.

            – Frxstrem
            Mar 25 at 19:21













          5












          5








          5







          From what I can tell, this is one of those things that are likely to work but not guaranteed, and may be subject to compiler-specific or platform-specific behavior.



          MaybeUninit is defined as follows in the current source:



          #[allow(missing_debug_implementations)]
          #[unstable(feature = "maybe_uninit", issue = "53491")]
          pub union MaybeUninit<T>
          uninit: (),
          value: ManuallyDrop<T>,



          Since it's not marked with the #[repr] attribute (as opposed to for instance ManuallyDrop), it's in the default representation, of which the reference says this:




          Nominal types without a repr attribute have the default representation. Informally, this representation is also called the rust representation.



          There are no guarantees of data layout made by this representation.




          In order to transmute from Wrapper<[T]> to [Wrapper<T>], it must be the case that the memory layout of Wrapper<T> is exactly the same as the memory layout of T. This is the case for a number of wrappers, such as the previously mentioned ManuallyDrop, and those will usually be marked with the #[repr(transparent)] attribute.



          But in this case, this is not necessarily true. Since () is a zero-size type, it's likely that the compiler will use the same memory layout for T and MaybeUninit<T> (and this is why it's working for you), but it is also possible
          that the compiler decides to use some other memory layout (e.g. for optimization purposes), in which case transmuting will not work anymore.




          As a specific example, the compiler may chose to use the following memory layout for MaybeUninit<T>:



          +---+---+...+---+
          | T | b | where b is "is initialized" flag
          +---+---+...+---+


          According to the above quote, the compiler is allowed to do this. In this case, [MaybeUninit<T>] and MaybeUninit<[T]> have different memory layouts, since MaybeUninit<[T]> has one b for the entire array, while [MaybeUninit<T>] has one b for each MaybeUninit<T> in the array:



          MaybeUninit<[T]>:
          +---+...+---+---+...+---+...+---+...+---+---+
          | T[0] | T[1] | … | T[n-1] | b |
          +---+...+---+---+...+---+...+---+...+---+---+
          Total size: n * size_of::<T>() + 1

          [MaybeUninit<T>]
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          | T[0] |b[0]| T[1] |b[1]| … | T[n-1] |b[n-1]|
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          Total size: (n + 1) * size_of::<T>()





          share|improve this answer















          From what I can tell, this is one of those things that are likely to work but not guaranteed, and may be subject to compiler-specific or platform-specific behavior.



          MaybeUninit is defined as follows in the current source:



          #[allow(missing_debug_implementations)]
          #[unstable(feature = "maybe_uninit", issue = "53491")]
          pub union MaybeUninit<T>
          uninit: (),
          value: ManuallyDrop<T>,



          Since it's not marked with the #[repr] attribute (as opposed to for instance ManuallyDrop), it's in the default representation, of which the reference says this:




          Nominal types without a repr attribute have the default representation. Informally, this representation is also called the rust representation.



          There are no guarantees of data layout made by this representation.




          In order to transmute from Wrapper<[T]> to [Wrapper<T>], it must be the case that the memory layout of Wrapper<T> is exactly the same as the memory layout of T. This is the case for a number of wrappers, such as the previously mentioned ManuallyDrop, and those will usually be marked with the #[repr(transparent)] attribute.



          But in this case, this is not necessarily true. Since () is a zero-size type, it's likely that the compiler will use the same memory layout for T and MaybeUninit<T> (and this is why it's working for you), but it is also possible
          that the compiler decides to use some other memory layout (e.g. for optimization purposes), in which case transmuting will not work anymore.




          As a specific example, the compiler may chose to use the following memory layout for MaybeUninit<T>:



          +---+---+...+---+
          | T | b | where b is "is initialized" flag
          +---+---+...+---+


          According to the above quote, the compiler is allowed to do this. In this case, [MaybeUninit<T>] and MaybeUninit<[T]> have different memory layouts, since MaybeUninit<[T]> has one b for the entire array, while [MaybeUninit<T>] has one b for each MaybeUninit<T> in the array:



          MaybeUninit<[T]>:
          +---+...+---+---+...+---+...+---+...+---+---+
          | T[0] | T[1] | … | T[n-1] | b |
          +---+...+---+---+...+---+...+---+...+---+---+
          Total size: n * size_of::<T>() + 1

          [MaybeUninit<T>]
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          | T[0] |b[0]| T[1] |b[1]| … | T[n-1] |b[n-1]|
          +---+...+---+----+---+...+---+----+...+---+...+---+------+
          Total size: (n + 1) * size_of::<T>()






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 19:23









          Shepmaster

          165k16346496




          165k16346496










          answered Mar 23 at 12:44









          FrxstremFrxstrem

          19.6k75084




          19.6k75084












          • Does it make a difference if I use MaybeUninit<[MaybeUninit<T>]>?

            – llogiq
            Mar 25 at 19:02






          • 1





            @llogiq No, it shouldn't. I've added a specific example to try to clarify example what I meant here.

            – Frxstrem
            Mar 25 at 19:21

















          • Does it make a difference if I use MaybeUninit<[MaybeUninit<T>]>?

            – llogiq
            Mar 25 at 19:02






          • 1





            @llogiq No, it shouldn't. I've added a specific example to try to clarify example what I meant here.

            – Frxstrem
            Mar 25 at 19:21
















          Does it make a difference if I use MaybeUninit<[MaybeUninit<T>]>?

          – llogiq
          Mar 25 at 19:02





          Does it make a difference if I use MaybeUninit<[MaybeUninit<T>]>?

          – llogiq
          Mar 25 at 19:02




          1




          1





          @llogiq No, it shouldn't. I've added a specific example to try to clarify example what I meant here.

          – Frxstrem
          Mar 25 at 19:21





          @llogiq No, it shouldn't. I've added a specific example to try to clarify example what I meant here.

          – Frxstrem
          Mar 25 at 19:21



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313460%2fis-it-sound-to-transmute-a-maybeuninitt-n-to-maybeuninitt-n%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현