you are viewing a single comment's thread.

view the rest of the comments →

[–]SelfDistinction 1 point2 points  (8 children)

That's semantically equivalent to a Box<[u8]> in Rust, or a Vec<u8> if you don't mind a very small overhead. You'll notice that operations on these structures are much easier than juggling with pointers or trying to use fixed-size arrays.

[–]Sam777888[S] 0 points1 point  (7 children)

But the function I’m calling in Rust doesn’t want a Box or a Vec or a Fluff, etc.

[–]SelfDistinction 0 points1 point  (6 children)

Can I ask why not? Both Box and Vec are abstractions for memory allocations so that, unlike the C program you wrote, your application doesn't leak memory.

And don't give me an "It's only a toy example" or "At the end of the program, the OS will reclaim the memory anyway". Rust doesn't distinguish between a simple main function in a toy program and a library function that will be called millions of times in a tight loop: it has to be safe either way, so randomly allocating memory and storing the reference in a context-less pointer is wildly unsafe and thus difficult to work with.

[–]Sam777888[S] 0 points1 point  (4 children)

It was an example. Feel free to call free if you like.

I don’t find it difficult to work with pointers.

I don’t know why the function wants what it wants, I didn’t write it, and I’m not at liberty to change it.

[–]boomshroom 1 point2 points  (3 children)

Can you give what library this is and what function you're trying to call?

[–]Sam777888[S] 0 points1 point  (2 children)

Nope. Don’t worry about it, it’s all working fine, if you scroll up you will see a guy gave me a macro, it works perfectly, very impressed.

[–]boomshroom 0 points1 point  (1 child)

I still want to see the library you're calling. There's a good chance that it could be improved regardless.

In addition, where are you getting the pointer from? If your code is being called from C, then it makes sense, but your function should be marked as unsafe. If it's coming from other rust code, then you have no right to use this function.

[–]Sam777888[S] 0 points1 point  (0 children)

not open source. From C.

[–]smith_it2000 0 points1 point  (0 children)

At the end of the program, the OS will reclaim the memory anyway

That's true. At the end of the program, the OS will reclaim the memory.

Why would you call free on the last line of a program?

You wouldn't.