summaryrefslogtreecommitdiffstats
path: root/web/src/widget
diff options
context:
space:
mode:
authorLibravatar Liam Murphy <liampm32@gmail.com>2021-03-21 15:34:20 +1100
committerLibravatar Liam Murphy <liampm32@gmail.com>2021-03-21 15:34:20 +1100
commit99c56b9bff891d55794dccc20f460192ff06f0b8 (patch)
tree7ff123a0e5ea47e1137d0229dacb8025893b4d1f /web/src/widget
parente9d122b1aaa47dc8dad5bd857a7430e56b8a8105 (diff)
downloadiced-99c56b9bff891d55794dccc20f460192ff06f0b8.tar.gz
iced-99c56b9bff891d55794dccc20f460192ff06f0b8.tar.bz2
iced-99c56b9bff891d55794dccc20f460192ff06f0b8.zip
fix: Don't rely on image handle not being dropped
It now causes a memory leak, though. :/
Diffstat (limited to 'web/src/widget')
-rw-r--r--web/src/widget/image.rs57
1 files changed, 21 insertions, 36 deletions
diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs
index 545e7e28..f38f5cfb 100644
--- a/web/src/widget/image.rs
+++ b/web/src/widget/image.rs
@@ -76,14 +76,21 @@ impl<Message> Widget<Message> for Image {
use dodrio::builder::*;
use dodrio::bumpalo::collections::String;
- let src = String::from_str_in(
- match self.handle.data.as_ref() {
- Data::Path(path) => path.to_str().unwrap_or(""),
- Data::ObjectUrl(url) => &url,
- },
- bump,
- )
- .into_bump_str();
+ let src = match self.handle.data.as_ref() {
+ Data::Path(path) => {
+ String::from_str_in(path.to_str().unwrap_or(""), bump)
+ .into_bump_str()
+ }
+ Data::Bytes(bytes) => bump.alloc(
+ Url::create_object_url_with_blob(
+ &Blob::new_with_u8_array_sequence(&Array::of1(
+ &Uint8Array::from(bytes.as_slice()),
+ ))
+ .unwrap(),
+ )
+ .unwrap(),
+ ),
+ };
let alt = String::from_str_in(&self.alt, bump).into_bump_str();
@@ -129,23 +136,10 @@ impl Handle {
/// Creates an image [`Handle`] containing the image data directly.
///
- /// NOTE: this unnecessarily takes ownership of the data to be compaticle with `iced_native`.
- /// If you're only using `iced_web`, you should use `from_slice` instead.
+ /// This is useful if you already have your image loaded in-memory, maybe
+ /// because you downloaded or generated it procedurally.
pub fn from_memory(bytes: Vec<u8>) -> Handle {
- Handle::from_slice(bytes.as_slice())
- }
-
- /// Creates an image [`Handle`] containing the image data directly.
- pub fn from_slice(bytes: &[u8]) -> Handle {
- // This copies the memory twice (once in Uint8Array::from and once in Blob::new),
- // but unsafe code is needed not to do that and #[forbid(unsafe_code)] is on.
- let blob = Blob::new_with_u8_array_sequence(&Array::of1(
- &Uint8Array::from(bytes),
- ))
- .unwrap();
- Self::from_data(Data::ObjectUrl(
- Url::create_object_url_with_blob(&blob).unwrap(),
- ))
+ Self::from_data(Data::Bytes(bytes))
}
fn from_data(data: Data) -> Handle {
@@ -187,24 +181,15 @@ pub enum Data {
/// A remote image
Path(PathBuf),
- /// An Object URL pointing to some image data.
- ObjectUrl(String),
+ /// In-memory data
+ Bytes(Vec<u8>),
}
impl std::fmt::Debug for Data {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Data::Path(path) => write!(f, "Path({:?})", path),
- Data::ObjectUrl(url) => write!(f, "ObjectUrl({:?})", url),
- }
- }
-}
-
-impl Drop for Data {
- fn drop(&mut self) {
- match self {
- Data::ObjectUrl(url) => Url::revoke_object_url(&url).unwrap(),
- _ => {}
+ Data::Bytes(_) => write!(f, "Bytes(...)"),
}
}
}