Skip to content

Commit

Permalink
Merge pull request #12 from apepkuss/refactor-image-type
Browse files Browse the repository at this point in the history
refactor: remove lifetime annotation from `ImageType`
  • Loading branch information
hydai authored Sep 26, 2024
2 parents 6399713 + 0fb8898 commit 4b98ce2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
if let Context::ImageToImage(mut image_to_image) = context.create_context().unwrap() {
image_to_image
.set_prompt("with blue eyes")
.set_image(ImageType::Path("output.png"))
.set_image(ImageType::Path("output.png".to_string()))
.set_output_path("output2.png")
.generate()
.unwrap();
Expand Down
42 changes: 21 additions & 21 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub enum Task {
}

#[derive(Debug)]
pub enum Context<'a> {
TextToImage(TextToImage<'a>),
ImageToImage(ImageToImage<'a>),
pub enum Context {
TextToImage(TextToImage),
ImageToImage(ImageToImage),
}

#[derive(Debug)]
Expand Down Expand Up @@ -54,13 +54,13 @@ pub struct StableDiffusion {
}

#[derive(Debug)]
pub struct BaseContext<'a> {
pub struct BaseContext {
pub session_id: u32,
pub prompt: String,
pub guidance: f32,
pub width: i32,
pub height: i32,
pub control_image: ImageType<'a>,
pub control_image: ImageType,
pub negative_prompt: String,
pub clip_skip: i32,
pub cfg_scale: f32,
Expand All @@ -77,8 +77,8 @@ pub struct BaseContext<'a> {
pub upscale_repeats: i32,
pub output_path: String,
}
pub trait BaseFunction<'a> {
fn base(&mut self) -> &mut BaseContext<'a>;
pub trait BaseFunction {
fn base(&mut self) -> &mut BaseContext;
fn set_prompt(&mut self, prompt: &str) -> &mut Self {
{
self.base().prompt = prompt.to_string();
Expand Down Expand Up @@ -131,14 +131,14 @@ pub trait BaseFunction<'a> {
}

#[derive(Debug)]
pub struct TextToImage<'a> {
pub common: BaseContext<'a>,
pub struct TextToImage {
pub common: BaseContext,
}

#[derive(Debug)]
pub struct ImageToImage<'a> {
pub common: BaseContext<'a>,
pub image: ImageType<'a>,
pub struct ImageToImage {
pub common: BaseContext,
pub image: ImageType,
pub strength: f32,
}

Expand Down Expand Up @@ -253,7 +253,7 @@ impl StableDiffusion {
guidance: 3.5,
width: 512,
height: 512,
control_image: ImageType::Path(""),
control_image: ImageType::Path("".to_string()),
negative_prompt: "".to_string(),
clip_skip: -1,
cfg_scale: 7.0,
Expand All @@ -274,7 +274,7 @@ impl StableDiffusion {
Task::TextToImage => Ok(Context::TextToImage(TextToImage { common })),
Task::ImageToImage => Ok(Context::ImageToImage(ImageToImage {
common,
image: ImageType::Path(""),
image: ImageType::Path("".to_string()),
strength: 0.75,
})),
}
Expand All @@ -287,8 +287,8 @@ impl StableDiffusion {
self
}
}
impl<'a> BaseFunction<'a> for TextToImage<'a> {
fn base(&mut self) -> &mut BaseContext<'a> {
impl BaseFunction for TextToImage {
fn base(&mut self) -> &mut BaseContext {
&mut self.common
}
fn generate(&self) -> Result<(), WasmedgeSdErrno> {
Expand Down Expand Up @@ -328,15 +328,15 @@ impl<'a> BaseFunction<'a> for TextToImage<'a> {
}
}

impl<'a> BaseFunction<'a> for ImageToImage<'a> {
fn base(&mut self) -> &mut BaseContext<'a> {
impl BaseFunction for ImageToImage {
fn base(&mut self) -> &mut BaseContext {
&mut self.common
}
fn generate(&self) -> Result<(), WasmedgeSdErrno> {
if self.common.prompt.is_empty() {
return Err(WASMEDGE_SD_ERRNO_INVALID_ARGUMENT);
}
match self.image {
match &self.image {
ImageType::Path(path) => {
if path.is_empty() {
return Err(WASMEDGE_SD_ERRNO_INVALID_ARGUMENT);
Expand Down Expand Up @@ -377,8 +377,8 @@ impl<'a> BaseFunction<'a> for ImageToImage<'a> {
Ok(())
}
}
impl<'a> ImageToImage<'a> {
pub fn set_image(&mut self, image: ImageType<'a>) -> &mut Self {
impl ImageToImage {
pub fn set_image(&mut self, image: ImageType) -> &mut Self {
{
self.image = image;
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/stable_diffusion_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ pub enum ScheduleT {
GITS = 5,
}
#[derive(Debug)]
pub enum ImageType<'a> {
Path(&'a str),
pub enum ImageType {
Path(String),
}
fn parse_image(image: &ImageType) -> (i32, i32) {
match image {
Expand Down

0 comments on commit 4b98ce2

Please sign in to comment.