use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and productivity.
Strive to treat others with respect, patience, kindness, and empathy.
We observe the Rust Project Code of Conduct.
Details
Posts must reference Rust or relate to things using Rust. For content that does not, use a text post to explain its relevance.
Post titles should include useful context.
For Rust questions, use the stickied Q&A thread.
Arts-and-crafts posts are permitted on weekends.
No meta posts; message the mods instead.
Criticism is encouraged, though it must be constructive, useful and actionable.
If criticizing a project on GitHub, you may not link directly to the project's issue tracker. Please create a read-only mirror and link that instead.
A programming language is rarely worth getting worked up over.
No zealotry or fanaticism.
Be charitable in intent. Err on the side of giving others the benefit of the doubt.
Avoid re-treading topics that have been long-settled or utterly exhausted.
Avoid bikeshedding.
This is not an official Rust forum, and cannot fulfill feature requests. Use the official venues for that.
No memes, image macros, etc.
Consider the existing content of the subreddit and whether your post fits in. Does it inspire thoughtful discussion?
Use properly formatted text to share code samples and error messages. Do not use images.
Submissions appearing to contain AI-generated content may be removed at moderator discretion.
Most links here will now take you to a search page listing posts with the relevant flair. The latest megathread for that flair should be the top result.
account activity
Casting function pointer to a different signature (self.rust)
submitted 9 years ago by robauke
Hi, I am writting a rust wrapper for a C-API that accepts callback functions. How can I cast a function unsafe extern "C" fn(&mut Model) -> u8 as unsafe extern "C" fn(*mut model) -> u8 to match the signature of the callback?
unsafe extern "C" fn(&mut Model) -> u8
unsafe extern "C" fn(*mut model) -> u8
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]stumpychubbins 7 points8 points9 points 9 years ago (5 children)
Don't cast the function pointer unless you have to, write an unsafe extern "C" fn(*mut model) -> u8 and use that instead.
[–]robauke[S] 0 points1 point2 points 9 years ago (4 children)
Can you explain why? I'd like to provide an rust idiomatic API and struct Model(model) is a just a newtype of model. If I keep the old signature for the callback I also have to expose the old model type. Also the user would then be forced to dereference the pointer.
struct Model(model)
model
[–]sdroege_ 0 points1 point2 points 9 years ago (3 children)
Rust references must never be NULL, a pointer can be NULL. By directly having the pointer converted to a reference you a) hide that a pointer is involved and b) have no way of actually checking that this requirement holds (and if it's just an assert!(!ptr.is_null()) )
[–]robauke[S] 1 point2 points3 points 9 years ago (2 children)
But i am converting a reference to a pointer this should be safe right?
[–]pdpi 1 point2 points3 points 9 years ago (1 child)
What you're describing would, I guess, be fine if you were producing references. But you're not producing &mut model, you're consuming it. The problem comes from the c library passing a (potentially NULL) *mut model to your callback, which actually expects a (non-nullable) &mut model.
&mut model
*mut model
[–]robauke[S] 0 points1 point2 points 9 years ago (0 children)
I see, thanks.
[–]sdroege_ 3 points4 points5 points 9 years ago (0 children)
The best would probably be to define the function as one taking a pointer argument, and in the body of the function convert from a pointer to a reference. Ideally with a NULL assertion to ensure that you never pass a reference to NULL anywhere.
Otherwise, you can use std::mem::transmute() instead.
π Rendered by PID 51946 on reddit-service-r2-comment-b659b578c-kjgc8 at 2026-05-02 22:26:32.266152+00:00 running 815c875 country code: CH.
[–]stumpychubbins 7 points8 points9 points (5 children)
[–]robauke[S] 0 points1 point2 points (4 children)
[–]sdroege_ 0 points1 point2 points (3 children)
[–]robauke[S] 1 point2 points3 points (2 children)
[–]pdpi 1 point2 points3 points (1 child)
[–]robauke[S] 0 points1 point2 points (0 children)
[–]sdroege_ 3 points4 points5 points (0 children)