Comments (12)
I think it's enough to pass a regular reference?
No as the data has to live until the destroy function is called. It can outlive the current stack frame
from cairo.
I mean, doesn't Box::new(slice)
copy the slice into the heap?
Going back... if we wanted this function to avoid a copy, wouldn't it have to take ownership of a Vec or whatever? What's the idiomatic way of transferring ownership of a byte buffer?
from cairo.
@federicomenaquintero Vec<u8>
is implementing AsRef<[u8]>
and you can pass it into that function directly. No copying will happen then, same thing with basically everything else that has the memory heap allocated.
If you put an array in there, it will be copied from the stack to the heap indeed.
from cairo.
I think what happens is this:
pub fn set_mime_data<T: AsRef<[u8]> + 'static>(&self, mime_type: &str, slice: T) -> Result<(), Status> {
let b = Box::new(slice);
The Box::new(slice)
creates a heap allocation big enough for the slice (ptr, usize) and moves the slice there. Below, there is this:
let user_data = Box::into_raw(b);
let status = unsafe {
let mime_type = CString::new(mime_type).unwrap();
ffi::cairo_surface_set_mime_data(self.to_raw_none(),
mime_type.as_ptr(),
data,
size as c_ulong,
Some(unbox::<T>),
user_data as *mut _,
)
};
The destroy func is unbox
, which will free the tiny allocation for (ptr, usize)... but the original buffer has a static
lifetime.
I think the semantics of cairo_surface_set_mime_data
is to take ownership of the buffer passed to it, and the C-ism there is to provide the destroy func. If this is correct, I think this wrapper should instead take ownership of a buf: Vec<u8>
. This avoids copies; the wrapper can then manage that Vec
as it wants. (Maybe just box it and pass the into_raw
to the Cairo function?).
Alternatively, we can keep it taking a slice (or an AsRef<[u8]>
like right now) but really do a copy internally. I think the static
lifetime requirement makes the wrapper as it is rather useless. For example, librsvg will read JPG referenced from the SVG, and then surface_set_mime_data
with that JPG buffer. But it can't be done with a static lifetime.
from cairo.
Cooking a patch right now.
from cairo.
You apparently didn't see my comments on IRC :)
federico: for your cairo issue, your analysis about passing arrays is correct. you don't really want to do that
federico: but if you look around what else implements AsRef<[u8]> you'll see that e.g. Vec<u8> also implements that. or a gst::MappedBuffer. or a hyper::Chunk. or ... :)
federico: you can pass any of these in there and e.g. in the case of a Vec the only thing copied to the Box is the metadata of the Vec (pointer, capacity, length)
federico: the 'static on the data only means that it must have no references itself to any non-static things. i.e. you can't pass anything in there that has references to a stack frame for example. fully heap allocated data is fine, for example. and the function takes ownership of the thing you pass there
federico: oh, Arc<Vec<u8>> also implements AsRef<[u8]> btw if you want to prevent some copies ;)
from cairo.
E.g., try surface.set_mime_data("foo/bar", vec![1, 2, 3, 4u8])
from cairo.
We use the same pattern for gio::OutputStream::write_async()
etc btw, and also in GStreamer for wrapping arbitrary things that are like byte arrays in buffers. So that's a well-established pattern that works fine elsewhere.
from cairo.
OMG, then I think my problem may have been that I was passing &myvec
instead of transferring ownership of myvec
. I'll try that again in librsvg. Thanks!
from cairo.
Ok, let's close this then?
from cairo.
@GuillaumeGomez can you close this for now? :)
from cairo.
Sure.
from cairo.
Related Issues (20)
- Context::set_source_surface not properly working HOT 2
- rust ImageSurface get_data panicled! HOT 9
- Make ImageSurface::get_data immutable HOT 10
- Add quick explanation of feature flags in documentation HOT 4
- Include rustfmt in CI HOT 1
- SvgSurface path should be an Option HOT 1
- Cross compilation error with MinGW-w64 HOT 3
- Possible memory leak in set_source_pixbuf HOT 12
- How to get the device from a surface? HOT 1
- Missing surface function(s)
- Implement Error trait for cairo::Success HOT 5
- Segmentation fault in safe code when accessing raw data from a finished surface HOT 7
- `cairo_ft_*` functions are assumed to be available HOT 5
- Get data out of a referenced ImageSurface HOT 1
- Stride issues HOT 3
- Make the cairo-rs-sys crate a public crate HOT 1
- Consider re-exporting `cairo-sys-rs` under `ffi`. HOT 4
- Why ImageSurface::create_for_data requires static lifetime? HOT 15
- Examples using Cairo for Rust HOT 1
- exit code: 0xc0000135, STATUS_DLL_NOT_FOUND HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cairo.