-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a separate libc_types crate for basic C types #1783
Conversation
# Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
- Adds an additional crate to the standard library. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be the standard library or the libc on crates.io?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both in a sense, since libc is a (hidden) dependency of the standard library. But you are right, from a user's point of view this is just another crate on crates.io.
pub type uint8_t; | ||
pub type uint16_t; | ||
pub type uint32_t; | ||
pub type uint64_t; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are fixed-width C types ever useful in Rust?
It's always more convenient to write native Rust uN
instead of uintN_t
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I included these because libc
exports these types as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expected this, but I thought may be there was some other reasons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the hypothetical future situation where Rust knows how to talk directly to a C++ library instead of going through C wrapper functions, it will be necessary to match the mangled names of the C++ functions, and that is likely to involve making a distinction between uNN
and uintNN_t
, because in C land the uintNN_t
types are "just" typedefs for whichever of the unpredictably-sized primitive integer types is the right match, and the name mangling uses the primitives, so for instance the mangled name of
extern "C++" foo(x: libc::uint64_t);
is _Z3foom
on x86_64-linux but _Z3fooy
on i686-linux.
I'm not really sure I quite understand the motivation for specifically the crate split itself here. Presumably crates could just use the I guess put another way, I'm not particularly clear on what the downsides of using libc are today. Could you clarify in the RFC what these downsides are? As a side note, as well the drawbacks and alternatives sections here are particularly bare, and the alternatives section doesn't seem to have a lot of thought put into it. Perhaps these sections could be fleshed out a bit more with some more information? For example "adds an additional crate to the standard library" to me is a pretty massive drawback as an incredibly large hammer. Why not crates.io? Why not part of libcore? Why not in libc itself? (for example...) |
The main motivation is being able to use the C types without linking to libc. This allows them to be used in environments that cannot use libc (kernels) or don't have a libc implementation (bare metal). As mentioned in the comment above, it seems that I didn't word the drawback correctly. Like |
pub type c_longlong; | ||
pub type c_ulonglong; | ||
pub type intmax_t; | ||
pub type uintmax_t; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case intmax_t
is some primitive type not supported by rustc, what happens (i.e. what’s the behaviour of current liblibc)?
I would really like this to happen and my exact usecase for such a split is the Currently I prefer using |
What happened to
|
@comex Ah, nice catch, I seem to have forgotten about
As for the other types, they simply aren't currently supported by Rust. |
I don't fully agree with the motivation behind this point, depending on what's happening. If libc is available, there's no detriment linking against it. It'll get stripped if it's not used. If libc is not available, however, then you're on a whole separate target, in which case the libc crate itself can just give you a different API (because no library exists). That is, in my mind libc doesn't link to the actual libc for any platform that doesn't have one, so this problem wouldn't exist.
These are just type definitions, why would it need to be included in the standard library? Why not ship a
Technically, this is incorrect. If you have a crate that binds a C API crate, then that native library requires libc, so that dependency needs to be expressed. If the liblibc crate is "too big", then that's a problem that needs to be solved. |
I think the motivation here is that the Let's say your target has a toolchain based on a minimal version of libc like newlib. If you try to link the crate solely for its type definitions, you will fail at compile-time if The case above is one that I'm personally familiar with, and perhaps that particular case could be addressed with explicit newlib support in the
I think what he means is that
That's what he's proposing. |
I'd like to see this as well, for building core-only programs with no libc that have FFI calls between Rust and C. As a minor nit/bikeshed, can we call it something shorter than |
Is this different from |
@SimonSapin Yes, this is basically a |
Hm so I may not quite be being clear here, so let me try again! The purpose of the Comments have been made about how libc today "requires too many libraries" or "pulls in too many symbols". This is not an inherent limitation to libc, but rather because libc is being lied to about its platform. The libc crates has been ported to a set of platforms, and it provides absolutely no guarantee of working on any other platform. If you've got a platform that doesn't have libc/librt/libutil then libc has not yet been ported to your platform. If such a platform masquerades as The intent of the libc crate design was specifically to avoid crates like Does that make sense? This is what I mean by the alternative above of just emptying libc of function declarations and just having types for these sorts of platforms. For example I could imagine an alternative to this RFC along the lines of:
That is, if
It does indeed! I explained this above, but to reiterate here this is not a limitation of the libc crate. The libc crate has only been ported to platforms with libraries like glibc/musl, it just hasn't been ported to any other platforms (but it certainly can be!)
These problems are all indicative of the work has not been done to port libc. That work can certainly be done, and it can be done at any time in the libc crate. Attempting to masquerade as a different platform and then seeing failures to me isn't a reason to abandon the libc crate entirely.
To clarify, this crate is being proposed to ship with the distribution, not on crates.io. Shipping a crate on crates.io doesn't require an RFC. |
I would argue that the The idea of
Not exactly. What I am proposing here is a crate on crates.io, however it is important that libc re-exports these types instead of using its own to avoid conflicting definitions of |
@alexcrichton so does this mean |
@Amanieu I think @alexcrichton's proposal sounds fine if you replace |
My point is that the purpose of My idea of
Indeed! |
@Amanieu if you could blacklist the relevant hosted features/scenarios/whatever for libc, that would solve the problem, right? |
What if you need |
In the case where the types are mixed into the |
@cuviper I would assert that those are two separate targets, not one. In that sense libc would compile itself separately for both targets. @jmesmon there shouldn't be any reason to not want functions from libc. If functions don't exist on a platform then libc was miscompiled (or not ported). If the functions do exist then there's no cost to compiling them in. |
|
I disagree. It is useful to document statically in a toolchain-enforced way that no C library functions are being used by a crate, when that is the case. When looking at just the dependencies of a libc-based crate, one cannot tell whether it depends on C library functions or not. This matters for some embedding use cases. |
The libs team discussed this RFC again during triage yesterday. The current consensus is to offer a canonical way of producing an "unknown, opaque type" (a better Once we make such a move, we'll be in a position to (1) redefine As such, I'm going to propose to close this RFC for the time being, and encourage further discussion on #1861. We can reopen the avenue proposed by this RFC if the opaque type direction doesn't pan out. @rfcbot fcp close |
Team member @aturon has proposed to close this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Can we move #1861 into FCP as well then? There hasn't been any on-topic activity there in over a month. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
Looks like nothing new came up during FCP, so closing. |
I’m late to the party, but |
@SimonSapin we already have different incompatible definitions of Edit: Although that last part might not be possible because of what you mentioned. Which means we'd need yet another breaking |
New RFC to propose moving |
For people still following this thread: #2521 is now in FCP. |
I know this is pretty old and was argued to death. |
I agree. People are now asking me to add support to my crate for platforms that libc doesn't support, and for which libc refuses to add support. I think this RFC should be reopened since it solves exactly the problem that needs to be solved here for these embedded platforms and wasm32-unknown-unknown. |
Still need as without ctype, it's create a gap between std and other no_std library. |
Now that we have |
Do I need create new PR for rfcs or just create a PR is enough? |
A PR is enough. |
I’m ok with that but I thought the reason this hadn’t happened a long time ago was desire to keep Also, assuming all of |
yeap,its os dependes, but its only about ABI, not about runtime, there is no function linkage, so I think thats not an issue, of cuase, std.os.rae should re export core.ffi |
Rendered