question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Legal values for a `&T` or `&mut T` where `T` is zero-sized

See original GitHub issue

In #3, the question was posed what values a Box<T> where T is ZST can have – one might ask a similar question about a &T. The answer may well be the same as #3, but it could be different, depending on whether the Box API is deemed to impose its own limitations.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
arielb1commented, Sep 12, 2016

I mean the protocol that governs the type while it is shared

This is the memory model repo, not the soundness repo.

1reaction
mystorcommented, Aug 18, 2016

I think that any non-null value should be OK, and that the compiler should correctly propagate that value. It is useful and expected in #[repr©] structs for a ZST before a sized type to have the same address of that field.

In addition, ZSTs have the advantage that they can be passed around as &T and &mut T while not allowing the consumer to move the actual data behind the reference (which may be a variable sized #[repr©] struct). The methods on the ZST T can then cast the reference to a reference to the actual backing data type, and avoid unsafety that would come by allowing moving, for example, the header of a FFI C type. I think patterns like that should be OK, as they solve a real problem, and shouldn’t break anything.

This means that pointers to opaque C structs can be passed around like native rust types behind &T and &mut T, rather than requiring special structs like:

struct SomeCTypeRef<'a> {
    ptr: *const SomeCType,
    _marker: PhantomData<&'a u8>,
}

which require a large number of impls on them and don’t provide a nice API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Legal values for a Box<T> where T is zero-sized #3 - GitHub
My preference is that any non-null value is a valid pointer to a 0-sized type. Also, for things to make sense, all reborrows...
Read more >
Why do zero-sized types cause real allocations in some cases?
Almost every operation with a ZST is a no-op since ZSTs have exactly one value, and therefore no state needs to be considered...
Read more >
Rust (Programming Language) | linkedin-skill-assessments-quizzes
It's creating a pointer on the heap that points to a value on the stack. ... What syntax is required to take a...
Read more >
ndoffice doesn't allow upload of zero-sized documents - Optiable
When you are using Workshare/Literia Compare, you get the error message that ndOffice doesn't allow zero-size documents.
Read more >
Math208 Discrete Mathematics - College of Arts & Sciences
That is, it must be possible to assign a truth value to any compound ... for two 0-1 matrices of the same size...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found